SpringBoot定时任务说明
1. 定时任务实现方式
定时任务实现方式:
- Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。一般用的较少,这篇文章将不做详细介绍。
- 使用Quartz,这是一个功能比较强大的的调度器,可以让你的程序在指定时间执行,也可以按照某一个频度执行,配置起来稍显复杂,有空介绍。
- SpringBoot自带的Scheduled,可以将它看成一个轻量级的Quartz,而且使用起来比Quartz简单许多,本文主要介绍。
定时任务执行方式:
- 单线程(串行)
- 多线程(并行)
2. 创建定时任务
package com.autonavi.task.test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.autonavi.task.ScheduledTasks;
@Component
public class ScheduledTest {
private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);
@Scheduled(cron="0 0/2 8-20 * * ?")
public void executeFileDownLoadTask() {
// 间隔2分钟,执行工单上传任务
Thread current = Thread.currentThread();
System.out.println("定时任务1:"+current.getId());
logger.info("ScheduledTest.executeFileDownLoadTask 定时任务1:"+current.getId()+ ",name:"+current.getName());
}
@Scheduled(cron="0 0/1 8-20 * * ?")
public void executeUploadTask() {
// 间隔1分钟,执行工单上传任务
Thread current = Thread.currentThread();
System.out.println("定时任务2:"+current.getId());
logger.info("ScheduledTest.executeUploadTask 定时任务2:"+current.getId() + ",name:"+current.getName());
}
@Scheduled(cron="0 0/3 5-23 * * ?")
public void executeUploadBackTask() {
// 间隔3分钟,执行工单上传任务
Thread current = Thread.currentThread();
System.out.println("定时任务3:"+current.getId());
logger.info("ScheduledTest.executeUploadBackTask 定时任务3:"+current.getId()+ ",name:"+current.getName());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
@Scheduled 注解用于标注这个方法是一个定时任务的方法,使用@Scheduled(cron=”…”) 表达式来设置定时任务。
// 每天早八点到晚八点,间隔2分钟执行任务
@Scheduled(cron="0 0/2 8-20 * * ?")
// 每天早八点到晚八点,间隔3分钟执行任务
@Scheduled(cron="0 0/3 8-20 * * ?")
// 每天早八点到晚八点,间隔1分钟执行任务
@Scheduled(cron="0 0/1 8-20 * * ?")
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
3. 启动定时任务
@ComponentScan
@EnableAutoConfiguration
@EnableScheduling
@Configuration
public class App {
private static final Logger logger = LoggerFactory.getLogger(App.class);
public static void main(String[] args) {
SpringApplication.run(App.class, args);
logger.info("oops");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
其中 @EnableScheduling 注解的作用是发现注解@Scheduled的任务并后台执行。
4. 执行结果
2016-02-14-14-51 [pool-2-thread-1] [com.autonavi.task.ScheduledTasks] [INFO] - ScheduledTest.executeUploadBackTask 定时任务3:15,name:pool-2-thread-1
定时任务2:15
2016-02-14-14-51 [pool-2-thread-1] [com.autonavi.task.ScheduledTasks] [INFO] - ScheduledTest.executeUploadTask 定时任务2:15,name:pool-2-thread-1
定时任务1:15
2016-02-14-14-52 [pool-2-thread-1] [com.autonavi.task.ScheduledTasks] [INFO] - ScheduledTest.executeFileDownLoadTask 定时任务1:15,name:pool-2-thread-1
定时任务2:15
2016-02-14-14-52 [pool-2-thread-1] [com.autonavi.task.ScheduledTasks] [INFO] - ScheduledTest.executeUploadTask 定时任务2:15,name:pool-2-thread-1
定时任务2:15
2016-02-14-14-53 [pool-2-thread-1] [com.autonavi.task.ScheduledTasks] [INFO] - ScheduledTest.executeUploadTask 定时任务2:15,name:pool-2-thread-1
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
5. 串行任务
上述方法可以实现定时任务,方式也比较简单,不用配置什么文件啥的,但你会发现一个问题,就是不论定时任务被安排在多少个class类中,其依然是单线程执行定时任务(串行任务):
2016-02-14-15-05 [pool-2-thread-1] [com.autonavi.task.ScheduledTasks] [INFO] - ScheduledTasks.executeUploadTask 定时任务1:15,name:pool-2-thread-1
定时任务2:15
2016-02-14-15-06 [pool-2-thread-1] [com.autonavi.task.ScheduledTasks] [INFO] - ScheduledTest.executeUploadTask 定时任务2:15,name:pool-2-thread-1
- 1
- 2
- 3
- 1
- 2
- 3
上述执行结果中ScheduledTest和ScheduledTasks是两个独立类,都有各自定时任务,但运行时起Thread Name都是一样的pool-2-thread-1,因此每个定时任务若要新启一个线程,需要自行编写实现或者配置文件。
SpringBoot定时任务默认单线程,多线程需要自行实现或配置文件
6. 并行任务
有时候会碰到不同业务的定时任务,这时候利用并行任务处理要妥当,采用多线程任务。只需要配置SpringBoot的配置文件:applicationContext.xml,添加如下内容:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<!-- Enables the Spring Task @Scheduled programming model -->
<task:executor id="executor" pool-size="5" />
<task:scheduler id="scheduler" pool-size="10" />
<task:annotation-driven executor="executor" scheduler="scheduler" />
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
添加红框中的内容
同时注意补充title中遗漏的网址。
效果如下,每个调度处理一个任务,每个调度也是一个子线程:
有关executor、scheduler参数的介绍见文中的34.5 The Task Namespace节。
7. 基于springboot的定时任务工程样例
8. 动态定时任务说明
有时候需要实现动态定时任务,即工程启动后,可以实现启动和关闭任务,同时也可以设置定时计划。这就需要利用到quartz,spring官方对于这个包下面各类的介绍,后续抽空配置下这类业务的实现:
http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/scheduling/quartz/package-summary.html。
quartz API:
http://www.quartz-scheduler.org/api/2.1.7/index.html
SpringBoot定时任务说明的更多相关文章
- springboot定时任务处理
定时任务是一种很常见的应用场景,springboot中的定时任务完全用的spring的那一套,用起来比较简单,需要注意的是线程池配置的那一块 使用 @EnableScheduling 注解就可以开启定 ...
- 解决SpringBoot 定时计划 quartz job 任务重复执行多次(10次)
上一篇:SpringBoot多任务Quartz动态管理Scheduler,时间配置,页面+源 设置了多个 任务,本应该是各司其职的,任务调用多线程处理任务,but这个定时任务竟然同时跑了10次???如 ...
- springBoot 定时+发送邮件
定时任务引入meaven依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifa ...
- java 与大数据学习较好的网站
C# C#中 Thread,Task,Async/Await,IAsyncResult 的那些事儿!https://www.cnblogs.com/doforfuture/p/6293926.html ...
- spring task定时器的配置使用
spring task的配置方式有两种:配置文件配置和注解配置. 1.配置文件配置 在applicationContext.xml中增加spring task的命名空间: xmlns:task=&qu ...
- SpringBoot中的定时任务与Quartz的整合
SpringBoot集成Quartz 定时任务Quartz : 就是在指定的时间执行一次或者循环执行,在项目的开发中有时候会需要的, 还是很有用的. SpringBoot内置的定时 添加依赖 < ...
- Websocket教程SpringBoot+Maven整合(详情)
1.大话websocket及课程介绍 简介: websocket介绍.使用场景分享.学习课程需要什么基础 笔记: websocket介绍: WebSocket协议是基于TCP的一种新的网络协议.它实现 ...
- Websocket教程SpringBoot+Maven整合
1.大话websocket及课程介绍 简介: websocket介绍.使用场景分享.学习课程需要什么基础 2.课程技术选型和浏览器兼容讲解 简介: 简单介绍什么是springboot.socketjs ...
- 最全spring boot视频系列,你值得拥有
================================== 从零开始学Spring Boot视频 ================================== àSpringBoot ...
随机推荐
- 单片机 MCU 中 stack 使用的探讨
stack 的使用,是单片机开发中影响最大,但是最少被讨论的问题.而提及这个问题的地方,都是对这个问题含糊其辞. 今天花了点时间,使用最笨的办法,直接阅读汇编代码,来对这个问题就行探究,这里做一下记录 ...
- 读取PBOC电子现金指令流
该指令流仅适用于T=0协议卡片. 终端对IC卡的响应: 60 须要额外的工作等待时间,说明IC卡端数据还未处理好. 61 发送GET RESPONSE命令取应答数据 6C 加上取字节数,命令重发 ...
- 怎么样加快JavaScript加载和执行效率
概览 无论当前 JavaScript 代码是内嵌还是在外链文件中,页面的下载和渲染都必须停下来等待脚本执行完成.JavaScript 执行过程耗时越久,浏览器等待响应用户输入的时间就越长.浏览器在下载 ...
- 关于asyncio知识(一)
一.介绍 asyncio 是python3.4 引入的一个新的并发模块,主要通过使用coroutines 和 futures 来让我们更容易的去实现异步的功能,并且几乎和写同步代码一样的写代码,还没有 ...
- 在interface vlan下敲no ip proxy-arp什么意思
取消由路由带来的ARP请求. proxy ARP有哪些优点? 最主要的一个优点就是能够在不影响其他router的路由表的情况下在网络上添加一个新的router,这样使得子网的变化对主机是透明的 pro ...
- activemq 安装 部署
ActiveMQ是一种开源的,实现了JMS1.1规范的,面向消息(MOM)的中间件,为应用程序提供高效的.可扩展的.稳定的和安全的企业级消息通信.ActiveMQ使用Apache提供的授权,任何人都可 ...
- Android 查看蓝牙hci日志
最近在调试android连接ble设备,需要查看hci日志.记录一下方法. 1. 开发者选项->启用蓝牙HCI信息收集日志. 2. android 8版本,默认位置/data/misc/blue ...
- 3.静态AOP实现-代理模式
通过代理模式实现在RegUser()方法本身业务前后加上一些自己的功能,如:BeforeProceed和AfterProceed,即不修改UserProcessor类又能增加新功能 定义1个用户接口, ...
- WebSphere MQ中的CCSID
CCSID是一个字符集的标识.作为unicode标准通过定义一个字符集内每个字符要对应那个数字值的方式定义了一个字符集.这说明CCSID就是一个定义字符集顺序的标识数码罢了.IBM的字符标识架构在文档 ...
- OpenGL——天空盒子模型
加载天空盒子的六个jpg图片,不知道为什么加载不出顶部和底部的jpg图片.没有解决. 加载来自http://www.custommapmakers.org/skyboxes.php的tga图片,没有问 ...