springboot实现定时任务的两种方式
方式一:在springboot启动类上添加@EnableScheduling注解,然后创建具体的任务类,在方法上添加@Scheduled注解,并指明执行频率即可。如下:
@Component
public class TestJob { private final Logger logger = LoggerFactory.getLogger(TestJob.class); @Scheduled(cron = "*/3 * * * * *")
public void testJob() {
logger.info("Job Test...");
}
} 方式二:创建一个继承了QuartzJobBean的任务类,同时创建quarz配置类,以向spring容器中注入JobDetail和Trigger。如下:
public class Test2Job extends QuartzJobBean {
private final Logger logger = LoggerFactory.getLogger(Test2Job.class);
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
logger.info("Test2 Job...");
}
}
@Configuration
public class ScheculerConfiguration { @Bean
public JobDetail jobDetail() {
return JobBuilder.newJob(Test2Job.class)
.withIdentity("test2Job")
.storeDurably()
.build();
} @Bean
public Trigger jobTrigger() {
ScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInMilliseconds(3)
.repeatForever();
return TriggerBuilder.newTrigger()
.withIdentity("test2JobTrigger")
.forJob(jobDetail())
.withSchedule(scheduleBuilder)
.build();
}
}
springboot实现定时任务的两种方式的更多相关文章
- SpringBoot配置定时任务的两种方式
一.导入相关的jar包 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...
- SpringBoot整合Servlet的两种方式
SpringBoot整合Servlet有两种方式: 1.通过注解扫描完成Servlet组件的注册: 2.通过方法完成Servlet组件的注册: 现在简单记录一下两种方式的实现 1.通过注解扫描完成Se ...
- SpringBoot从入门到精通二(SpringBoot整合myBatis的两种方式)
前言 通过上一章的学习,我们已经对SpringBoot有简单的入门,接下来我们深入学习一下SpringBoot,我们知道任何一个网站的数据大多数都是动态的,也就是说数据是从数据库提取出来的,而非静态数 ...
- 【SpringBoot】05.SpringBoot整合Listener的两种方式
SpringBoot整合Listener的两种方式: 1.通过注解扫描完成Listener组件的注册 创建一个类实现ServletContextListener (具体实现哪个Listener根据情况 ...
- 【SpringBoot】03.SpringBoot整合Servlet的两种方式
SpringBoot整合Servlet的两种方式: 1. 通过注解扫描完成Servlet组件注册 新建Servlet类继承HttpServlet 重写超类doGet方法 在该类使用注解@WebServ ...
- Spring Boot 中实现定时任务的两种方式
在 Spring + SpringMVC 环境中,一般来说,要实现定时任务,我们有两中方案,一种是使用 Spring 自带的定时任务处理器 @Scheduled 注解,另一种就是使用第三方框架 Qua ...
- springboot整合mybatis的两种方式
https://blog.csdn.net/qq_32719003/article/details/72123917 springboot通过java bean集成通用mapper的两种方式 前言:公 ...
- spring实现定时任务的两种方式
本文为博主原创,未经允许不得转载 项目中要经常事项定时功能,在网上学习了下用spring的定时功能,基本有两种方式,在这里进行简单的总结, 以供后续参考,此篇只做简单的应用. 1.在spring-se ...
- 【SpringBoot】04.SpringBoot整合Filter的两种方式
SpringBoot整合Filter过滤器的两种方式: 1.通过扫描注解完成Filter组件注册 创建一个类,实现Filter接口,实现doFilter()方法 在该类使用注解@WebFilter,设 ...
随机推荐
- 开源中国+soucetree
参考链接:http://www.cocoachina.com/programmer/20151012/13682.html 1.创建一个工程
- Python之Monitor监控线程(干货)
在日常工作中常遇到这样的情况,我们需要一个监控线程用于随时的获得其他进程的任务请求,或者我们需要监视某些资源等的变化,一个高效的Monitor程序如何使用python语言实现呢?为了解决上述问题,我将 ...
- Nastya Studies Informatics
Nastya Studies Informatics time limit per test 1 second memory limit per test 256 megabytes in ...
- *LOJ#2322. 「清华集训 2017」Hello world!
$n \leq 50000$的树,有点权$\leq 1e13$,$q \leq 400000$次操作,有两种操作:从$s$跳到$t$每次$k$步,不到$k$步直接跳到$t$,每次把经过的点取根号:同样 ...
- 自动化测试框架之robot framework的应用分析
序言:很多人都对自动化测试框架痴迷,我曾经也痴迷过一段时间,以前觉得自己对框架说的头头是道,现在回过头来看以前,说归说,但在如何应用还是欠缺,这一段时间,自己经历了一系列框架的构建和应用的时期,所以, ...
- codevs——1570 去看电影
1570 去看电影 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 农夫约翰带着他的一些奶牛去看电影.而他的 ...
- LeetCode:926. 将字符串翻转到单调递增
暴力法超时:思想:动态规划 public int minFlipsMonoIncrb(String S) { int result = S.length(); for (int i = 0; i &l ...
- Angular2.X 笔记
本文为原创,转载请注明出处: cnzt 文章:cnzt-p http://www.cnblogs.com/zt-blog/p/7762590.html 前提: angular-cli (前 ...
- react 起手式
http://blog.csdn.net/zhouzhiande/article/details/52349344 http://blog.csdn.net/zhouzhiande/article/d ...
- CocoaPods为project的全部target添加依赖支持
在使用CocoaPods时.pod install默认仅仅能为xcodeproject的第一个target加入依赖库支持.假设要为全部的target添加可依照例如以下步骤进行 两种情 1. 编辑Pod ...