Spring Shedule Task之注解实现 (两次启动Schedule Task 的解决方案)
在spring 中的新引入的task 命名空间。可以部分取代 quartz 功能,配置和API更加简单,并且支持注解方式。
第一步:
在Spring的相关配置文件中(applicationContext.xml或者是{project_name}_servelt.xml或者是独立的配置文件如XXX_quartz.xml)中配置并开启Spring Schedule Task.注意其中高亮的部分是必须的。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xmlns:mvc="http://www.springframework.org/schema/mvc" 8 xmlns:p="http://www.springframework.org/schema/p" 9 xmlns:task="http://www.springframework.org/schema/task" 10 xsi:schemaLocation=" 11 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 12 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 13 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 14 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd 15 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 16 http://www.springframework.org/schema/task 17 http://www.springframework.org/schema/task/spring-task-3.0.xsd 18 "> 19 <mvc:annotation-driven /> 20 <context:component-scan base-package="com.mytools.validator.engine" /> 21 22 <!-- 启动定时器 --> 23 <task:annotation-driven/> 24 </beans>
第二步:
可以在类中的需要定时执行的方法下指定如下Annotation
1 @Scheduled(cron="0 33/3 * * * ?") //每小时的33分钟开始执行,每3分钟执行1次 2 public void start() throws ServletException { 3 validate(); 4 }
备注:其实@Scheduled中可以指定如下3种时间表达式:
(1)fixedRate:每隔多少毫秒执行一次该方法。如:
1 @Scheduled(fixedRate=2000) // 每隔2秒执行一次 2 public void scheduleMethod(){ 3 System.out.println("Hello world..."); 4 }
(2)fixedDelay:当一次方法执行完毕之后,延迟多少毫秒再执行该方法。
(3)cron:详细配置了该方法在什么时候执行。cron值是一个cron表达式。如:
1 @Scheduled(cron="0 0 0 * * SAT") 2 public voidarchiveOldSpittles() { 3 // ... 4 }
到指定时间后,任务总是执行2次的解决方案:
这是因为我们很容易在一个基于Spring的Web工程中启动2个定时线程:
第一次:web容器启动的时候,读取applicationContext.xml(或者别的Spring核心配置文件)文件时,会加载一次。
第二次:Spring本身会加载applicationContext.xml(或者别的Spring核心配置文件)一次。
解决方案:将你的Task的相关配置独立出来并在web.xml中通过context-param加载。而不是通过spring加载。
1) 独立出Spring-Task,如新命名一个文件名叫cms_quartz.xml
2) 在web.xml中去加载该文件:
1 <context-param> 2 <param-name>contextConfigLocation</param-name> 3 <param-value>/WEB-INF/cms-servlet.xml, classpath:cms-quartz.xml</param-value> 4 </context-param>
注:出自 http://www.tuicool.com/articles/jmU7bq
转载于:https://www.cnblogs.com/kzhan/p/5742523.html
Spring Shedule Task之注解实现 (两次启动Schedule Task 的解决方案)相关推荐
- Spring3 Shedule Task之注解实现 (两次启动Schedule Task 的解决方案)
在spring3 中的新引入的task 命名空间.可以部分取代 quartz 功能,配置和API更加简单,并且支持注解方式.但是如果需要使用比较复杂的任务调度.还是建议使用quartz. 第一步: 在 ...
- Spring的任务调度@Scheduled注解——task:scheduler和task:executor的解析
Spring的任务调度@Scheduled注解--task:scheduler和task:executor的解析 applicationContext 的配置如下: <?xml version= ...
- spring springboot springcloud常用注解
@SpringBootApplication 组合注解,用在启动类上,源码: @Retention(RetentionPolicy.RUNTIME) @SpringBootConfiguration ...
- Spring加载properties文件的两种方式
2019独角兽企业重金招聘Python工程师标准>>> 在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取pro ...
- Spring Boot自定义 Servlet Filter 的两种方式
点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | 码农小胖哥 来源 | 公众号「码农小胖哥」 针 ...
- Spring 2.5 基于注解驱动的 Spring MVC
基于注解的配置有越来越流行的趋势,Spring 2.5 顺应这种趋势,为 Spring MVC 提供了完全基于注解的配置.本文将介绍 Spring 2.5 新增的 Sping MVC 注解功能,讲述如 ...
- 使用 Spring 2.5 基于注解驱动的 Spring MVC--转
概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Spring 2.5 又为 Spring MVC 引入了注解驱动功能.现在你无须让 Controller 继承任何接口,无需在 ...
- java spring框架 注解_详解Java的Spring框架中的注解的用法
1. 使用Spring注解来注入属性 1.1. 使用注解以前我们是怎样注入属性的类的实现: class UserManagerImpl implements UserManager { private ...
- 在spring中使用自定义注解注册监听器
2019独角兽企业重金招聘Python工程师标准>>> 接口回调 监听器本质上就是利用回调机制,在某个动作发生前或后,执行我们自己的一些代码.在Java语言中,可以使用接口来实现. ...
最新文章
- 打开 XP Pro SP2 远程桌面的多用户支持
- 【shell】shell编程(一)-入门
- 音视频通话:小议音频处理与压缩技术
- Python如何防止sql注入
- python异常处理优点_python各类异常处理学习笔记
- mysql重启root不能登_Mysql 5.7.28初始化使用root无法登录
- NOIP2007 树网的核
- 现代软件工程 M2 博客要求
- mysql 双主 脑裂_MySQL双主(主主)架构方案
- IPAD移动端交互原型通用设计方案、ipad元件库、移动元件库、元件列表、设计元件、交互示例、界面模板、设备模板、手势图标、社交界面、音乐、电商、视图控制器、指示器、指纹解锁、手势解锁、rp元件库
- excel表格行列显示十字定位_取消excel单元格十字定位(excle表格里的十字对准)
- 叛乱联机服务器未响应,叛乱沙漠风暴开服注意事项及操作指南经验一览
- 软件测试报告模板--实用--绝对靠谱
- fitbit手表中文说明书_fitbit感觉智能手表动手
- linux 怎么连接到网络打印机,如何在网络上的Windows,Mac和Linux PC之间共享打印机...
- win10电脑IIS服务器配置ASP环境
- php去除微信特殊符号,PHP方法处理微信昵称特殊符号过滤
- FBReader 探究 2
- aop:aspectj-autoproxy
- pytorch1.10新功能inference_mode
热门文章
- Git初学使用命令记录
- CentOS 7 源码编译MariaDB 5.5.46
- 给文本框添加模糊搜索功能(“我记录”MVC框架下实现)
- ORA-28056:Writing audit records to Windows Even...
- KN-S1008S1016S1024S1024F端口状态指示
- DATAGUARD配置错误的解决日志
- 提示No Launcher activity found
- android textView设置粗体
- git 简易指南+常用命令
- iPhone4 FaceTime 联通官方教程