1.task
package quartz.task;
import java.util.Timer;
import java.util.TimerTask;
public class EmailReportTask extends TimerTask {
// 每次执行过程中num的值都会发生变化,说明此事使用的是同一个类对象
private int num = 0;
@Override
public void run() {
System.out.println("num = " + num++);
/* try {
num = num/0; 25 1.5 4 5 6 7
} catch (Exception e) {
e.printStackTrace();
}*/
System.out.println(this);
}
public static void main(String[] args) throws InterruptedException {
Timer t = new Timer();
t.scheduleAtFixedRate(new EmailReportTask(), 100, 1000);
}
}
task某次任务中抛出异常,任务生命周期结束,不继续下一次执行。catch后则可继续执行。
2.springboot @Scheduled注解
任务类
package quartz.task;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ScheduleTask {
@Scheduled(initialDelay = 2000,fixedDelay = 5000)
public void scheduleTest() {
System.err.println("scheduleTest开始定时执行");
/* int i = 1/0;*/不影响下次执行。
}
}
启动类
@SpringBootApplication
@EnableScheduling// 该注解必须要加
public class QuartzStarter {
public static void main (String... args){
SpringApplication.run(QuartzStarter.class,args);
}
}
3.springboot整合quartz
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
<!--springboot 整合quartz-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
</dependencies>
方式一:
package quartz.task;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ScheduleTask {
public void scheduleTest() {
System.err.println("scheduleTest开始定时执行");
/* int i = 1/0;*/
}
}
package quartz.conf;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component;
@Component
public class MyAdaptableJobFactory extends AdaptableJobFactory {
// 可以将一个对象添加到SpringIOC 容器中,并且完成该对象注入
@Autowired
private AutowireCapableBeanFactory autowireCapableBeanFactory;
/**
* 该方法需要将实例化的任务对象手动的添加到springIOC 容器中并且完成对象的注入
*/
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Object obj = super.createJobInstance(bundle);
//将obj 对象添加Spring IOC 容器中,并完成注入
this.autowireCapableBeanFactory.autowireBean(obj);
return obj;
}
}
package quartz.conf;
import org.quartz.Trigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import quartz.task.ScheduleTask;
import quartz.task.ScheduledJob;
@Configuration
public class QuartzConfigration {
@Bean(name = "jobDetail")
public MethodInvokingJobDetailFactoryBean detailFactoryBean(ScheduleTask task) {
// ScheduleTask为需要执行的任务
MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
/* * 是否并发执行
* 例如每5s执行一次任务,但是当前任务还没有执行完,就已经过了5s了,
* 如果此处为true,则下一个任务会bing执行,如果此处为false,则下一个任务会等待上一个任务执行完后,再开始执行*/
jobDetail.setConcurrent(true);
jobDetail.setName("scheduler");// 设置任务的名字
jobDetail.setGroup("scheduler_group");// 设置任务的分组,这些属性都可以存储在数据库中,在多任务的时候使用
/** 这两行代码表示执行task对象中的scheduleTest方法。定时执行的逻辑都在scheduleTest。*/
jobDetail.setTargetObject(task);
jobDetail.setTargetMethod("scheduleTest");
return jobDetail;
}
@Bean(name = "jobTrigger")
public CronTriggerFactoryBean cronJobTrigger(MethodInvokingJobDetailFactoryBean jobDetail) {
CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
tigger.setJobDetail(jobDetail.getObject());
tigger.setCronExpression("0/6 * * * * ?");// 表示每隔6秒钟执行一次
//tigger.set
tigger.setName("myTigger");// trigger的name
return tigger;
}
/* @Bean(name = "scheduler")
public SchedulerFactoryBean schedulerFactory(Trigger cronJobTrigger) {
SchedulerFactoryBean bean = new SchedulerFactoryBean();
//设置是否任意一个已定义的Job会覆盖现在的Job。默认为false,即已定义的Job不会覆盖现有的Job。
bean.setOverwriteExistingJobs(true);
// 延时启动,应用启动5秒后 ,定时器才开始启动
bean.setStartupDelay(5);
// 注册定时触发器
bean.setTriggers(cronJobTrigger);
return bean;
}*/
@Bean(name = "scheduler")
public SchedulerFactoryBean schedulerFactoryBean(Trigger cronJobTrigger,MyAdaptableJobFactory jobFactory){
SchedulerFactoryBean factory = new SchedulerFactoryBean();
//关联trigger
factory.setStartupDelay(5);
factory.setTriggers(cronJobTrigger);
factory.setJobFactory(jobFactory);
return factory;
}
//多任务时的Scheduler,动态设置Trigger。一个SchedulerFactoryBean可能会有多个Trigger
/* @Bean(name = "multitaskScheduler")
public SchedulerFactoryBean schedulerFactoryBean(){
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
return schedulerFactoryBean;
}*/
}
quartz框架的基础组成为 job/jobdetai/trigger/schedule 其中job是任务类,jobdetail是job的实例化,可以对任务进行标识,给定任务名,任务分组。获取jobdetail主要有两种方式MethodInvokingJobDetailFactoryBean 和 JobDetailFactoryBean。使用区别:方式一中的MethodInvokingJobDetailFactoryBean 通过setTargetObject 和 setTargetMethod两个方法确认任务类,不需要实现Job
方式二:
package quartz.task;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import quartz.service.JobService;
@Component
public class ScheduledJob implements Job {
@Autowired
JobService jobService;
@Override
public void execute(JobExecutionContext context)
throws JobExecutionException {
// System.err.println("这是第一个任务 is running…………………………………… ");
jobService.scheduleTest();
}
/* public void scheduleTest(int c) {
System.err.println("scheduleTest开始定时执行");
}*/
public void scheduleTest() {
System.err.println("scheduleJOBTest开始定时执行");
}
}
package quartz.conf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import quartz.task.ScheduledJob;
public class QuartzConfigration2 {
@Autowired
private MyAdaptableJobFactory myJobFactory;
@Bean(name = "jobDetail")
public JobDetailFactoryBean simpleJobDetail() {
JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
factoryBean.setJobClass(ScheduledJob.class);
factoryBean.setGroup("group1");
factoryBean.setName("job1");
return factoryBean;
}
@Bean(name = "jobTrigger")
public CronTriggerFactoryBean cronJobTrigger() {
CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
tigger.setJobDetail(simpleJobDetail().getObject());
tigger.setCronExpression("0/6 * * * * ?");// 表示每隔6秒钟执行一次
//tigger.set
tigger.setName("myTigger");// trigger的name
return tigger;
}
@Bean(name = "scheduler")
public SchedulerFactoryBean schedulerFactory() {
SchedulerFactoryBean bean = new SchedulerFactoryBean();
//设置是否任意一个已定义的Job会覆盖现在的Job。默认为false,即已定义的Job不会覆盖现有的Job。
bean.setOverwriteExistingJobs(true);
// 延时启动,应用启动5秒后 ,定时器才开始启动
bean.setStartupDelay(5);
// 注册定时触发器
bean.setJobFactory(myJobFactory);
bean.setTriggers(cronJobTrigger().getObject());
return bean;
}
/* @Bean(name = "scheduler")
public SchedulerFactoryBean schedulerFactoryBean(Trigger cronJobTrigger,MyAdaptableJobFactory jobFactory){
SchedulerFactoryBean factory = new SchedulerFactoryBean();
//关联trigger
factory.setStartupDelay(5);
factory.setTriggers(cronJobTrigger);
factory.setJobFactory(jobFactory);
return factory;
}*/
//多任务时的Scheduler,动态设置Trigger。一个SchedulerFactoryBean可能会有多个Trigger
/* @Bean(name = "multitaskScheduler")
public SchedulerFactoryBean schedulerFactoryBean(){
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
return schedulerFactoryBean;
}*/
}
方式二 JobDetailFactoryBean 通过setJobClass指定任务类,需实现Job类,重写execute()