1. 程式人生 > >SpringBoot整合Quartz-動態讀取任務執行(2.2.1)

SpringBoot整合Quartz-動態讀取任務執行(2.2.1)

本次使用redis作為資料庫,儲存定時任務類
redis的連線不是重點,重點是解析序列化處理過的任務陣列和Quartz如何新增任務

1. JobEntity 用來儲存執行任務類

public class JobEntity implements Serializable {
    //cron表示式
    private String cronExpression;
    //組名
    private String jobGroup = Scheduler.DEFAULT_GROUP;

    private String jobName;

    private
String className; // 執行任務的類(完整路徑 包含包名) private String methodName;//執行任務的方法名 set ... get ... }

2.任務類

public class Test {
    public void xun(){
        System.out.println("--------定時任務2-------");
    }
}
public class Test2 {
    public void test(){
        System.out.println("--------定時任務1-------"
); } }

3.存入redis

public void start2() {
        Gson gson = new Gson();
        JobEntity jobEntity = new JobEntity();
        jobEntity.setMethodName("test");
        jobEntity.setJobName("MyJob2");
        jobEntity.setClassName("zebra.shjf.schedule.Test2");
        jobEntity.setCronExpression("0/1 * * * * ?");
        jobEntity.setJobGroup("MyGroup2");

        JobEntity jobEntity2 = new JobEntity();
        jobEntity2.setMethodName("xun");
        jobEntity2.setJobName("MyJob");
        jobEntity2.setClassName("zebra.shjf.schedule.Test");
        jobEntity2.setCronExpression("0/1 * * * * ?");
        jobEntity2.setJobGroup("MyGroup");

        ArrayList<JobEntity> list = new ArrayList<JobEntity>();
        list.add(jobEntity);
        list.add(jobEntity2);
        jedis.set("jobEntity", gson.toJson(list));
    }
127.0.0.1:6379> get "jobEntity" "[{\"cronExpression\":\"0/1 * * * * ?\",\"jobGroup\":\"MyGroup2\",\"jobName\":\"MyJob2\",\"className\":\"zebra.shjf.schedule.Test2\",\"methodName\":\"test\"},{\"cronExpression\":\"0/1 * * * * ?\",\"jobGroup\":\"MyGroup\",\"jobName\":\"MyJob\",\"className\":\"zebra.shjf.schedule.Test\",\"methodName\":\"xun\"}]"

3.重點解析(註釋解釋)

   @Test
    public void start3() throws Exception {
        //準備新增從redis中得到的實體類,目的是遍歷,然後新增到定時容器中,去執行
        ArrayList<JobEntity>arrayList=new ArrayList<JobEntity>();

        Gson gson = new Gson();
        //從redis中得到json陣列物件
        String str = jedis.get("jobEntity");
        //json解析器
        JsonParser parser = new JsonParser();
        //解析出json元素,jsonElement物件中有一些方法判斷是物件還是陣列,各對應不同的處理
        JsonElement jsonElement = parser.parse(str);

        //如果是json陣列就轉換為jsonArray
        JsonArray jsonArray = null;
        if (jsonElement.isJsonArray()) {
            jsonArray = jsonElement.getAsJsonArray();
        }
        //遍歷
        Iterator it= jsonArray.iterator();
        while (it.hasNext()){
            JsonElement e = (JsonElement)it.next();
            //把獲得的陣列中每一個物件,重新新增到陣列中
            arrayList.add(gson.fromJson(e,JobEntity.class));
        }
        //容器
        Scheduler scheduler=null;
        //遍歷陣列
        for(JobEntity jobEntity:arrayList){
            //遍歷獲得每個job物件
            JobDetail jobDetail = JobBuilder.newJob(ScheduledTasks.class).withIdentity(jobEntity.getJobName(), jobEntity.getJobGroup()).//
                    usingJobData("className", jobEntity.getClassName())
                    .usingJobData("methodName", jobEntity.getMethodName()).build();
            //jobDetail.getJobDataMap().put("test", jobEntity);
            //為每個任務動態構建表示式
            CronScheduleBuilder cron = CronScheduleBuilder.cronSchedule(jobEntity.getCronExpression());
            //構建觸發器
            CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(jobEntity.getJobName(), jobEntity.getJobGroup()).withSchedule(cron).build();

            SchedulerFactory schedulerFactory = new StdSchedulerFactory();
            scheduler = schedulerFactory.getScheduler();
            scheduler.scheduleJob(jobDetail, trigger);
        }
        scheduler.start();

        Thread thread = new Thread();
        thread.sleep(10000);

    }