定时任务

SpringBoot定时任务

默认单线程

1.开启注解

在Spring启动类上加上@EnableScheduling

2.设置执行时间:

  • 使用fixRate:

    在方法上加上@Scheduled(fixRate=30* 60 *1000)

    fixRate=(小时* 分钟* 秒* 1000毫秒*)--只能实现指定间隔

  • 使用Cron表达式:

    在方法上加上@Scheduled(cron="0 0 9-22/4 * *")

    @Scheduled(cron="秒 分 时 日 月 星期 年(可选)")

    Cron表达式讲解:

    在线生成cron表达式:https://cron.qqe2.com/

    分隔符:

    符号 作用 例子
    列出所有值 如果在分钟中使用 5,8表示 分钟为5和8时触发
    - 范围 在分钟中使用5-8表示分钟从5到8每分钟都会触发
    * 该域的任意值 在分钟中使用*,表示对每分钟不做限制
    / 起始时间时触发,然后每隔固定时间触发一次 在分钟中使用5/8,表示五分钟时触发一次,之后每隔8分钟触发一次

    专有符号: --tips:除?外,Spring定时任务不支持其他转有符号

符号 作用 例子
只能用在月和星期互斥时
L 表示最后 ,只能出现在星期和月时 在周(周从星期日开始)中使用5L,表示最后一个星期四
W 表示周一到周五(工作日)只能出现在月中,系统自动离最近的工作日开始时触发
LW L和W连用,表示某月最后一个工作日
# 用于确定的每个月的第几个星期几只能出现在周中 设定3#2表示某月的第二个星期二执行
C 只能用在月和周中,需要关联日历 在月中使用5C 表示每个月的五号执行

使用异步多线程

  1. 开启异步注解

Spring启动类上加上@EnableScheduling的同时加上@EnableAsync

  1. 设置异步执行

在方法上加上@Scheduled(cron="0 0 9-22/4 * *")的同时加上@Async

Spring定时任务XML配置(注解形式几乎同上)

使用springTask: ----springTask为spring自带的

  1. 引用spring-context的依赖(使用springboot项目基本可省)

  2. 添加配置文件spring.xml,开启注解扫描

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd"> <context:component-scan base-package="com.code.xxx" /> </beans>
  1. 定义定时任务方法添加@Component--交给spring容器管理
public class Todo{
public void task1(){
System.out.println("task1开始")}
}
  1. 在spring.xml中配置:
<task:scheduled-tasks>
<!-- 每两秒执行一次-->
<task:scheduler ref="todo" method="task1" cron="0/2 * * * * ?"/>
</task:scheduled-tasks>
  1. 测试定时任务:

5.1获取springContext

   ApplicationContext context=new ClassPathXmlApplication("spring.xml")

5.2. 获取指定的Bean对象

    Todo todo1=(Todo)context.getBean("todo")
  1. 重要:在xml中开启定时任务驱动
<task:annotation-driven>

最新文章

  1. jQuery 上传头像插件Jcrop的实例
  2. 前端面试那些坑之HTML篇
  3. poj3250
  4. iOS开发之巧用Block和代理方法结合来传值
  5. 进程间通信 System V 消息队列
  6. 【原】ios打包ipa的四种实用方法(.app转.ipa)
  7. Java 工程转 C#
  8. nginx实战2---浏览器设置缓存
  9. [iOS Keychain本地长期键值存储]
  10. jdbc 日期时间相关的类型
  11. DrawerLayout一个简单的实例(与ActionBar无关)
  12. SQL2008 SQL Server 代理服务提供的凭据无效
  13. 【WPF】【火车站点信息查询】
  14. javascript面向对象2
  15. php 常用的JS
  16. 201521123100 《Java程序设计》第9周学习总结
  17. MySQL基本命令1
  18. monkey测试 -- 原理和操作步骤
  19. 自动化pip安装
  20. [软件]Xcode查找系统framework所在路径

热门文章

  1. 我所使用的生产 Java 17 启动参数
  2. 【Java面试】为什么引入偏向锁、轻量级锁,介绍下升级流程
  3. Python双人五子棋
  4. 实测:云RDS MySQL性能是自建的1.6倍
  5. 前端学习 linux —— shell 编程
  6. Javaweb-在idea中配置Tomcat
  7. cut命令、case与select语句
  8. MYSQL的事务和索引
  9. Mysql错误:The server time zone value is unrecognized or represents more than one time zone
  10. arcgis中nodata设为0及其小技巧