Spring 配置 定时器

来源:互联网 发布:php变量覆盖 编辑:程序博客网 时间:2024/05/09 02:56

要实现每隔多长时间发送一个请求

在applicationContext.xml中配置

所需jar包:

链接:http://pan.baidu.com/s/1jGL4kzO 密码:6ojg

一,配置目标类

[javascript] view plaincopy
  1. <pre name="code" class="html">   <bean id="backupAdListPolicyJob" class="com.hyan.jms.Test">    
  2.     <property name="para" value="Spring定时测试v1"></property>  
  3.    </bean>  
二,定时器配置
[html] view plaincopy
  1. <bean id="scheduSer" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">    
  2.        <!-- 指定 要执行的定时任务类 -->    
  3.        <property name="targetObject">    
  4.            <ref bean="scheduImpl"/>    
  5.        </property>    
  6.        <!-- 指定执行任务的方法名称-->    
  7.        <property name="targetMethod">    
  8.            <value>scheduImpl</value>    
  9.        </property>    
  10.    </bean>    
三,定时器时间间隔
[html] view plaincopy
  1. <bean id="timeTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">    
  2.        <!-- 声明要运行的实体 -->    
  3.        <property name="jobDetail">    
  4.            <ref bean="scheduSer"/>    
  5.        </property>    
  6.        <property name="cronExpression">    
  7.            <!-- 在每天10点到下午10:59期间的每1分钟触发  -->    
  8.            <value>0 * 10 * * ?</value>    
  9.        </property>    
  10.    </bean>  
四,启动定时器
[html] view plaincopy
  1. <bean id="sfb" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">    
  2.         <property name="triggers">    
  3.             <list>    
  4.                 <ref local="timeTrigger"/>    
  5.             </list>    
  6.         </property>    
  7. </bean>  
在web.xml中的配置
[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"  
  3.  xmlns="http://java.sun.com/xml/ns/javaee"  
  4.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
  6.  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.    
  8.  <context-param>  
  9.   <param-name>contextConfigLocation</param-name>  
  10.   <param-value>/WEB-INF/applicationContext.xml</param-value>  
  11.  </context-param>  
  12.   
  13.  <listener>  
  14.   <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
  15.  </listener>  
  16.  <listener>  
  17.   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  18.  </listener>  
  19.   <welcome-file-list>  
  20.     <welcome-file>index.jsp</welcome-file>  
  21.   </welcome-file-list>  
  22. </web-app>  

java的实现代码
[java] view plaincopy
  1. package com.hyan.jms;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4. import java.util.Date;  
  5.   
  6. public class Test {  
  7.      private String para;  
  8.      public void scheduImpl(){  
  9.       SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  10.       System.out.println(para+" Time is :"+format.format(new Date()));  
  11.      }  
  12.      public String getPara() {  
  13.       return para;  
  14.      }  
  15.      public void setPara(String para) {  
  16.       this.para = para;  
  17.      }  
  18.   
  19. }  

启动tomcat 实现效果

关于定时器的表达式

时间的配置如下:
<value>0 * 10 * * ?</value> 

时间大小由小到大排列,从秒开始,顺序为 秒,分,时,天,月,年    *为任意 ?为无限制。由此上面所配置的内容就是,在每天10点到下午10:59期间的每1分钟触发 

-----------------------------------------------------------------------------------------------------------------


定时任务设置

<!--启用spring @Transactional注解 -->
        <tx:annotation-driven />  
          
        <!-- 定时任务设置 -->
        <bean id="scheduler"
            class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        </bean>
            <!-- 定时任务 -->
            <!-- 调用工作对象和工作方法 -->
        <bean id="jobBean" class="com.gamexun.gm.common.RemovetTemp" />  
        <bean id="jobWeekly" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
            <!-- 调用的类 -->
            <property name="targetObject" ref="jobBean" />  
            <!-- 调用类的方法 -->
            <property name="targetMethod" value="executeWeekly" />  
            <!-- 不能并行 -->
            <property name="concurrent" value="false" />  
        </bean>
        <!-- 每隔2小时执行一次 -->
        <bean id="triggerWeekly" class="org.springframework.scheduling.quartz.CronTriggerBean">  
            <property name="jobDetail" ref="jobWeekly" />  
            <property name="cronExpression" value="0 0 0/2 * * ?"/><!-- 0 0/1 * * * ? --><!-- 0 */2 * * * -->
        </bean>  
        <bean id="startQuertz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="true" >  
            <property name="triggers">  
                <list>  
                    <ref bean="triggerWeekly" />
                </list>  
               </property>  
        </bean> 

0 0
原创粉丝点击