0
点赞
收藏
分享

微信扫一扫

spring 定时器配置


评:

 

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

 

spring 定时器(spring3.0)

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

 

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

 

 

首先要引入xsd:


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.0.xsd  
    http://www.springframework.org/schema/task   
>
  
<!--  
  
这里加入了  
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task   
    http://www.springframework.org/schema/task/spring-task-3.0.xsd  
  
-->
  
<task:annotation-driven /> <!-- 定时器开关-->
  
  
<bean id="taskTest" class="com.jungle.test.TaskTest"></bean>
  
<task:scheduled-tasks>
<!-- 
这里表示的是从第五秒开始 ,每三秒执行一次 (而不是 三分之五 秒执行一次哦~~) 
-->
<task:scheduled ref="taskTest" method="say" cron="5/3 * * * * ?" />
<task:scheduled ref="taskTest" method="hello" cron="5/3 * * * * ?"/>
</task:scheduled-tasks>



 

普通java类:

 


package
  
import
  
public class
  
public void
"这个真好用!!!" + new
    }  
  
public void
"hello!!!");  
    }  
}

 

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 定时器配置_xhtml

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

 

spring 定时器配置_触发器_02

spring 定时器配置_触发器_03

 

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

spring 定时器配置_spring_04

 

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

举报

相关推荐

0 条评论