定时任务框架-quartz

孤街浪徒 提交于 2020-02-02 08:00:43

依赖

 

                <!-- 定时任务jar -->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.1.7</version>
        </dependency>            

 

任务调度类

 

 1 /**
 2  * 
 3  * @作者 陈祥
 4  * @创建时间 2018年5月29日 
 5  * @功能描述 定时任务, 分配结算大小B 业务方法实现
 6  */
 7 public class DistributeSettlementDateJobService  {
 8     
 9     //非大B分销商佣金结算 时间  每个月 20 号3点 触发
10     private final String DATE_DISTRI = "0 0 3 20 * ?";
11     // 大B  每天整点30分触发
12     private final String DATE_B_DISTRI = "0 30 * * * ?";
13     // 检查订单过期  每分钟触发
14     private final String DATE_ORDER =  "30 * * * * ?";
15     // 自动好评, 每天 4点触发
16     private final String DATE_OrderEvaluate =  "0 0 4 * * ?";
17     // 自动收货, 每天 2点触发
18     private final String DATE_OrderTake =  "0 0 3 * * ?";
19     
20     public void DistributeSettlement() throws Exception {
21 
22         SchedulerFactory sf = new StdSchedulerFactory();
23         Scheduler sched = sf.getScheduler();
24 
25         //结算小B 和 其他
26         JobDetail job = newJob(DistributeSettlementDateJobAll.class).withIdentity("job1", "group1").build();
27         Trigger trigger = newTrigger().withIdentity("trigger1", "group1").withSchedule(cronSchedule(DATE_DISTRI)).build();
28         sched.scheduleJob(job, trigger);
29         
30         //检查订单是否过期任务
31         job = newJob(OrderDateJob.class).withIdentity("job2", "group1").build();
32         trigger = newTrigger().withIdentity("trigger2", "group1").withSchedule(cronSchedule(DATE_ORDER)).build();
33         sched.scheduleJob(job, trigger);
34         
35         //结算大B
36         job = newJob(DistributeSettlementDateJobFoB.class).withIdentity("job3", "group1").build();
37         trigger = newTrigger().withIdentity("trigger3", "group1").withSchedule(cronSchedule(DATE_B_DISTRI)).build();
38         sched.scheduleJob(job, trigger);
39 
40         // 自动好评
41         job = newJob(OrderEvaluateDateJob.class).withIdentity("job4", "group1").build();
42         trigger = newTrigger().withIdentity("trigger4", "group1").withSchedule(cronSchedule(DATE_OrderEvaluate)).build();
43         sched.scheduleJob(job, trigger);
44         
45         // 自动收货
46         job = newJob(OrderTakeDateJob.class).withIdentity("job5", "group1").build();
47         trigger = newTrigger().withIdentity("trigger5", "group1").withSchedule(cronSchedule(DATE_OrderTake)).build();
48         sched.scheduleJob(job, trigger);
49         
50         
51         
52         
53         
54         
55         
56         
57         sched.start();
58     }
59     
60 }

 

任务实现类,实现 job 接口, 重写 execute() 方法即可

 

 1 package eidolon.time.service;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Date;
 5 import java.util.HashMap;
 6 import java.util.Map;
 7 
 8 import org.quartz.Job;
 9 import org.quartz.JobExecutionContext;
10 import org.quartz.JobExecutionException;
11 
12 import bingosoft.metro.model.OrderModel;
13 import leap.orm.dao.Dao;
14 import utils.redis.RedisUtil;
15 
16 /**
17  * 
18  * @作者 陈祥
19  * @创建时间 2018年5月31日
20  * @功能描述 定时检查是否有订单过期
21  */
22 public class OrderDateJob implements Job {
23 
24     private final String mapName = "orderTime";
25 
26     @Override
27     public void execute(JobExecutionContext arg0) throws JobExecutionException {
28 
29         RedisUtil redisUtil = RedisUtil.getRedisUtil();
30         Map<String, String> mapAll = redisUtil.getMapAll(mapName);
31         if (null != mapAll && mapAll.size() > 1) {
32 
33             ArrayList<String> ids = new ArrayList<>();
34             // 当前时间
35             long newTime = new Date().getTime();
36             for (String key : mapAll.keySet()) {
37                 Long lowTime = Long.valueOf(mapAll.get(key));
38                 if (newTime > lowTime) {
39 
40                     ids.add(key);
41                     System.err.println("删除过期:" + key);
42                 }
43             }
44             if(ids  !=  null &&  ids.size() > 0 ){
45                 HashMap<String, Object> params = new HashMap<>();
46                 params.put("orderId", ids);
47                 Dao.get().doTransaction((s) -> {
48                     // 数据库修改状态
49                     OrderModel.dao().executeNamedUpdate("orderDateJob-updateOrder", params);
50                     // 删除缓存
51                     for (String id : ids) {
52                         redisUtil.delKeyAndValueForMap(mapName, id);
53                     }
54 
55                 });
56             }
57         } else {
58             System.err.println("没有过期");
59         }
60 
61     }
62 }

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!