spring3.0以后,自带的 schedule定时任务器
第三方quartz定时任务框架
添加schedule的坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
编写定时任务类
@Component
public class ScheduleDemo {
/**
* 定时任务方法
* @Scheduled 设置定时任务
* cron属性:cron表达式,定时任务触发时间的一个字符串表达式
*/
@Scheduled(cron = "0/2 * * * * ?")
public void scheduleMethod() {
System.out.println("定时器被触发" + LocalDateTime.now());
}
}
在启动类上开启定时任务
@EnableScheduling
public class Application {}
cron表达式是字符串,6个域或7个域
S M H D M W Y
一般通用是6个域
java.lang.IllegalStateException: Encountered invalid @Scheduled method 'scheduleMethod': Range exceeds maximum (60)
*:通配符
?: 占位符
-:范围
,: 取值列表
/: 从几开始,step
日和星期可能有冲突,要舍弃
springboot整合quartz定时任务框架
Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的程序。Jobs可以做成标准的Java组件或 EJBs。Quartz的最新版本为Quartz 2.3.0。
Quartz是一个完全由java编写的开源作业调度框架。
Quartz的使用思路
job:任务:要做什么事
Trigger:触发器:什么时候去做
Scheduler:任务调度:什么时候做什么事
quartz坐标
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
创建job类
/**
* @Description: 定义任务类
* @Author:
* @CreateDate: 2019/5/19 18:01
*/
public class QuartzDemo implements Job {
/**
* 任务被触发时所执行的方法
* @param jobExecutionContext
* @throws JobExecutionException
*/
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("Execute....."+ LocalDateTime.now());
}
}
编写测试代码
/**
* @Description:
* @Author: yuanyanan
* @CreateDate: 2019/5/19 18:05
*/
public class QuartzMain {
public static void main(String[] args) throws Exception {
//1.创建job对象
JobDetail job = JobBuilder.newJob(QuartzDemo.class).build();
/**
* 简单的trigger触发时间,通过quartz提供一个方法完成简单的重复调用
* cron Trigger,按照表达式制定触发时间
*/
//2.创建Trigger对象
//Trigger trigger = TriggerBuilder.newTrigger().withSchedule(SimpleScheduleBuilder.repeatSecondlyForever()).build();
Trigger trigger = TriggerBuilder.newTrigger().withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?")).build();
//3.创建Scheduler对象
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.scheduleJob(job, trigger);
//4.启动
scheduler.start();
}
}
Springboot整合quartz
quartz坐标
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
添加schedule的坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
spring事务
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
编写Job任务类
/**
* @Description: 任务类
* @Author:
* @CreateDate: 2019/5/19 18:29
*/
public class QuartzDemo1 implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("Execute..."+ LocalDateTime.now());
}
}
编写配置类
/**
* @Description: quartz配置类
* @Author:
* @CreateDate: 2019/5/19 18:30
*/
@Configuration
public class QuartzConfig {
//1.创建job对象
@Bean
public JobDetailFactoryBean jobDetailFactoryBean() {
JobDetailFactoryBean factory = new JobDetailFactoryBean();
//关联我们自己的job类
factory.setJobClass(QuartzDemo1.class);
return factory;
}
//2.创建Trigger对象
//创建简单的Trigger
@Bean
public SimpleTriggerFactoryBean simpleTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean) {
SimpleTriggerFactoryBean factory = new SimpleTriggerFactoryBean();
//关联JobDetail对象
factory.setJobDetail(jobDetailFactoryBean.getObject());
//该参数表示一个执行的毫秒数
factory.setRepeatInterval(2000);
//设置重复次数
factory.setRepeatCount(5);
return factory;
}
//3.创建Scheduler对象
@Bean
public SchedulerFactoryBean schedulerFactoryBean(SimpleTriggerFactoryBean simpleTriggerFactoryBean) {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
//关联Trigger
factory.setTriggers(simpleTriggerFactoryBean.getObject());
return factory;
}
}
修改启动类
加入@Scheduling注解
//创建Cron Trigger
@Bean
public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean) {
CronTriggerFactoryBean factory = new CronTriggerFactoryBean();
factory.setJobDetail(jobDetailFactoryBean.getObject());
//设置触发时间
factory.setCronExpression("0/2 * * * * ?");
return factory;
}
在job类中注入对象,注入时会产生异常:org.quartz.SchedulerException: Job threw an unhandled exception.
factory.setJobClass(QuartzDemo1.class);
QuartzDemo1类不是通过spring容器实例化,而是通过下面方法
AdaptableJobFactory.createJobInstance(),通过反射实例化
编写MyAdaptableJobFactory解决报错问题
/**
* @Description:
* @Author:
* @CreateDate: 2019/5/19 20:43
*/
@Component
public class MyAdaptableJobFactory extends AdaptableJobFactory {
//AutowireCapableBeanFactory可以将一个对象添加到spring容器中,并且完成该对象注入
@Autowired
private AutowireCapableBeanFactory autowireCapableBeanFactory;
/**
* 该方法需要将实例化的任务对象手动的添加到springIOC容器中并完成对象的注入
* @param bundle
* @return
* @throws Exception
*/
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Object obj = super.createJobInstance(bundle);
//将obj对象添加springIOC容器中,并完成注入
autowireCapableBeanFactory.autowireBean(obj);
return obj;
}
}
修改QuartzConfig类
//3.创建Scheduler对象
@Bean
public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean,MyAdaptableJobFactory myAdaptableJobFactory) {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
//关联Trigger
factory.setTriggers(cronTriggerFactoryBean.getObject());
factory.setJobFactory(myAdaptableJobFactory);
return factory;
}