springboot整合Quartz实现定时任务
1.maven依赖:
<!--quartz-->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.</version>
</dependency>
<!-- 该依赖必加,里面有sping对schedule的支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
2.定时任务配置类
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean; /**
* 定时任务配置
*
* @author yangfeng
* @date 2018-07-03
*/
@Configuration
public class QuartzConfig {
@Autowired
private SpringJobFactory springJobFactory; @Bean
public SchedulerFactoryBean schedulerFactoryBean() {
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setJobFactory(springJobFactory);
return schedulerFactoryBean;
} @Bean
public Scheduler scheduler() {
return schedulerFactoryBean().getScheduler();
}
}
3.配置job交给spring管理,主要目的就是解决job类 注入其他service或者使用Spring组件
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component; @Component
public class SpringJobFactory extends AdaptableJobFactory { @Autowired
private AutowireCapableBeanFactory capableBeanFactory; @Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Object jobInstance = super.createJobInstance(bundle);
capableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
4.定时任务创建job,通过注入Scheduler对任务进行操作
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; /**
* 定时任务服务
*
* @author yangfeng
* @date 2018-07-03
*/
@Service
public class QuartzService { private static final String JOB_GROUP = "event_job_group";
private static final String TRIGGER_GROUP = "event_trigger_group";
@Autowired
private Scheduler scheduler; /**
* 创建定时任务
* @param jobDetailName
* @param cronExpression
* @param jobClass
* @throws SchedulerException
*/
public void createScheduleJob(String jobDetailName, String cronExpression, Class<? extends Job> jobClass) throws SchedulerException {
JobDetail jobDetail = JobBuilder.newJob(jobClass)
.withIdentity("task_" + jobDetailName, JOB_GROUP).storeDurably().requestRecovery().build();
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("task_" + jobDetailName, TRIGGER_GROUP).withSchedule(scheduleBuilder).build();
scheduler.scheduleJob(jobDetail, trigger);
}
}
5.任务执行类,具体业务逻辑
import com.jp.zpzc.service.IUserVoteService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import javax.annotation.Resource; /**
* 投票统计
*
* @author yangfeng
* @date 2018-07-03
*/
public class VoteStatisticsTask implements Job { private static Logger LOG = LoggerFactory.getLogger(VoteStatisticsTask.class); @Resource
private IUserVoteService userVoteService; @Override
public void execute(JobExecutionContext jobExecutionContext) {
//统计已结束的投票
userVoteService.updateVote();
}
}
6.启动监听去初始化Quartz
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent; /**
* 监听器,启动定时任务
*
* @author yangfeng
* @date 2018-07-03
*/
@Configuration
public class ApplicationStartQuartzJobListener implements ApplicationListener<ContextRefreshedEvent> {
private static Logger LOG = LoggerFactory.getLogger(ApplicationStartQuartzJobListener.class); @Autowired
private QuartzService quartzService; /**
* 初始启动quartz
*/
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
try {
//成分分析
//quartzService.createScheduleJob("IngredientAnalysisTask", "*/5 * * * * ?", IngredientAnalysisTask.class);
//投票统计
quartzService.createScheduleJob("VoteStatisticsTask", "*/5 * * * * ?", VoteStatisticsTask.class);
LOG.info("任务已经启动...");
} catch (SchedulerException e) {
LOG.error(e.getMessage());
}
}
}
0k
springboot整合Quartz实现定时任务的更多相关文章
- 【spring-boot】 springboot整合quartz实现定时任务
在做项目时有时候会有定时器任务的功能,比如某某时间应该做什么,多少秒应该怎么样之类的. spring支持多种定时任务的实现.我们来介绍下使用spring的定时器和使用quartz定时器 1.我们使用s ...
- SpringBoot整合Quartz定时任务 系统job Spring Boot教程 调度任务
原文地址:https://www.cnblogs.com/allalongx/p/8477368.html 构建工程 创建一个Springboot工程,在它的程序入口加上@EnableScheduli ...
- SpringBoot整合Quartz定时任务
记录一个SpringBoot 整合 Quartz 的Demo实例 POM.XML文件 <!-- 定时器任务 quartz需要导入的坐标 --> <dependency> < ...
- SpringBoot整合Quartz及log4j实例
SpringBoot整合Quartz及log4j实例 因为之前项目中经常会做一些定时Job的东西,所以在此记录一下,目前项目中已经使用elastic-job,这个能相对比Quartz更加简单方便一些, ...
- SpringBoot整合Quartz定时任务(持久化到数据库)
背景 最近在做项目,项目中有个需求:需要使用定时任务,这个定时任务需要即时生效.查看Quartz官网之后发现:Quartz提供两种基本作业存储类型: RAMJobStore :RAM也就是内存,默认情 ...
- SpringBoot整合Quartz作为调度中心完整实用例子
因为想要做一个类似于调度中心的东西,定时执行一些Job(通常是一些自定义程序或者可执行的jar包),搭了一个例子,总结了前辈们的相关经验和自己的一些理解,如有雷同或不当之处,望各位大佬见谅和帮忙指正. ...
- springBoot集成 quartz动态定时任务
项目中需要用到定时任务,考虑了下java方面定时任务无非就三种: 用Java自带的timer类.稍微看了一下,可以实现大部分的指定频率的任务的调度(timer.schedule()),也可以实现关闭和 ...
- SpringBoot集成Quartz实现定时任务
1 需求 在我的前后端分离的实验室管理项目中,有一个功能是学生状态统计.我的设计是按天统计每种状态的比例.为了便于计算,在每天0点,系统需要将学生的状态重置,并插入一条数据作为一天的开始状态.另外,考 ...
- springboot整合xxl-job分布式定时任务【图文完整版】
一.前言 定时任务有很多种,有一些大的框架也有一些简单的实现. 比如常见的: JDK的Timer和TimerTask Quartz异步任务调度框架 分布式定时任务XXL-JOB Spring Task ...
随机推荐
- 网络协议 21 - RPC 协议(中)- 基于 JSON 的 RESTful 接口协议
上一节我们了解了基于 XML 的 SOAP 协议,SOAP 的 S 是啥意思来着?是 Simple,但是好像一点儿都不简单啊! 传输协议问题 对于 SOAP 来讲,比如我创建一个订单, ...
- vue的父子组件间的相互传参props及props数据的多种验证机制
感觉自己即将完全步入前端大军,后台老板都不需要我弄了,塞翁失马...时间会告诉我们是好是坏 好了言归正传,最近vue是搞的不亦乐乎啊,下面来总结一下vue组件间的各种使用方法以及一些技巧 ------ ...
- 制造业物料清单BOM、智能文档阅读、科学文献影响因子、"Celebrated Italian mathematician ZepartzatT Gozinto" 与 高津托图
意大利数学家Z.高津托 意大利伟大数学家Sire Zepartzatt Gozinto的生卒年代是一个谜[1],但是他发明的 “高筋图” 在 制造资源管理.物料清单(BOM)管理.智能阅读.科学文献影 ...
- 【面试】我是如何在面试别人Spring事务时“套路”对方的
“中国最好面试官” 自从上次写了一篇“[面试]我是如何面试别人List相关知识的,深度有点长文”的文章后,有读者专门加我微信,说我是“中国最好面试官”,这个我可受不起呀. 我只是希望把面试当作是一次交 ...
- Linux通配符应用详解
一.强大的“*” “*”在通配符中是最常用的一种,代表0个.一个或多个字符.在使用中有三种情况,表示三种不同的含义. 1.单独的“*” 这里指的是只有“*”出现的情况,默认为单独的一个,当然连续敲两个 ...
- Redis原理及使用
一:原理介绍 1:什么是redis? Redis 是一个基于内存的高性能key-value数据库. 2:Reids的特点Redis本质上是一个Key-Value类型的内存数据库,很像memcache ...
- K2制作流程
K2流程制作注意事项 1:分析需求 2:实施 步骤1:绘制流程图 步骤2:添加datafield[必备:ActJumped IsPass] 步骤3:添加线规则(如下图所示,在添加完毕规则之后,再给同 ...
- Windows环境下使用pip install安装lxml库
lxml是Python语言和XML以及HTML工作的功能最丰富和最容易使用的库.lxml是为libxml2和libxslt库的一个Python化的绑定.它与众不同的地方是它兼顾了这些库的速度和功能完整 ...
- webpack使用exclude
在进行项目打包的时候,当使用babel-loader进行js兼容时,不需要将node_modules模块下的所有js文件进行打包.
- Jmeter使用JDBC请求简介
1.现在oracle或mysql的jdbc然后放到jmeter的lib路径下 2.添加jdbc默认请求控件. 3.添加jdbc请求 4.发送 5.出现ORA-00911错误是由于sql语句错误,注意别 ...