Quartz实现记录

1、配置文件quartz.properties

#ID设置为自动获取,所有调度器实例中是唯一的
org.quartz.scheduler.instanceId=AUTO
#StdJDBCDelegate说明支持集群
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#quartz内部表的前缀
org.quartz.jobStore.tablePrefix=QRTZ_
#是否加入集群
org.quartz.jobStore.isClustered=true
#指定调度程序的主线程是否应该是守护线程
org.quartz.scheduler.makeSchedulerThreadDaemon=true
#ThreadPool实现的类名
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
#ThreadPool配置线程守护进程
org.quartz.threadPool.makeThreadsDaemons=true
#线程数量
org.quartz.threadPool.threadCount:20
#线程优先级
org.quartz.threadPool.threadPriority:5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true
#数据保存方式为持久化
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
#容许的最大作业延长时间
org.quartz.jobStore.misfireThreshold=60000
org.quartz.jobStore.clusterCheckinInterval=20000

2、控制器

@RestController
@RequestMapping("/quartz")
public class QuartzController {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    @Value("${quartz.cron:0 0 1 * * ?}")
    private String quartzCron;

    @ApiOperation("添加任务")
    @RequestMapping(value="/addTask", method = RequestMethod.PUT)
    public void addTask(){
	    // 示例
		String jobName = "testJob";
		String jobGroup = "testGroup";
		Class<? extends Job> jobClass = QuartzTestJob.class; // 这里QuartzTestJob是继承Job的任务类
        try {
            Scheduler scheduler = schedulerFactoryBean.getScheduler();
            JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
            JobDetail jobDetail = scheduler.getJobDetail(jobKey);
            if (jobDetail != null){
                // 已存在该任务了
            } else {
                // 创建新的任务
                jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName).build();
                CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(quartzCron);
                CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(jobName + "_trigger", jobGroup + "_trigger").withSchedule(cronScheduleBuilder).build();
                scheduler.scheduleJob(jobDetail, cronTrigger);
            }
        } catch (SchedulerException e){
            e.printStackTrace();
        }
    }

    @ApiOperation("删除任务")
    @RequestMapping(value="/deleteTask", method = RequestMethod.DELETE)
    public ServiceData<Object> deleteTask(){
	    // 示例
		String jobName = "testJob";
		String jobGroup = "testGroup";
		try {
            Scheduler scheduler = schedulerFactoryBean.getScheduler();
            JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
            scheduler.deleteJob(jobKey);
        } catch (SchedulerException e){
            e.printStackTrace();
        }
    }
}

3、任务类

 

@DisallowConcurrentExecution
@Component
public class QuartzTestJob implements Job {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        // 执行具体的任务
    }
}

  

posted @ 2020-11-12 17:32  晒太阳的兔子很忙  阅读(223)  评论(0编辑  收藏  举报