SpringBoot整合Quartz实现定时任务(单任务、多任务)

前言:为了实现如:定时发送邮件,订单超时等操作需要使用到任务调度,为了节约自己封装的时间所以使用Quatrz开源项目来帮助我们实现这些繁杂的操作,Quatrz内部有自己的线程池,不需要我们去手动维护线程,而且支持cron表达式等,下面我们讲解一下使用Springboot2.x来整合Quatrz

1,先是导入依赖,少了依赖可不行


	4.0.0
	com.itpengwei.diao
	boot-quartz
	0.0.1-SNAPSHOT
	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.4.RELEASE
		 
	
	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
			org.springframework
			spring-context-support
		
		
			org.quartz-scheduler
			quartz
		
		
		
			org.springframework
			spring-tx
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


第二步:创建任务类,这里我就创建两个来实现多任务,单任务也是同理

package com.itpengwei.config;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

@Component
@EnableScheduling
public class FirstJob {

	public void task() {
		System.out.println("任务1执行....当前时间为" + dateFormat().format(new Date()));
	}

	private SimpleDateFormat dateFormat() {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
		return simpleDateFormat;
	}
}
package com.itpengwei.config;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

@Component
@EnableScheduling
public class SecondJob {

	public void task() {
		System.out.println("任务2执行....当前时间为" + dateFormat().format(new Date()));
	}

	private SimpleDateFormat dateFormat() {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
		return simpleDateFormat;
	}
}

第三步:将任务进行配置,项目启动就会开始执行了

package com.itpengwei.config;

import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;

@Configuration
public class QuartzConfiguration {

	// 配置定时任务1
	@Bean(name = "firstJobDetail")
	public MethodInvokingJobDetailFactoryBean firstJobDetail(FirstJob firstJob) {
		MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
		// 是否并发执行
		jobDetail.setConcurrent(true);
		// 为需要执行的实体类对应的对象
		jobDetail.setTargetObject(firstJob);
		// 需要执行的方法
		jobDetail.setTargetMethod("task");
		return jobDetail;
	}

	// 配置触发器1
	@Bean(name = "firstTrigger")
	public SimpleTriggerFactoryBean firstTrigger(JobDetail firstJobDetail) {
		SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
		trigger.setJobDetail(firstJobDetail);
		// 设置任务启动延迟
		trigger.setStartDelay(0);
		// 每10秒执行一次
		trigger.setRepeatInterval(10000);
		return trigger;
	}

	// 配置定时任务2
	@Bean(name = "secondJobDetail")
	public MethodInvokingJobDetailFactoryBean secondJobDetail(SecondJob secondJob) {
		MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
		// 是否并发执行,假如设置为10秒一次,如果上一次因为到了时间没有执行那么当前这个任务会并发执行,
		// 如果为false的话就会等上一次执行完才执行
		jobDetail.setConcurrent(true);
		// 为需要执行的实体类对应的对象
		jobDetail.setTargetObject(secondJob);
		// 需要执行的方法
		jobDetail.setTargetMethod("task");
		return jobDetail;
	}

	// 配置触发器2
	@Bean(name = "secondTrigger")
	public CronTriggerFactoryBean secondTrigger(JobDetail secondJobDetail) {
		CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
		trigger.setJobDetail(secondJobDetail);
		// cron表达式,每过10秒执行一次
		trigger.setCronExpression("0/10 * * * * ?");
		return trigger;
	}

	// 配置Scheduler
	@Bean(name = "scheduler")
	public SchedulerFactoryBean schedulerFactory(Trigger firstTrigger, Trigger secondTrigger) {
		SchedulerFactoryBean bean = new SchedulerFactoryBean();
		// 延时启动,应用启动5秒后
		bean.setStartupDelay(5);
		// 注册触发器
		bean.setTriggers(firstTrigger, secondTrigger);
		return bean;
	}
}

测试结果:

SpringBoot整合Quartz实现定时任务(单任务、多任务)_第1张图片

你可能感兴趣的:(springboot,Quartz)