SpringBoot集成Quartz实现定时器
2018-06-21 06:52:30来源:未知 阅读 ()
SpringBoot+Quartz实现定时器,
由于本人也是刚学习,不足之处请各位大神指正 ..
1.pom配置
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency>
2.注册[pring-boot启动完成事件监听,用于启动job任务
@Configuration public class SchedulerListener implements ApplicationListener<ContextRefreshedEvent> { @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参数设置
@Component public 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注入工具类 */ @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
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:Eclipse引入DTD文件
下一篇:hibernate系列之一
- springboot2配置JavaMelody与springMVC配置JavaMelody 2020-06-11
- SpringBoot 2.3 整合最新版 ShardingJdbc + Druid + MyBatis 2020-06-11
- 掌握SpringBoot-2.3的容器探针:实战篇 2020-06-11
- nacos~配置中心功能~springboot的支持 2020-06-10
- SpringBoot + Vue + ElementUI 实现后台管理系统模板 -- 后 2020-06-10
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash