打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
SpringBoot整合Quartz

本次使用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 ...}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

2.任务类

public class Test {    public void xun(){        System.out.println("--------定时任务2-------");    }}public class Test2 {    public void test(){        System.out.println("--------定时任务1-------");    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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\"}]"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

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);    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
课堂笔记第七周
[转]用GSON 五招之内搞定任何JSON数组
java创建json数据,构建json数据
java json不生成null或者空字符串属性
Java入门自学视频
十年架构师留下最完整的Java学习路线,学完年薪65W
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服