Spring之调度器(Scheduler)
Posted 随意了
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring之调度器(Scheduler)相关的知识,希望对你有一定的参考价值。
调度器的使用基本步骤
准备工作:
1.获得一个调度器工厂:SchedulerFactory
2.获得一个调度器:通过SchedulerFactory.getSchduler()获得
3.往调度器里面添加一些想要写入的信息:scheduler.getContext().put("ypDao", scan_trans_task_quartz_period);
创建任务:
1.创建JobDetail实例,绑定Job实现类(例如QuartJob.class实现了Job的类):
JobDetail job = JobBuilder.newJob(QuartzJob.class).withIdentity("job132121", "jgrou12412p1").build();// 定义调度触发规则
创建触发器:
1.创建Trigger: Trigger trigger = TriggerBuilder.newTrigger().withIdentity("simpleTrigger2132123","triggerGr423oup").withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(5)).startNow().build();
把作业和触发器注册到任务调度中
scheduler.scheduleJob(job, trigger);
启动调度
scheduler.start();
注意:
1.一个调度器里面可以有多个多个被调度的任务(一个被调度的任务=触发器加任务)
2.一个触发器可以只能触发一个任务(触发器和任务之间是一一对应的关系)。
代码:
实现调度功能的类():
package com.tudy.SpringScheduler.task; import javax.annotation.PostConstruct; import org.apache.log4j.Logger; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.SimpleScheduleBuilder; import org.quartz.Trigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class ProcessQuartz { private static final Logger LOGGER = Logger .getLogger(ProcessMsgNotPushedWithQuartz.class); //该注解只能读取项目根目录下名为Application.properties(名字只能是这个)的配置文件, //“scan.trans.task.quartz.period:10” 中的10代表默认值 @Value("${scan.trans.task.quartz.period:10}") private Integer scan_trans_task_quartz_period; @PostConstruct public void triggerRunner() throws SchedulerException, InterruptedException { // 通过schedulerFactory获取一个调度器 SchedulerFactory schedulerfactory = new StdSchedulerFactory(); Scheduler scheduler = null; try { // 通过schedulerFactory获取一个调度器 scheduler = schedulerfactory.getScheduler(); scheduler.getContext().put("ypDao", scan_trans_task_quartz_period); // 创建jobDetail实例,绑定Job实现类 // 指明job的名称,所在组的名称,以及绑定job类 JobDetail job = JobBuilder.newJob(QuartzJob.class) .withIdentity("job132121", "jgrou12412p1").build(); // 定义调度触发规则 // 使用simpleTrigger规则 Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("simpleTrigger2132123", "triggerGr423oup") .withSchedule(SimpleScheduleBuilder.repeatSecondlyForever( 5)) .startNow().build(); // 把作业和触发器注册到任务调度中 scheduler.scheduleJob(job, trigger); // 启动调度 scheduler.start(); } catch (Exception e) { LOGGER.error(e, e); } } }
任务类(Quartz.class)
package com.iflytek.study.SpringScheduler.task; import org.apache.log4j.Logger; import org.quartz.DisallowConcurrentExecution; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.SchedulerException; @DisallowConcurrentExecution public class QuartzJob implements Job { private static final Logger LOGGER = Logger.getLogger(QuartzJob.class); private final static int NOT_SEND = 0; private final static int SENT = 1; public void execute(JobExecutionContext context) throws JobExecutionException { try { System.out.println("this:" + this); int ypDao = (Integer) context.getScheduler().getContext().get("ypDao"); System.out.println("ypDao:" + ypDao); } catch (SchedulerException e) { e.printStackTrace(); } } }
以上是关于Spring之调度器(Scheduler)的主要内容,如果未能解决你的问题,请参考以下文章