Quartz任务调度器

背景:
              近期项目中遇到跨区调拨商品的需求,比如A区和B区,需要判断A区或者B区某种sku是否需要从对方库调拨商品来补充货源,避免因缺失商品而出现订单延误,影响销售和对用户产生不良影响。


问题:
             数据量庞大,如果当查看的时候去获取数据,那么会严重影响系统的性能,甚至导致数据库和应用服务器无法响应。


解决方案:
            规定在某个时间点,最好是在晚上12点时系统自动获取需要调拨的数据,然后将数据存储到数据库中。晚上12点,用户访问量和系统的其它工作最少,这个时候启动一个定时线程来获取数据对系统的影响几乎没有。


采用技术:
         Quartz,Quartz是一个开源的作业调度框架,它完全由Java写成,并设计用于J2SE和J2EE应用中。它提供了巨大的灵活性而不牺牲简单性。可以使用这个框架来完成定时任务的调度。

使用步骤:
          1、编写业务处理模块。
                      该模块针对本需求,首先获取近期需要调度的sku列表,这是比较耗时的,因为需要查看近20天的
 进销存记录并统计发货数量。

                      其次判断发货量是否满足某种条件(条件为业务内容,条件准备比较复杂)来进行补货,当满足时,查看其它库区同种sku是否满足调拨条件。
                      再次记录两库的日均发货量。
当上述条件满足时,生成数据,如果不满足则跳过,进行下一个sku判断,直到所有sku处理完毕。
 
 Demo:
 业务处理模块:

1
2
3
4
5
6
7
8
package com.tgb.test;
 
public class BusinessJob {
 
    public void generateBusinessInfo(){
        System.out.println("业务数据产生完毕!");
    }
}


定时任务模块:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.tgb.test;
 
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
public class TimeJob implements Job {   
     
    //重写方法
    public void execute(JobExecutionContext context)throws JobExecutionException { 
        BusinessJob businessJob = new BusinessJob(); 
        businessJob.generateBusinessInfo();
    
}


定时调度模块:

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.tgb.test;
 
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;
 
public class JobSchedule {
 
    private static Scheduler scheduler = null;
    private static final Object lock = new Object();
     
     
    public static Scheduler getSchedulerInstance(){
        if(scheduler == null){
            synchronized(lock){
                if(scheduler == null){
                    try {
                        scheduler = StdSchedulerFactory.getDefaultScheduler();
                    catch (SchedulerException e) {
                        return null;
                    }
                }
            }
        }
        return scheduler;
    }
     
    /**
     * 添加任务
     * @param jobName任务名称
     * @param jobGroup任务组名称
     * @param jobClass任务类对象
     * @param cronExpress定时调度表达式
     */
    public void addJob(String jobName, String jobGroup, Class<!--?--> jobClass, String cronExpress){
         
        try{
            JobDetail jobDetail = new JobDetail(jobName, jobGroup, jobClass); 
            //创建触发器对象,并为它设置名称,组名称,及任务调度的时间参数 
            CronTrigger cronTrigger = new CronTrigger("trigger1""triggerGroup"); 
             
            cronTrigger.setCronExpression(cronExpress);
             //配置JobDetail和Trigger对象 
            JobSchedule.getSchedulerInstance().scheduleJob(jobDetail, cronTrigger);
        }catch(Exception e){
            throw new RuntimeException(e);
        }
    }
     
    /**
     * 启动调度器
     */
    public void startScheduler(){
        try{
            if(!JobSchedule.getSchedulerInstance().isStarted()){
                JobSchedule.getSchedulerInstance().start();
            }
        }catch(Exception e){
            throw new RuntimeException(e);
        }
    }
     
     
    /**
     * 停止调度器
     */
    public void stopScheduler(){
        try{
            if(JobSchedule.getSchedulerInstance().isStarted()){
                JobSchedule.getSchedulerInstance().shutdown(true);
            }
        }catch(Exception e){
            throw new RuntimeException(e);
        }
    }
}

主线程入口:

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
package com.tgb.test;
 
 
public class TestMain {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
         
        //构造定时模块
        TimeJob timeJob = new TimeJob(); 
         
        //构造调度管理者
        JobSchedule jobSchedule = new JobSchedule();
         
        //添加job
        jobSchedule.addJob("jobName""jobGroup", timeJob.getClass(), "0 0 23 * * ?");
         
        //启动调度器
        jobSchedule.startScheduler();
         
        //关闭调度器
//      jobSchedule.stopScheduler();
    }
}


 

控制台输出:

Quartz任务调度器

原文地址:https://www.cnblogs.com/likeju/p/4784976.html