SpringBoot集成Quartz实现定时器
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
SpringBoot集成Quartz实现定时器的更多相关文章
- SpringBoot集成Quartz实现定时任务
1 需求 在我的前后端分离的实验室管理项目中,有一个功能是学生状态统计.我的设计是按天统计每种状态的比例.为了便于计算,在每天0点,系统需要将学生的状态重置,并插入一条数据作为一天的开始状态.另外,考 ...
- SpringBoot集成Quartz(解决@Autowired空指针Null问题即依赖注入的属性为null)
使用spring-boot作为基础框架,其理念为零配置文件,所有的配置都是基于注解和暴露bean的方式. Quartz的4个核心概念: 1.Job表示一个工作,要执行的具体内容.此接口中只有一个方法v ...
- Springboot集成Quartz
之前学习过spring的定时任务 :https://www.cnblogs.com/slimshady/p/10112515.html 本文主要学习记录下springboot使用quartz 1. ...
- springBoot集成 quartz动态定时任务
项目中需要用到定时任务,考虑了下java方面定时任务无非就三种: 用Java自带的timer类.稍微看了一下,可以实现大部分的指定频率的任务的调度(timer.schedule()),也可以实现关闭和 ...
- Spring Boot笔记(三) springboot 集成 Quartz 定时任务
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 1. 在 pom.xml 中 添加 Quartz 所需要 的 依赖 <!--定时器 quartz- ...
- springboot集成quartz定时任务课动态执行
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</ ...
- springboot集成quartz实现任务调度
quartz 概述 特点 强大的调度功能 灵活的应用方式 分布式和集群能力 用到的设计模式 Builder 模式 factory模式 组件模式 链式写法 体系结构 调度器 任务 触发器 架构图 spr ...
- springboot自带定时任务和集成quartz
1,springboot自带的定时任务 默认是单线程 有这个依赖就可以 <dependency> <groupId>org.springframework.boot</ ...
- SpringBoot整合Quartz定时任务(持久化到数据库)
背景 最近在做项目,项目中有个需求:需要使用定时任务,这个定时任务需要即时生效.查看Quartz官网之后发现:Quartz提供两种基本作业存储类型: RAMJobStore :RAM也就是内存,默认情 ...
随机推荐
- 11.2 uptime:显示系统的运行时间及负载
uptime命令可以输出当前系统时间.系统开机到现在的运行时间.目前有多少用户在线和系统平均负载等信息. [root@cs6 ~]# uptime 17:02:25 up 1:48, 3 user ...
- 【转】Spring_IOC学习
原文地址:http://github.thinkingbar.com/spring/ 一.XML文件语法的知识点 对于XML没有提示的话,在Eclipse中搜索XML catalog设置.对于XML文 ...
- (xxx) is not defined at HTMLInputElement.onblur(Day_27)
错误: 这个报错我当时是卡了很久,方法是肯定没有问题的,但js所有的事件都失效了. 解决方案: 1.检查js命名是否有误,若外部引用js文件,尽量使用全小写命名,遵守js命名规范. 2.若还不行,请将 ...
- Question&&Answer
1.使用Navicat连接Ubuntu上面的MySql数据库失败 解决办法:Navicat版本的问题,尝试换用更高版本的Navicat解决了问题(当时使用了Navicat Premium_11.2.7 ...
- [leetcode] 75. 分类颜色(常数空间且只扫描一次算法)
75. 分类颜色 我们直接按难度最高的要求做:你能想出一个仅使用常数空间的一趟扫描算法吗? 常数空间 只能扫描一趟.注意,是一趟,而不是O(n) 题中只会出现3个数字:0,1,2.换句话说,0肯定在最 ...
- Python+Selenium - Alert弹框
上面三种弹窗可以在浏览器的控制台做出效果,如下图 上面三种弹窗可以用alert方法处理 示例: #出现弹窗的操作xxxx# 切换al = driver.switch_to.alert# print(a ...
- anaconda同时集成Python2 和 Python3
参考帖子,亲测有效: 利用anaconda同时使用python2和python3的方法 注意:最后一步是再对应的python环境中输入:conda install anaconda
- CUDA数学库
CUDA数学库 高性能数学例程 CUDA数学库是经过行业验证的,高度准确的标准数学函数的集合.只需在源代码中添加" #include math.h",即可用于任何CUDA C或CU ...
- TensorRT原理图示
TensorRT原理图示 NVIDIA的核心 TensorRT是有助于在NVIDIA图形处理单元(GPU)的高性能推理一个C ++库.它旨在与TensorFlow,Caffe,PyTorch,MXNe ...
- 编写HSA内核
编写HSA内核 介绍 HSA提供类似于OpenCL的执行模型.指令由一组硬件线程并行执行.在某种程度上,这类似于 单指令多数据(SIMD)模型,但具有这样的便利:细粒度调度对于程序员而言是隐藏的,而不 ...