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也就是内存,默认情 ...
随机推荐
- mysql基础之mysql主从架构
一.概念 主从多用于网站架构,因为主从的同步机制是异步的,数据的同步有一定延迟,也就是说有可能会造成数据的丢失,但是性能比较好,因此网站大多数用的是主从架构的数据库,读写分离必须基于主从架构来搭建 二 ...
- 问题解决: PythonStudy 环境搭建
环境搭建的时候遇到问题 参见帖子: http://www.xitongcheng.com/jiaocheng/dnrj_article_24923.html 虚拟机运行的时候会遇到 最近有用户发现在电 ...
- git/repo常用命令
Git作为广受欢迎的一款版本控制工具,它该如何通过命令行使用呢?本文为你揭晓浓缩精华精华版:git常用命令一览,含部分repo操作. 代码下载 repo init -- -->初始化需要下载的分 ...
- xshell 终端 中文乱码
我们在终端输入命令,中文显示乱码了. 解决方案:将xshell 传输采用的默认encode改为utf-8 解决:
- 性能调优必备:NIO的优化实现原理
前言 我们就从底层的网络 I/O 模型优化出发,再到内存拷贝优化和线程模型优化,深入分析下 Tomcat.Netty 等通信框架是如何通过优化 I/O 来提高系统性能的. 网络 I/O 模型优化 网络 ...
- nlp任务中的传统分词器和Bert系列伴生的新分词器tokenizers介绍
layout: blog title: Bert系列伴生的新分词器 date: 2020-04-29 09:31:52 tags: 5 categories: nlp mathjax: true ty ...
- 2021.5.22 noip模拟1
这场考试考得很烂 连暴力都没打好 只拿了25分,,,,,,,,好好总结 T1序列 A. 序列 题目描述 HZ每周一都要举行升旗仪式,国旗班会站成一整列整齐的向前行进. 郭神作为摄像师想要选取其中一段照 ...
- 无人驾驶汽车发展需要激光雷达和V2X技术
无人驾驶汽车发展需要激光雷达和V2X技术
- 视频处理单元Video Processing Unit
视频处理单元Video Processing Unit VPU处理全局视频处理,它包括时钟门.块复位线和电源域的管理. 缺少什么: •完全重置整个视频处理硬件块 •VPU时钟的缩放和设置 •总线时钟门 ...
- Relay张量集成
Relay张量集成 Introduction NVIDIA TensorRT是一个用于优化深度学习推理的库.这种集成将尽可能多地减轻从中继到TensorRT的算子,在NVIDIA GPU上提供性能提升 ...