Spring 整合quartz 時 定時任務被調用兩次以及quartz 的配置


package com.synpower.quzrtz;

import java.util.Date;

import org.springframework.stereotype.Component;


@Component
public class TimedTasks {
    public void execute1() {  
      for (int i = 0; i <1000; i++) {
          System.out.println(new Date() + "==================execute1");  
    }
    }  
    public void execute2() {  
        for (int i = 0; i <1000; i++) {
      System.out.println(new Date() + " -> execute2======");  
        }
    }
}
 1 <?xml version="1.0" encoding="UTF-8"?>  
 2 <beans xmlns="http://www.springframework.org/schema/beans"  
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 4     xmlns:context="http://www.springframework.org/schema/context"  
 5     xmlns:aop="http://www.springframework.org/schema/aop"  
 6     xmlns:tx="http://www.springframework.org/schema/tx"  
 7     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd  
 8         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
 9         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd  
10         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> 
11    
12     <bean id="taskDetail_1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
13         <!-- 執行的類 -->  
14         <property name="targetObject">  
15             <ref bean="timedTasks" />  
16         </property>  
17         <!-- 類中的方法 -->  
18         <property name="targetMethod">  
19             <value>execute1</value>  
20         </property>  
21     </bean>  
22     
23     <bean id="cronTriggerBean_1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">  
24         <property name="jobDetail">  
25             <ref bean="taskDetail_1" />  
26         </property>  
27         <!-- 每天凌晨一點執行一次 -->  
28         <property name="cronExpression">  
29             <value>0 0 1 * * ?</value>  
30         </property>  
31     </bean>  
32     <bean id="taskDetail_2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
33         <!-- 任務所屬組 -->
34         <property name="group" value="job_work"/>
35         <!-- 任務所屬組名字 -->
36             <property name="name" value="job_work_name"/>
37              <!--false表示等上一個任務執行完后再開啟新的任務-->
38             <property name="concurrent" value="false"/>
39         <!-- 執行的類 -->  
40         <property name="targetObject">  
41             <ref bean="timedTasks" />  
42         </property>  
43         <!-- 類中的方法 -->  
44         <property name="targetMethod">  
45             <value>execute2</value>  
46         </property>  
47     </bean>  
48     <bean id="cronTriggerBean_2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">  
49         <property name="jobDetail">  
50             <ref bean="taskDetail_2" />  
51         </property>  
52         <!-- 每天凌晨兩點執行一次 -->  
53         <property name="cronExpression">  
54             <value>0 0 2 * * ?</value>  
55         </property>  
56     </bean>  
57       <!-- ======================== 調度工廠 ======================== -->  
58     <bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
59         <!-- 添加觸發器 -->  
60         <property name="triggers">  
61             <list>  
62                 <ref bean="cronTriggerBean_1" />
63                 <ref bean="cronTriggerBean_2" />  
64             </list>  
65         </property>  
66     </bean>  
67 </beans>  

然后在Springmvc中引入這個XML,重點來了

需要刪掉web.xml的一段配置

這樣quartz 就不會被Spring加載兩次了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM