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以及组合条件.条件注解说简单点就是根据特定的条 ...
随机推荐
- this 的值到底是什么?
你可能遇到过这样的 JS 面试题: var obj = { foo: function(){ console.log(this) } } var bar = obj.foo obj.foo() // ...
- Rails中的缓存
最近学习Rails. 看到如下代码: <% if notice %> <p id="notice"><%= notice %></p> ...
- 64位ubuntu下安装微博客户端的方法
最近安装了12.04的ubuntu系统,在unbutu提供的软件中心找不到微博客户端的应用,但在新浪的http://sinatair.sinaapp.com/下找到了官方的客户端. 于是下载了linu ...
- session,ajax 跨域cookie
什么是Session, 什么是Cookie? Session是由应用服务器维持的一个服务器端的存储空间,用户在连接服务器时,会由服务器生成一个唯一的SessionID,用该SessionID为标识符来 ...
- SwitchyOmega 在线调试
1,chrome 安装 Proxy SwitchyOmega 扩展程序 2,新建情景模式,输入模式名称"例如:new proxy1",选择"请选择情景模式的类型:代理服务 ...
- Netscape HTTP Cooke File Parser In PHP
http://www.hashbangcode.com/blog/netscape-http-cooke-file-parser-php I recently needed to create a f ...
- iOS开发 跳转系统设置
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; [[UIApplication sharedApplica ...
- R语言实战(一)介绍、数据集与图形初阶
本文对应<R语言实战>前3章,因为里面大部分内容已经比较熟悉,所以在这里只是起一个索引的作用. 第1章 R语言介绍 获取帮助函数 help(), ? 查看函数帮助 exampl ...
- 多线程环境的UI控件属性更新
Winform: public delegate void UpadataTextCallBack(string str,TextBox text); public void UpadtaText(s ...
- CentOS忘记密码或者丢失口令解决方法
重启系统,然后再五秒之内按下任意键. 进入下面画面后按 [ e ] 键 把光标移动到第二行(或者找到以kernel /vmlinuz开头的),再按下 [ e ] 键 在文本结尾处空一格再添加s ...