SpringBoot整合Quartz定时任务 系统job Spring Boot教程 调度任务
原文地址:https://www.cnblogs.com/allalongx/p/8477368.html
构建工程
创建一个Springboot工程,在它的程序入口加上@EnableScheduling,开启调度任务。
1
2
3
4
5
6
7
8
|
@SpringBootApplication @EnableScheduling public class SpringbootSchedulingTasksApplication { public static void main(String[] args) { SpringApplication.run(SpringbootSchedulingTasksApplication. class , args); } } |
创建定时任务
创建一个定时任务,每过5s在控制台打印当前时间。
1
2
3
4
5
6
7
8
9
10
11
12
|
@Component public class ScheduledTasks { private static final Logger log = LoggerFactory.getLogger(ScheduledTasks. class ); private static final SimpleDateFormat dateFormat = new SimpleDateFormat( "HH:mm:ss" ); @Scheduled (fixedRate = 5000 ) public void reportCurrentTime() { log.info( "The time is now {}" , dateFormat.format( new Date())); } } |
通过在方法上加@Scheduled注解,表明该方法是一个调度任务。
- @Scheduled(fixedRate = 5000) :上一次开始执行时间点之后5秒再执行
- @Scheduled(fixedDelay = 5000) :上一次执行完毕时间点之后5秒再执行
- @Scheduled(initialDelay=1000, fixedRate=5000) :第一次延迟1秒后执行,之后按fixedRate的规则每5秒执行一次
- @Scheduled(cron=” /5 “) :通过cron表达式定义规则,什么是cro表达式,自行搜索引擎。
测试
启动springboot工程,控制台没过5s就打印出了当前的时间。
1
2
3
4
|
2017 - 04 - 29 17 : 39 : 37.672 INFO 677 — [pool- 1 -thread- 1 ] com.forezp.task.ScheduledTasks : The time is now 17 : 39 : 37 2017 - 04 - 29 17 : 39 : 42.671 INFO 677 — [pool- 1 -thread- 1 ] com.forezp.task.ScheduledTasks : The time is now 17 : 39 : 42 2017 - 04 - 29 17 : 39 : 47.672 INFO 677 — [pool- 1 -thread- 1 ] com.forezp.task.ScheduledTasks : The time is now 17 : 39 : 47 2017 - 04 - 29 17 : 39 : 52.675 INFO 677 — [pool- 1 -thread- 1 ] com.forezp.task.ScheduledTasks : The time is now 17 : 39 : 52 |
在springboot创建定时任务只需2步:
- 1.在程序的入口加上@EnableScheduling注解。
- 2.在定时方法上加@Scheduled注解。
原文地址: https://www.cnblogs.com/mr-wuxiansheng/p/6971493.html
记录一个SpringBoot 整合 Quartz 的Demo实例
POM.XML文件
- <!-- 定时器任务 quartz需要导入的坐标 -->
- <dependency>
- <groupId>org.quartz-scheduler</groupId>
- <artifactId>quartz</artifactId>
- <version>1.8.5</version>
- </dependency>
类似于控制器代码:

- package com.xiaowu.quartz.demo;
- import java.util.Date;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Component;
- /***
- *
- * Quartz设置项目全局的定时任务
- *
- * @Component注解的意义 泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。一般公共的方法我会用上这个注解
- *
- *
- * @author WQ
- *
- */
- @Component
- public class QuartzDemo {
- @Scheduled(cron = "0 0/1 * * * ?") // 每分钟执行一次
- public void work() throws Exception {
- System.out.println("执行调度任务:"+new Date());
- }
- @Scheduled(fixedRate = 5000)//每5秒执行一次
- public void play() throws Exception {
- System.out.println("执行Quartz定时器任务:"+new Date());
- }
- @Scheduled(cron = "0/2 * * * * ?") //每2秒执行一次
- public void doSomething() throws Exception {
- System.out.println("每2秒执行一个的定时任务:"+new Date());
- }
- @Scheduled(cron = "0 0 0/1 * * ? ") // 每一小时执行一次
- public void goWork() throws Exception {
- System.out.println("每一小时执行一次的定时任务:"+new Date());
- }
- }

启动SpringBoot项目,即可。
- public static void main(String[] args) {
- SpringApplication.run(Chapter1Application.class, args);
- }
,截图如下:
SpringBoot整合Quartz定时任务 系统job Spring Boot教程 调度任务的更多相关文章
- SpringBoot整合Quartz定时任务
记录一个SpringBoot 整合 Quartz 的Demo实例 POM.XML文件 <!-- 定时器任务 quartz需要导入的坐标 --> <dependency> < ...
- SpringBoot整合Quartz定时任务(持久化到数据库)
背景 最近在做项目,项目中有个需求:需要使用定时任务,这个定时任务需要即时生效.查看Quartz官网之后发现:Quartz提供两种基本作业存储类型: RAMJobStore :RAM也就是内存,默认情 ...
- SpringBoot整合Quartz定时任务 的简单实例 2
(1)什么是Quartz?(2)Quartz的特点:(3)Quartz专用词汇说明:(4)Quartz任务调度基本实现原理: 接下来看下具体的内容: (1)什么是Quartz? Quartz是一个完全 ...
- SpringBoot整合Quartz定时任务 的简单实例
POM.XML文件 <!-- 定时器任务 quartz需要导入的坐标 --> <dependency> <groupId>org.quartz-scheduler& ...
- Spring整合Quartz定时任务执行2次,Spring定时任务执行2次
Spring整合Quartz定时任务执行2次,Spring定时任务执行2次 >>>>>>>>>>>>>>>&g ...
- Spring整合Quartz定时任务 在集群、分布式系统中的应用(Mysql数据库环境)
Spring整合Quartz定时任务 在集群.分布式系统中的应用(Mysql数据库环境) 转载:http://www.cnblogs.com/jiafuwei/p/6145280.html 单个Q ...
- SpringBoot整合Quartz及log4j实例
SpringBoot整合Quartz及log4j实例 因为之前项目中经常会做一些定时Job的东西,所以在此记录一下,目前项目中已经使用elastic-job,这个能相对比Quartz更加简单方便一些, ...
- 使用Spring boot整合Hive,在启动Spring boot项目时,报错
使用Spring boot整合Hive,在启动Spring boot项目时,报出异常: java.lang.NoSuchMethodError: org.eclipse.jetty.servlet.S ...
- Spring Boot 教程 - Elasticsearch
1. Elasticsearch简介 Elasticsearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearc ...
随机推荐
- 037 SparkSQL ThriftServer服务的使用和程序中JDBC的连接
一:使用 1.实质 提供JDBC/ODBC连接的服务 服务运行方式是一个Spark的应用程序,只是这个应用程序支持JDBC/ODBC的连接, 所以:可以通过应用的4040页面来进行查看操作 2.启动服 ...
- C语言结构体在内存中的存储情况探究------内存对齐
条件(先看一下各个基本类型都占几个字节): void size_(){ printf("char类型:%d\n", sizeof(char)); printf("int类 ...
- 002.FTP配置项详解
一 相关配置项 anonymous_enable=YES #允许匿名用户登录 local_enable=YES #允许本地用户登录 write_enable=YES #允许本地用户上传 local_u ...
- 关于ImportError: libssl.so.10: cannot open shared object file: No such file or directory unable to load app 0 (mountpoint='') (callable not found or import error)
一.问题描述 在亚马逊云服务器使用Nginx+uwsgi部署django项目时,项目可以使用python manage.py runserver正常运行,uwsgi测试也没问题,Nginx也正常启动, ...
- kafka配置监控和消费者测试
概念 运维 配置 监控 生产者与消费者 流处理 分区partition 一定条件下,分区数越多,吞吐量越高.分区也是保证消息被顺序消费的基础,kafka只能保证一个分区内消息的有序性 副本 每个分区有 ...
- 深入分析Spring Boot2,解决 java.lang.ArrayStoreException异常
将某个项目从Spring Boot1升级Spring Boot2之后出现如下报错,查了很多不同的解决方法都没有解决: Spring boot2项目启动时遇到了异常: java.lang.ArraySt ...
- react初始化阶段
初始化阶段可以使用的函数:getDefaultProps:只调用一次,实例之间共享引用.只有在组件的第一个实例被初始化的时候,才会调用他,然后react会把这个函数的返回结果保存起来,从第二个实例开始 ...
- BZOJ4275 : [ONTAK2015]Badania naukowe
设f[i][j]为a[1..i]与b[1..j]的LCS,g[i][j]为a[i..n]与b[j..m]的LCS. 若C为0,则ans=f[n][m]. 否则求出d[i]=a中从i开始往右匹配上c串的 ...
- ==与equals与hashCode的区别联系。
前言:对于引用类型的变量,它涉及到两块内存,一块是堆中的内存,用于存放new出来的对象(就是对象的具体内容):另一块是栈内存,用来存放变量在堆内存中的地址. 1,“==” 判断两个变量是否是同一个变量 ...
- LINUX 源码+内核所有参数说明
http://www.cnblogs.com/tolimit/p/5065761.html