`
MauerSu
  • 浏览: 494735 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

spring 定时器配置

 
阅读更多

源:http://blog.csdn.net/cl61917380/article/details/6265664

http://blog.163.com/qiu_yuanjie/blog/static/1637448312010113092625562/

评:

 

demo svn地址:https://localhost:8443/svn/Demo

 

spring 定时器(spring3.0)

cron介绍:
http://www.360doc.com/content/10/0127/14/36589_14507247.shtml

 

用了标签 真的简单好多!!!

 

 

首先要引入xsd:

  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  3.     xmlns:task="http://www.springframework.org/schema/task"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  5.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  6.     http://www.springframework.org/schema/task   
  7.     http://www.springframework.org/schema/task/spring-task-3.0.xsd">  
  8.   
  9. <!--  
  10.   
  11. 这里加入了  
  12. xmlns:task="http://www.springframework.org/schema/task"  
  13. http://www.springframework.org/schema/task   
  14.     http://www.springframework.org/schema/task/spring-task-3.0.xsd  
  15.   
  16. -->  
  17.   
  18. <task:annotation-driven /> <!-- 定时器开关-->  
  19.   
  20.   
  21.     <bean id="taskTest" class="com.jungle.test.TaskTest"></bean>  
  22.   
  23.     <task:scheduled-tasks>  
  24.         <!--  
  25. 这里表示的是从第五秒开始 ,每三秒执行一次 (而不是 三分之五 秒执行一次哦~~)  
  26. -->  
  27.         <task:scheduled ref="taskTest" method="say" cron="5/3 * * * * ?" />  
  28.         <task:scheduled ref="taskTest" method="hello" cron="5/3 * * * * ?"/>  
  29.     </task:scheduled-tasks>  

 

普通java类:

 

  1. package com.jungle.test;  
  2.   
  3. import java.util.Date;  
  4.   
  5. public class TaskTest {  
  6.   
  7.     public void say() {  
  8.         System.out.println("这个真好用!!!" + new Date());  
  9.     }  
  10.   
  11.     public void hello(){  
  12.         System.out.println("hello!!!");  
  13.     }  

 

spring定时器的使用(spring2.5.6)

 

spring的定时器功能,它不仅实现起来方便,功能强大,而且在web开发时正好配合spring框架使用。

    spring支持jdk内置的Timer类和Quartz Scheduler

 

介绍spring的定时器,当然要先介绍配置文件applicationContext.xml了。

 

<bean name="job" class="org.springframework.scheduling.quartz.JobDetailBean">

     <property name="jobClass">

         <value>jaoso.news.web.action.JobAction</value>

     </property>

     <property name="jobDataAsMap">

         <map>

             <entry key="timeout">

                 <value>10</value>

              </entry>

         </map>

     </property>

</bean>

 

说 明:org.springframework.scheduling.quartz.JobDetailBean是spring对你的类进行调度的代理, 在jobClass中要指定你的任务类(com.yangsq.web.action.JobAction),在jobDataAsMap中向你的任务类 中注入一些信息,当然也可以reference一个,不要忘记在你的任务里加入这些属性及set方法(有些罗嗦)。

timeout属性设定了当服务器启动后过10秒钟首次调用你的JobAction。

 

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">

     <property name="jobDetail">

         <ref bean="job"/>

     </property>

     <property name="cronExpression">

         <value>0 0/2 * * * ?</value>

     </property>

</bean>

 

说 明:org.springframework.scheduling.quartz.CronTriggerBean是spring提供的触发器,在这个 触发器中设定了要触发的job(jobDetail属性设定了先前定义的bean),同时设定了触发时间(cronExpression)---每隔两分 钟(0 0/2 * * * ?),这个的设定方式最后会说明。

 

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

     <property name="triggers">

         <list>

             <ref local="cronTrigger"/>

         </list>

     </property>

</bean>

 

说明:org.springframework.scheduling.quartz.SchedulerFactoryBean这是一个spring的工厂bean,在他的triggers属性列表中加入刚才定义的触发器,这里可以定义多个触发器(list嘛)。

 

好了,配置文件就介绍完了,该介绍com.yangsq.web.action.JobAction类了,

 

引入包:

spring中的定时器功能 - 邱园杰 - 邱园杰的博客

说明:QuartzJobBean是spring自带的,把spring的jar包加入就行了,但是前两个包要去下了,呵呵,google吧。

 

spring中的定时器功能 - 邱园杰 - 邱园杰的博客

spring中的定时器功能 - 邱园杰 - 邱园杰的博客

 

当然要继承QuartzJobBean了,但是光extends不行,必须要重载他的executeInternal方法

spring中的定时器功能 - 邱园杰 - 邱园杰的博客

 

好了,一个spring的时间调度完成了。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics