Spring任务调度两种配置方式
来源:互联网 发布:灰蚂蚁太阳镜 知乎 编辑:程序博客网 时间:2024/06/01 19:15
XML配置方式
- 任务类
@Componentpublic class TestTask { public TestTask() { System.out.println("TestTask.TestTask()"); } public void task1() { System.out.println(Thread.currentThread().getName() + " >>> TestTask.task1()"); } public void task2() { System.out.println(Thread.currentThread().getName() + " >>> TestTask.task2()"); }}
- 最少任务调度配置: spring-task
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd "> <task:scheduled-tasks> <task:scheduled ref="testTask" method="task2" cron="*/1 * * * * ?" /> </task:scheduled-tasks></beans>
配置解析
- 任务池
<!-- 定义任务池 --><task:scheduled-tasks></task:scheduled-tasks>
在同一个任务池中定义的所有任务都是同步执行.
现做如下调度定义: 在同一任务池中定义两个不同的任务, 并做同时触发. ==同步阻塞(或断点)其中一个任务, 发现同一任务池中另一个任务也会进入等待状态.==
<task:scheduled-tasks> <task:scheduled ref="testTask" method="task1" cron="*/1 * * * * ?" /> <task:scheduled ref="testTask" method="task2" cron="*/2 * * * * ?" /></task:scheduled-tasks>
要实现多个任务异步执行, 需要将不互斥的任务分别定义在不同的任务池中.
<!-- 任务池1 --><task:scheduled-tasks> <task:scheduled ref="testTask" method="task1" cron="*/1 * * * * ?" /></task:scheduled-tasks><!-- 任务池2 --><task:scheduled-tasks> <task:scheduled ref="testTask" method="task2" cron="*/2 * * * * ?" /></task:scheduled-tasks>
- 任务队列
一条<task:scheduled/>
定义一个任务, 在同一任务池中, 可以定义多个相同任务.
<task:scheduled ref="testTask" method="task1" cron="*/1 * * * * ?" />
Annotation配置
- XML最少配置, default-lazy-init指定为false或default
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd " default-lazy-init="false"> <context:component-scan base-package="com.ty.task" /> <task:scheduler id="scheduler" pool-size="10" /> <task:annotation-driven scheduler="scheduler" /></beans>
- 类定义
- @Async 异步任务注解
- @Scheduled 调度注解
@Componentpublic class TestTask { private static final Logger LOGGER = LoggerFactory.getLogger(TestTask.class); public TestTask() { System.out.println("TestTask.TestTask()"); } @Scheduled(cron = "*/1 * * * * ?") @Async public void task1() { // LOGGER.error(Thread.currentThread().getName() + " ------ "); LOGGER.error(Thread.currentThread().getName() + " >>> TestTask.task1()"); } @Scheduled(cron = "*/2 * * * * ?") public void task2() { LOGGER.error(Thread.currentThread().getName() + " >>> TestTask.task2()"); }}
阅读全文
0 0
- Spring任务调度两种配置方式
- spring的两种定时任务配置方式
- Spring任务调度配置
- spring任务调度配置
- Spring配置Quartz任务调度
- Spring配置Quartz任务调度
- spring配置定时调度任务
- Spring配置quartz调度任务
- spring的三种任务调度实现方式
- Spring quartz 任务调度(注解方式)
- Spring定时调度任务实现方式
- spring 中配置定时调度两种方法介绍
- spring的quartz两种配置方式
- spring事务的两种配置方式
- Spring AOP 的两种配置方式
- spring aop 两种配置方式
- spring aop两种配置方式
- spring容器框架定时任务实现的两种方式
- BZOJ 4540: [Hnoi2016]序列 莫队算法
- iOS支持富文本Label控件
- android 跑马灯出现重复跳动、不滚动问题
- Spring Boot --微信企业号开发01-使用新花生壳进行本地开发测试
- MacOS 开发- NSBox
- Spring任务调度两种配置方式
- leetcode---unique-paths---dp
- 高可用方案之二 nginx热机双备
- Python日记【开更关于学习python的点点滴滴】-16.09.14
- 静态成员变量实现个对象之间数据共享
- struts2属性驱动,模型驱动
- Qt QMutexLock 自动解锁机制
- 第一次使用taskctl启动遇到问题
- python开发环境配置与部署