设为首页 加入收藏

TOP

Spring MVC使用Cron表达式的定时器
2015-02-02 14:32:15 来源: 作者: 【 】 浏览:13
Tags:Spring MVC 使用 Cron 表达式 定时器

Spring MVC的功能非常强大,集成了Quartz定时器的功能,可以通过Cron表达式和简单的注解就实现定时执行任务的功能。


网上看到不少例子,但是都不是很全。


闲话少说,首先要在springmvc.xml中添加下面几行:


xmlns:task="http://www.springframework.org/schema/task"



http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd


有了这两行代码,就可以在配置文件中添加定时器配置的XML代码。例子如下:


还是在springmvc.xml里面,这两行不用再解释,让springmvc知道去哪里扫描带注解的文件:





?
? ? ?




然后在下面加上:



? ? ?
? ? ? ? queue-capacity="500" rejection-policy="CALLER_RUNS" />
? ?


这几行从网上copy。


同时还要添加一个aopaliaance.jar,否则会报错:noClassDefoundError:org/aopalliance/aop/Advice


地址:http://mirrors.ibiblio.org/pub/mirrors/maven2/aopalliance/aopalliance/1.0/


下载后add to buildpath。


至此配置工作完成。


下面开始写代码:


import java.util.Date;


import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component ;


@Component
public class ScheduledTest2 {


?@Scheduled(cron = "0 0/1 * * * ?")
?public void runFunction(){
? System.out.println(new Date() + " package.controller scheduled test --> mahaha") ;
?}
?
}


然后就OK了!每分钟执行一次~~~?


?


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇一些关于Java的句子 下一篇FCkeditor和struts2结合使用解决..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: