Java框架_SpringBoot整合Quartz

一、Quartz的介绍以及Quartz的使用思路

        Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的程序。Jobs可以做成标准的Java组件或 EJBs。
  • job - 任务 - 你要做什么事?
  • Trigger - 触发器 - 你什么时候去做?
  • Scheduler - 任务调度 - 你什么时候需要去做什么事?

二、Quartz的使用

1、首先要创建Job类
/**
 * 定义任务类
 */
public class QuartzDemo implements Job {
	/**
	 * 任务被触发时所执行的方法
	 */
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		System.out.println("Execute...."+new Date());
	}
}
2、编写测试代码
public class QuartzMain {
	public static void main(String[] args) throws Exception {
		// 1.创建Job对象:你要做什么事?
		JobDetail job = JobBuilder.newJob(QuartzDemo.class).build();
		/**
		 * 简单的trigger触发时间:通过Quartz提供一个方法来完成简单的重复调用 cron
		 * Trigger:按照Cron的表达式来给定触发的时间
		 */
		// 2.创建Trigger对象:在什么时间做?
		Trigger trigger = TriggerBuilder.newTrigger().withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?"))
				.build();

		// 3.创建Scheduler对象:在什么时间做什么事?
		Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
		scheduler.scheduleJob(job, trigger);
		
		//启动
		scheduler.start();
	}

}

三、springBoot 整合 QuartZ

1、修改pom.xml文件

	4.0.0
	com.lxp
	framework
	0.0.1-SNAPSHOT
	
	
	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.10.RELEASE
	
	
	
		1.7
		3.0.2.RELEASE
		2.0.4
	
	
	
	
	
		
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
        
            org.springframework.boot
            spring-boot-starter-test
        
        
        
        
	        org.springframework.boot
	        spring-boot-starter-thymeleaf
	    
		  
	    
        
	        org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.0
	    
	    
	    
		
			mysql
			mysql-connector-java
		
	    
	    
        
	        com.alibaba
            druid
            1.0.9
	    
    
    	
		
			org.springframework.boot
			spring-boot-starter-data-jpa
		
		
		
		
			org.springframework.boot
			spring-boot-starter-cache
		

		
		
			net.sf.ehcache
			ehcache
		
	    
	    
	    
			org.quartz-scheduler
			quartz
			2.2.1
			
				
					slf4j-api
					org.slf4j
				
			
		

		
		
	        org.springframework
	        spring-context-support
	    
	    
	    
	    
			org.springframework
			spring-tx
		
	    

    
	
		?
	
2、编写Quartz的启动类
/**
 * 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;
	}
	
	/**
	 * 3.创建Scheduler对象
	 */
	@Bean
	public SchedulerFactoryBean schedulerFactoryBean(SimpleTriggerFactoryBean simpleTriggerFactoryBean){
		SchedulerFactoryBean factory = new SchedulerFactoryBean();
		//关联trigger
		factory.setTriggers(simpleTriggerFactoryBean.getObject());
		
		return factory;
	}
}
3、修改启动类

/**
 * 
 *spring Boot 整合Quartz案例
 *
 */
@SpringBootApplication
@EnableScheduling
public class App {

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

四、Job类中注入对象

 使用@Autowired时异常

@Autowired
	private SysUsersService sysUsersService;

Java框架_SpringBoot整合Quartz_第1张图片

1、  编写一个MyAdaptableJobFactory解决该问题
@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;
	}

}
2、修改QuartzConfig类
/**
 * 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;
	}
}



你可能感兴趣的:(后端开发)