SpringBoot集成Quartz实现定时器

SpringBoot+Quartz实现定时器,
由于本人也是刚学习,不足之处请各位大神指正 .. 

1.pom配置

  1.  
    <dependency>
  2.  
    <groupId>org.springframework</groupId>
  3.  
    <artifactId>spring-context-support</artifactId>
  4.  
    </dependency>
  5.  
    <dependency>
  6.  
    <groupId>org.springframework</groupId>
  7.  
    <artifactId>spring-tx</artifactId>
  8.  
    </dependency>
  9.  
    <dependency>
  10.  
    <groupId>org.quartz-scheduler</groupId>
  11.  
    <artifactId>quartz</artifactId>
  12.  
    <version>2.2.1</version>
  13.  
    </dependency>
  14.  
     
  15.  
     
  16.  
     

2.注册[pring-boot启动完成事件监听,用于启动job任务

 

  1.  
    @Configuration
  2.  
    public class SchedulerListener implements ApplicationListener<ContextRefreshedEvent> {
  3.  
    @Autowired
  4.  
    public MyScheduler myScheduler;
  5.  
    @Override
  6.  
    public void onApplicationEvent(ContextRefreshedEvent event) {
  7.  
    try {
  8.  
    myScheduler.scheduleJobs();
  9.  
    } catch (SchedulerException e) {
  10.  
    e.printStackTrace();
  11.  
    }
  12.  
    }
  13.  
    @Bean
  14.  
    public SchedulerFactoryBean schedulerFactoryBean(){
  15.  
    SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
  16.  
    return schedulerFactoryBean;
  17.  
    }
  18.  
    }
  19.  
     
  20.  
     

3、job参数设置

 

  1.  
    @Component
  2.  
    public class MyScheduler {
  3.  
    @Autowired
  4.  
    SchedulerFactoryBean schedulerFactoryBean;
  5.  
    public void scheduleJobs() throws SchedulerException {
  6.  
    Scheduler scheduler = schedulerFactoryBean.getScheduler();
  7.  
    startJob1(scheduler);
  8.  
    startJob2(scheduler);
  9.  
    }
  10.  
    private void startJob1(Scheduler scheduler) throws SchedulerException{
  11.  
    JobDetail jobDetail = JobBuilder.newJob(ScheduledJob.class) .withIdentity("job1", "group1").build();
  12.  
    CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
  13.  
    CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1") .withSchedule(scheduleBuilder).build();
  14.  
    scheduler.scheduleJob(jobDetail,cronTrigger);
  15.  
    }
  16.  
    private void startJob2(Scheduler scheduler) throws SchedulerException{
  17.  
    JobDetail jobDetail = JobBuilder.newJob(ScheduledJob2.class) .withIdentity("job2", "group1").build();
  18.  
    CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/10 * * * * ?");
  19.  
    CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger2", "group1") .withSchedule(scheduleBuilder).build();
  20.  
    scheduler.scheduleJob(jobDetail,cronTrigger);
  21.  
    }
  22.  
    }
  23.  
     
  24.  
     

4.实现各个任务job

  1.  
    public class ScheduledJob implements Job{
  2.  
    private SimpleDateFormat dateFormat() {
  3.  
    return new SimpleDateFormat("HH:mm:ss");
  4.  
    }
  5.  
    @Override
  6.  
    public void execute(JobExecutionContext context) throws JobExecutionException {
  7.  
    System.out.println("AAAA: The time is now " + dateFormat().format(new Date()));
  8.  
    }
  9.  
    }
  10.  
     
  11.  
    这样会导致一个问题,就是执行定时器的时候 ,service不能注入 !
  12.  
     
  13.  
    解决方法: 通过外部来创建serivce ,然后在定时器里调用
  14.  
     
  15.  
     
创建ApplicationContextUtil工具类
/**
 * 定时器service注入工具类
 */
@Component
public class ApplicationContextUtil implements ApplicationContextAware{

    private static ApplicationContext applicationContext;

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }


    public  void setApplicationContext(ApplicationContext applicationContext) {
        ApplicationContextUtil.applicationContext = applicationContext;
    }

    public static Object getBean(String BeanName){
        return applicationContext.getBean(BeanName);
    }
}


2. 在定时器里调用工具类来创建service
   BankService bankService=(BankService) ApplicationContextUtil.getBean("BankService");
   Double money=bankService.getBalance("62279205947481841");

   这样就完美的解决了service注入空指针异常的问题 
转自:https://blog.csdn.net/dreamer_ax/article/details/72282392
posted @ 2018-08-13 10:07  Graffiti  阅读(281)  评论(0编辑  收藏  举报