SpringBoot使用Quartz定时执行任务

pom.xml


    		
    		
    			org.springframework.boot
    			spring-boot-starter-web
    		
    		
    		
    			org.springframework.boot
    			spring-boot-starter-thymeleaf
    		
    
    		
    		
    			org.quartz-scheduler
    			quartz
    			2.2.1
    			
    				
    					slf4j-api
    					org.slf4j
    				
    			
    		
    
    		
    		
    			org.springframework
    			spring-context-support
    		
    
    		
    		
    			org.springframework
    			spring-tx
    		
    	

启动类
注意添加@EnableScheduling

@SpringBootApplication
@EnableScheduling
public class App {

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

需要定时执行的方法写execute方法里

public class QuartzDemo implements Job {
	
	@Autowired
	private UsersService usersService;
	
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
	
		System.out.println("Execute...."+new Date());
		this.usersService.addUsers();
		
	}

}

service层

@Service
public class UsersService {

	public void addUsers() {
		System.out.println("Add Users......");
	}

}

Quartz配置类

@Configuration
public class QuartzConfig {
	/**
	 * 1.创建Job对象
	 */
	@Bean
	public JobDetailFactoryBean jobDetailFactoryBean(){
		JobDetailFactoryBean factory = new JobDetailFactoryBean();
		//关联我们自己的Job类
		factory.setJobClass(QuartzDemo.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;
	}*/
	
	/**
	 * Cron Trigger
	 */
	@Bean
	public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
		CronTriggerFactoryBean factory = new CronTriggerFactoryBean();
		factory.setJobDetail(jobDetailFactoryBean.getObject());
		//设置触发时间
		factory.setCronExpression("0/2 * * * * ?");
		return factory;
	}
	
	/**
	 * 3.创建Scheduler对象
	 */
	@Bean
	public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean,MyAdaptableJobFactory myAdaptableJobFactory){
		SchedulerFactoryBean factory = new SchedulerFactoryBean();
		//关联trigger
		factory.setTriggers(cronTriggerFactoryBean.getObject());
		factory.setJobFactory(myAdaptableJobFactory);
		return factory;
	}
}

重写AdaptableJobFactory中的createJobInstance()方法

@Component("myAdaptableJobFactory")
public class MyAdaptableJobFactory extends AdaptableJobFactory  {
	
	//AutowireCapableBeanFactory 可以将一个对象添加到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;
	}

}

目录结构:
SpringBoot使用Quartz定时执行任务_第1张图片

你可能感兴趣的:(SpringBoot使用Quartz定时执行任务)