o_oand0_0

软件开发
随笔 - 4, 文章 - 2, 评论 - 5, 引用 - 0
数据加载中……

基于spring+quartz开发定时器

1、准备Jar包
       在Spring所有包齐全的前提下还要导入一个定时器工具包:quartz-1.6.2.jar <http://www.opensymphony.com/quartz/download.action>
2、开发定时器类,实例代码如下:
 1public class TriggerUtil {
 2
 3    private TriggerUtil(){
 4        
 5    }

 6    
 7    public void expDataBase(){
 8        System.out.println("trigger actived..");
 9    }

10    
11}
3、配置定时任务
     为了清晰代码结构,单独建立一个配置定时任务的配置文件context-trigger.xml,并在applicationContext.xml进行import:
<import resource="context-trigger.xml"/>
    
     context-trigger.xml内容如下:
 1<?xml version="1.0" encoding="UTF-8"?>
 2<beans
 3    xmlns="http://www.springframework.org/schema/beans"
 4    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
 6    <!-- 定时器配置 -->
 7<!-- 配置定时器类 -->
 8  <bean id="triggerUtil" class="com.pro.base.util.TriggerUtil" >
 9  </bean>
10  <!-- 指定任务(方法) -->
11  <bean id="expDataBaseJob"
12      class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
13      <property name="targetObject">
14          <ref local="triggerUtil" />
15      </property>
16      <property name="targetMethod">
17          <value>expDataBase</value>
18      </property>
19  </bean>
20  <!-- 设定计划执行时间 -->
21  <bean id="expDataBaseTrigger"
22      class="org.springframework.scheduling.quartz.CronTriggerBean">
23      <property name="jobDetail">
24          <ref local="expDataBaseJob" />
25      </property>
26      <property name="cronExpression">
27          <value>00 33 21 * * ?</value>
28      </property>
29      </bean>
30      <!-- 任务执行器配置 -->
31      <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
32        <property name="triggers">
33            <list>
34                <ref local="expDataBaseTrigger" />
35            </list>
36        </property>
37    </bean>
38</beans>

附:定时时间配置说明
Expression    Meaning                                                                                                                                                                                      
0 0 12 * * ?     每天中午12点触发【Fire at 12pm (noon) every day 】                                                                                                                                           
0 15 10 ? * *     每天上午10:15触发【Fire at 10:15am every day 】                                                                                                                                              
0 15 10 * * ?     每天上午10:15触发【Fire at 10:15am every day 】                                                                                                                                              
0 15 10 * * ? *     每天上午10:15触发【Fire at 10:15am every day 】                                                                                                                                              
0 15 10 * * ? 2005     在2005这一年中每天上午10:15触发【Fire at 10:15am every day during the year 2005 】                                                                                                           
0 * 14 * * ?     每天下午14:00到15:00之间,每1分钟触发一次【Fire every minute starting at 2pm and ending at 2:59pm, every day 】                                                                             
0 0/5 14 * * ?     每天下午14:00到14:55之间,每5分钟触发一次【Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day 】                                                                          
0 0/5 14,18 * * ?     每天的14:00~14:55和18:00~18:55之间,每5分钟触发一次【Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day 】 
0 0-5 14 * * ?     每天的14:00~14:05之间,每1分钟触发一次【Fire every minute starting at 2pm and ending at 2:05pm, every day 】                                                                                
0 10,44 14 ? 3 WED     3月的每周三的14:10和14:44触发【Fire at 2:10pm and at 2:44pm every Wednesday in the month of March. 】                                                                                       
0 15 10 ? * MON-FRI     每周周一到周五的10:15触发【Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday 】                                                                                         
0 15 10 15 * ?    每月15日的10:15触发【 Fire at 10:15am on the 15th day of every month 】                                                                                                                     
0 15 10 L * ?     每月最后一天的10:15触发【Fire at 10:15am on the last day of every month 】                                                                                                                  
0 15 10 ? * 6L     每月的最后一个周五的10:15触发【Fire at 10:15am on the last Friday of every month 】                                                                                                         
0 15 10 ? * 6L 2002-2005     在2002到2005之间,每月的最后一个周五的10:15触发【Fire at 10:15am on every last friday of every month during the years 2002, 2003, 2004 and 2005 】                                          
0 15 10 ? * 6#3     每月的第三个星期五的10:15触发【Fire at 10:15am on the third Friday of every month 】                                                                                                        

posted on 2011-10-21 15:27 o_oand0_0 阅读(154) 评论(0)  编辑  收藏 所属分类: JavaJ2EE应用开发


只有注册用户登录后才能发表评论。


网站导航: