Spring Task 任务调度器
来源:互联网 发布:詹姆斯生涯数据统计 编辑:程序博客网 时间:2024/05/16 09:27
一句话说spring task
spring task 是一个可以让你的程序在你规定的时机执行规定好的动作,如同古人云:万事俱备只欠东风,那么东风来时即可一战。
程序中理解
spring task 在spring-core包下,我们只需引入
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.7.RELEASE</version></dependency>
准备好要执行的任务(作战计划)
package com.lagersoft.task;import java.time.LocalDateTime;import org.springframework.stereotype.Component;/** * 调度任务 * @author xiaoping * */@Componentpublic class FirstTask { public void execute1(){ System.out.printf("Task: %s, Current time: %s\n", 1, LocalDateTime.now()); } public void execute2(){ System.out.printf("Task: %s, Current time: %s\n", 2, LocalDateTime.now()); } public void execute3(){ System.out.printf("Task: %s, Current time: %s\n", 3, LocalDateTime.now()); } public void execute4(){ System.out.printf("Task: %s, Current time: %s\n", 4, LocalDateTime.now()); } public void execute5(){ System.out.printf("Task: %s, Current time: %s\n", 5, LocalDateTime.now()); } public void execute6(){ System.out.printf("Task: %s, Current time: %s\n", 6, LocalDateTime.now()); } public void execute7(){ System.out.printf("Task: %s, Current time: %s\n", 7, LocalDateTime.now()); } public void execute8(){ System.out.printf("Task: %s, Current time: %s\n", 8, LocalDateTime.now()); } public void execute9(){ System.out.printf("Task: %s, Current time: %s\n", 9, LocalDateTime.now()); } public void execute10(){ System.out.printf("Task: %s, Current time: %s\n", 10, LocalDateTime.now()); } public void execute11(){ System.out.printf("Task: %s, Current time: %s\n", 11, LocalDateTime.now()); }}
spring配置文件(配置好作战时机)
<!-- 配置注解扫描 --> <context:component-scan base-package="com.lagersoft.task"/> <task:scheduler id="taskScheduler" pool-size="100" /> <!--cron 表达式 : {秒} {分} {时} {日期(具体哪天)} {月} {星期} 6个参数,空格隔开 --> <!-- ref 关联到实现类FirstTask method 关联到具体执行的方法 cron 则指明该方法的执行时机 --> <task:scheduled-tasks scheduler="taskScheduler"> <!-- 每分钟的30秒时触发任务 --> <task:scheduled ref="firstTask" method="execute1" cron="30 * * * * ?"/> <!-- 每小时的30分钟时触发任务 --> <task:scheduled ref="firstTask" method="execute2" cron="0 30 * * * ?"/> <!-- 每天12点钟触发任务 --> <task:scheduled ref="firstTask" method="execute3" cron="0 0 12 * * ?"/> <!-- 每月15号的3点40分时触发任务,比如说计算员工工资,嘻嘻 --> <task:scheduled ref="firstTask" method="execute4" cron="0 40 3 15 * ?"/> <!-- 每年10月1号的8点10分时触发任务 比如说国庆节给员工发福利 --> <task:scheduled ref="firstTask" method="execute5" cron="0 10 8 1 10 ?"/> <!-- 每分钟的15秒、30秒、45秒时触发任务 --> <task:scheduled ref="firstTask" method="execute6" cron="15,30,45 * * * * ?"/> <!-- 每分钟的15秒到45秒每隔1秒触发任务 --> <task:scheduled ref="firstTask" method="execute7" cron="15-45 * * * * ?"/> <!-- 每分钟的每15秒时任务任务,每隔5秒触发一次 --> <task:scheduled ref="firstTask" method="execute8" cron="15/5 * * * * ?"/> <!-- 每分钟的15到30秒之间开始触发,每隔5秒触发一次 --> <task:scheduled ref="firstTask" method="execute9" cron="15-30/5 * * * * ?"/> <!-- 每小时的0分0秒开始触发,每隔3分钟触发一次 --> <task:scheduled ref="firstTask" method="execute10" cron="0 0/3 * * * ?"/> <!-- 星期一到星期五的9点30分0秒触发任务 比如开始计算工作日员工考勤情况 --> <task:scheduled ref="firstTask" method="execute11" cron="0 30 9 ? * MON-FRI"/> </task:scheduled-tasks>
启动程序(开始暗中观察)
public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/root-context.xml"); }
最后
再来看spring task 的时候,你可能会认为它是一个闹钟或是定时器,啥也不说,code里面出真知(专属于你的认知)
1 0
- Spring Task 任务调度器
- Spring任务调度器Task的使用
- Spring-Task 定时任务调度
- spring task定时调度任务
- Spring Task - Spring 原生定时任务调度
- Spring之Spring Task任务调度
- Spring任务调度器之Task的使用
- Spring任务调度器之Task的使用
- Spring任务调度器之Task的使用
- Spring任务调度器之Task的使用
- spring task 调度器
- Spring实现自动任务调度-------------task
- Spring实现自动任务调度-------------task
- Spring任务调度 task:scheduled-tasks
- Spring任务调度<task:scheduled-tasks>(定时器)
- Spring任务调度<task:scheduled-tasks>
- Spring Boot -- Spring的任务调度Spring Task
- spring实现自动定时任务调度【Spring Task】【Quartz】
- java--面向对象
- <<HTML5从入门到精通>>
- 计算某日是该年第几天
- JAVA去重复字符串
- android标题栏中添加返回按钮
- Spring Task 任务调度器
- layabox笔记 目录结构
- OpenGL系列教程(1)——认识OpenGL
- 求某个数n的某一个因子的个数 (高斯取整函数思想)
- 操作mysql的相关错误
- 向量的点乘和叉乘在游戏中的应用
- spring项目的单元测试编写
- 文章标题
- 【LeetCode】Base 7 解题报告