博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot集成Quartz实现定时器
阅读量:4974 次
发布时间:2019-06-12

本文共 3488 字,大约阅读时间需要 11 分钟。

SpringBoot+Quartz实现定时器,

由于本人也是刚学习,不足之处请各位大神指正 .. 
1.pom配置

org.springframework
spring-context-support
org.springframework
spring-tx
org.quartz-scheduler
quartz
2.2.1

  

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

 

@Configurationpublic class SchedulerListener implements ApplicationListener
{ @Autowired public MyScheduler myScheduler; @Override public void onApplicationEvent(ContextRefreshedEvent event) { try { myScheduler.scheduleJobs(); } catch (SchedulerException e) { e.printStackTrace(); } } @Bean public SchedulerFactoryBean schedulerFactoryBean(){ SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean(); return schedulerFactoryBean; }}

  

3、job参数设置

 

@Componentpublic class MyScheduler {    @Autowired    SchedulerFactoryBean schedulerFactoryBean;    public void scheduleJobs() throws SchedulerException {        Scheduler scheduler = schedulerFactoryBean.getScheduler();        startJob1(scheduler);         startJob2(scheduler);     }    private void startJob1(Scheduler scheduler) throws SchedulerException{        JobDetail jobDetail = JobBuilder.newJob(ScheduledJob.class) .withIdentity("job1", "group1").build();         CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");         CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1") .withSchedule(scheduleBuilder).build();         scheduler.scheduleJob(jobDetail,cronTrigger);     }     private void startJob2(Scheduler scheduler) throws SchedulerException{         JobDetail jobDetail = JobBuilder.newJob(ScheduledJob2.class) .withIdentity("job2", "group1").build();        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/10 * * * * ?");         CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger2", "group1") .withSchedule(scheduleBuilder).build();         scheduler.scheduleJob(jobDetail,cronTrigger);    }}

  

4.实现各个任务job

public class ScheduledJob implements Job{     private SimpleDateFormat dateFormat() {        return new SimpleDateFormat("HH:mm:ss");    }     @Override    public void execute(JobExecutionContext context) throws JobExecutionException {         System.out.println("AAAA: The time is now " + dateFormat().format(new Date()));     }}
这样会导致一个问题,就是执行定时器的时候 ,service不能注入 ! 解决方法: 通过外部来创建serivce ,然后在定时器里调用

 创建ApplicationContextUtil工具类

/** * 定时器service注入工具类 */@Componentpublic 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

转载于:https://www.cnblogs.com/xiyunjava/p/9197428.html

你可能感兴趣的文章
hive安装以及hive on spark
查看>>
勇者无畏
查看>>
12864点阵液晶显示模块的原理和实例程序(HJ12864M-1)
查看>>
jz1074 【基础】寻找2的幂
查看>>
Wannafly模拟赛5 A 思维 D 暴力
查看>>
Javascript获取select下拉框选中的的值
查看>>
【Linux开发】CCS远程调试ARM,AM4378
查看>>
springmvc常用注解标签详解
查看>>
Linux之ssh服务介绍
查看>>
Java Swing提供的文件选择对话框 - JFileChooser
查看>>
排序:冒泡排序
查看>>
github下载安装
查看>>
Hat’s Words
查看>>
Java中instanceof关键字的用法总结
查看>>
引用类型-Function类型
查看>>
Nginx Configuration 免费HTTPS加密证书
查看>>
(转)Android 仿订单出票效果 (附DEMO)
查看>>
数据库多张表导出到excel
查看>>
微信小程序去除button默认样式
查看>>
11/26
查看>>