定时调度

  时间:2019-02-11
本文章向大家介绍定时调度,主要包括定时调度使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

https://www.cnblogs.com/dawnheaven/p/5301482.html

https://www.jianshu.com/p/d732707ff194

解释:在规定的时间执行相关的程序,比如在距离活动结束前10分钟发送一个短信,定时调度就是在这个时间点调度发送短信的任务。

例子:

controller:

(可以写在service中,我写在controller了)

 @Autowired
    private Scheduler scheduler;



/**
     * 测试定时调度
     * @param req
     * @return
     */
    @RequestMapping(value = "/test",method = RequestMethod.POST)
    public ResponseBase test(@Valid @RequestBody PushReq req){
        System.out.println("---"+req.getEndTime());
        try {
            if(req.getEndTime()!=null){
                PushTask testTask = new PushTask(PushTaskName.PUSH_TASK_NAME.name(),req.getOrderNo());
                Trigger trigger = scheduler.getTrigger(testTask.getTriggerKey());
                if (trigger != null) {
                    scheduler.deleteJob(trigger.getJobKey());
                }
                pushService.testJob(scheduler,testTask,req.getEndTime());

            }
            return ResponseBase.succeeded();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBase.failed(ApiCode.INTERNAL_SERVER_ERROR, e.getMessage());
        }
    }

basetask: (是一个基础的,方便其他task的实现)

public class BaseTask {
    String group;
    String jobName;
    String triggerName;

    private JobKey jobKey;
    private TriggerKey triggerKey;

    public BaseTask(){

    }

    public BaseTask(String group, String taskName, String id) {
        this.group          = group;
        this.jobName        = taskName + "_JOB_" + id;
        this.triggerName    = taskName + "_TRIGGER_" + id;

        this.jobKey         = new JobKey(jobName, group);
        this.triggerKey     = new TriggerKey(triggerName, group);
    }

    public String getGroup() {
        return group;
    }

    public String getJobName() {
        return jobName;
    }

    public void setJobName(String jobName) {
        this.jobName = jobName;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public String getTriggerName() {
        return triggerName;
    }

    public void setTriggerName(String triggerName) {
        this.triggerName = triggerName;
    }


    public JobKey getJobKey() {
        return jobKey;
    }

    public void setJobKey(JobKey jobKey) {
        this.jobKey = jobKey;
    }

    public TriggerKey getTriggerKey() {
        return triggerKey;
    }

    public void setTriggerKey(TriggerKey triggerKey) {
        this.triggerKey = triggerKey;
    }
}

 实现basetask的task

public class PushTask extends BaseTask{
    @NotBlank
    private String pushId;
    @NotBlank
    private Long orderno;
    public PushTask(String taskName, String id){
        super(PushGroup.PUSH_GROUP.name(),taskName,id);
        this.pushId = id;
    }

    public String getPushId() {
        return pushId;
    }

    public void setPushId(String pushId) {
        this.pushId = pushId;
    }

    public Long getOrderno() {
        return orderno;
    }

    public void setOrderno(Long orderno) {
        this.orderno = orderno;
    }
}

service:

@Override
    public void testJob(Scheduler scheduler, PushTask testTask, Date time) throws Exception {
        //用于如何开始和关闭一个定时任务以及如何运行一个运行任务
        /**
         * 1. 获取scheduler对象,此处是由参数传入
         * 2. 获取时间,此处由参数传入
         * 3. 定义一个job对象,绑定job类,即本次的TestJob[真正执行的任务并不是Job接口的实例,而是用反射的方式实例化的一个JobDetail实例 ]
         * 4. 定义一个触发器,startAt方法定义了任务应当开始的时间
         * 5. 将任务和tigger放入scheduler
         */
        logger.info("[TEAM] close team" + testTask);

        //3. job对象绑定job类
        JobDetail jobDetail = JobBuilder
                .newJob(TestJob.class)
                .withIdentity(testTask.getJobKey())
                .build();

        JobDataMap jobDataMap = jobDetail.getJobDataMap();

        jobDataMap.put("pintuanid_job", testTask.getOrderno());

        //4. 触发器
        Trigger trigger = TriggerBuilder
                .newTrigger()
                .withIdentity(testTask.getTriggerKey())
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2))
                .startAt(time)
                .build();

        //5. 将任务和tigger放入scheduler
        scheduler.scheduleJob(jobDetail,trigger);

        System.out.println("任务调度已经启动---"+time);
    }

 job:

package com.eyee.scheduler.job;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.Date;

public class TestJob implements Job {
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
        String result = (String)jobDataMap.get("pintuanid_job");
        System.out.println("===="+result);
        System.out.println("hello word-"+new Date());
    }
}

 

上一页 下一页

原文地址:http://www.manongjc.com/article/51778.html