Spring Shedule Task之注解实现 (两次启动Schedule Task 的解决方案)
在spring 中的新引入的task 命名空间。可以部分取代 quartz 功能,配置和API更加简单,并且支持注解方式。
第一步:
在Spring的相关配置文件中(applicationContext.xml或者是{project_name}_servelt.xml或者是独立的配置文件如XXX_quartz.xml)中配置并开启Spring Schedule 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:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:p="http://www.springframework.org/schema/p"
- 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/aop http://www.springframework.org/schema/aop/spring-aop-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/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.0.xsd
- ">
- <mvc:annotation-driven />
- <context:component-scan base-package="com.mytools.validator.engine" />
- <!-- 启动定时器 -->
- <task:annotation-driven/>
- </beans>
第二步:
可以在类中的需要定时执行的方法下指定如下Annotation
- @Scheduled(cron="0 33/3 * * * ?") //每小时的33分钟开始执行,每3分钟执行1次
- public void start() throws ServletException {
- validate();
- }
备注:其实@Scheduled中可以指定如下3种时间表达式:
(1)fixedRate:每隔多少毫秒执行一次该方法。如:
- @Scheduled(fixedRate=2000) // 每隔2秒执行一次
- public void scheduleMethod(){
- System.out.println("Hello world...");
- }
(2)fixedDelay:当一次方法执行完毕之后,延迟多少毫秒再执行该方法。
(3)cron:详细配置了该方法在什么时候执行。cron值是一个cron表达式。如:
- @Scheduled(cron="0 0 0 * * SAT")
- public voidarchiveOldSpittles() {
- // ...
- }
到指定时间后,任务总是执行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中去加载该文件:
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/cms-servlet.xml, classpath:cms-quartz.xml</param-value>
- </context-param>
注:出自 http://www.tuicool.com/articles/jmU7bq
Spring Shedule Task之注解实现 (两次启动Schedule Task 的解决方案)的更多相关文章
- Spring3 Schedule Task之注解实现 (两次起动Schedule Task 的解决方案)
Spring3 Schedule Task之注解实现 (两次起步Schedule Task 的解决方案)Spring3 Schedule Task之注解实现 (两次启动Schedule Task 的解 ...
- spring 基于XML和注解的两种事务配置方式
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Spring 配置定时器(注解+xml)方式—整理
一.注解方式 1. 在Spring的配置文件ApplicationContext.xml,首先添加命名空间 xmlns:task="http://www.springframework.or ...
- 使用spring提供的@Scheduled注解创建定时任务
使用方法 操作非常简单,只要按如下几个步骤配置即可 1. 导入jar包或添加依赖,其实定时任务只需要spring-context即可,当然起服务还需要spring-web: 2. 编写定时任务类和方法 ...
- spring boot @Async异步注解上下文透传
上一篇文章说到,之前使用了@Async注解,子线程无法获取到上下文信息,导致流量无法打到灰度,然后改成 线程池的方式,每次调用异步调用的时候都手动透传 上下文(硬编码)解决了问题. 后面查阅了资料,找 ...
- 使用spring的@Scheduled注解执行定时任务,启动项目不输出警告
在applicationContext.xml中添加: xmlns:task="http://www.springframework.org/schema/task" xsi:sc ...
- 使用 Spring 2.5 基于注解驱动的 Spring MVC
http://www.ibm.com/developerworks/cn/java/j-lo-spring25-mvc/ 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Sp ...
- 使用 Spring 2.5 基于注解驱动的 Spring MVC--转
概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Spring 2.5 又为 Spring MVC 引入了注解驱动功能.现在你无须让 Controller 继承任何接口,无需在 ...
- JavaEE开发之Spring中的条件注解组合注解与元注解
上篇博客我们详细的聊了<JavaEE开发之Spring中的多线程编程以及任务定时器详解>,本篇博客我们就来聊聊条件注解@Conditional以及组合条件.条件注解说简单点就是根据特定的条 ...
随机推荐
- 过目不忘JS正则表达式
正则表达式,有木有人像我一样,学了好几遍却还是很懵圈,学的时候老明白了,学完了忘光了.好吧,其实还是练的不够,所谓温故而知新,可以为师矣,今天就随我来复习一下这傲娇的正则表达式吧. 为啥要有正则表达式 ...
- [SAP ABAP开发技术总结]ABAP调优——代码优化
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- mysql基本命令整理
1.replace into(insert into 的增强版) replace into tbl_name(col_name, ...) values(...)replace into tbl_na ...
- 基于Python的网页文档处理脚本实现
嵌入式web服务器不同于传统服务器,web需要转换成数组格式保存在flash中,才方便lwip网络接口的调用,最近因为业务需求,需要频繁修改网页,每次的压缩和转换就是个很繁琐的过程,因此我就有了利用所 ...
- ssh启动报错:org.dom4j.DocumentException: Connection timed out: connect Nested exception: Connection timed out: connect
ssh项目启动报错: org.dom4j.DocumentException: Connection timed out: connect Nested exception: Connection t ...
- 嵌入式Linux学习笔记(0)基础命令。——Arvin
学习记录: 到今天为止ARM裸机开发学习进程:1.2.1-1.2.14 预科班知识Linux介绍学习进程:0.2.1-0.2.6 学习内容笔记: 学习了Linux的开发方式的优劣介绍 学习了常用文件夹 ...
- jquery.autocomplete 模糊查询 支持分组
//demo <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <lin ...
- Python之路 day2 字符串/元组/列表/字典互转
#-*-coding:utf-8-*- #1.字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type ' ...
- php 二维数组排序
usort($info ,function($a,$b){ $a1 = $a['score']; $b1 = $b['score']; if($a1 == $b1) return 0; return ...
- 实际项目中的一个angularjs 应用
实际需求:通过下拉框,选择自己需要的类型,创建元素(要求必须是输入点击保存了才能出现对应的类型块) html代码: <div class="list-panel-data"& ...