The problem of quartz starting twice under spring

<div class="iteeye-blog-content-contain" style="font-size: 14px">
I was working on a project recently. During the test, I found that the scheduled task would be executed twice, so I checked the data, and they all said yes The Tomcat configuration problem, I changed it or not. Later, I had no choice but to use an online method to synchronize the method in the JOB and control it through a static variable. The configuration is as follows:



Xml code 
1. <bean id="sendOffMailTimerBean" class ="com.c35.rouen.job.OffMailReportJob">   
2. <property name="isolationAreaService"> 
3. <ref bean="isolationAreaService" /> 
4. </property>      
5. </bean> 
6. 
7. <bean id="offMailReportJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">   
8. <property name="targetObject"> 
9. <ref bean="  sendOffMailTimerBean"/>
10. </property> 
11. <!-- call the method in the class--> 
12.      <property name="targetMethod"> 
13.          <value>doSend</value> 
14.      </property> 
15.      <property name="concurrent"> 
16.          <value>false</value> 
17.      </property>              
18.   </bean>   
19.     
20.   <bean id="deleteOffMailTimerBean" class="com.c35.rouen.job.OffMailDestroyJob">   
21. <property name="isolationAreaService"> 
22.  <ref bean="isolationAreaService" /> 
23. </property>      
24.   </bean>      
25.     
26.<bean id="offMailDestroyJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">   
27.      <property name="targetObject"> 
28.          <ref bean="deleteOffMailTimerBean"/> 
29.      </property> 
30.      <!-- 调用类中的方法 --> 
31.      <property name="targetMethod"> 
32.          <value>doDelete</value> 
33.      </property> 
34.      <property name="concurrent"> 
35.          <value>false</value> 
36.      </property>              
37.   </bean>       
38. 
39.   <!-- 每天整点发送隔离邮件报告 --> 
40.<bean id="offMailReportSendTrigger" 
41. class="org.springframework.scheduling.quartz.CronTriggerBean"> 
42. <property name="jobDetail" ref="offMailReportJob"></property> 
43. <property name="cronExpression"> 
44.  <value>0 0 * * * ?</value> 
45. </property> 
46.</bean> 
47. 
48.<!-- 每天凌晨三点检查删除过期的隔离邮件 --> 
49.<bean id="offMailDestroyTrigger" 
50. class="org.springframework.scheduling.quartz.CronTriggerBean"> 
51. <property name="jobDetail" ref="offMailDestroyJob"></property> 
52. <property name="cronExpression"> 
53.  <value>0 0 3 * * ?</value> 
54. </property> 
55.</bean>   
56. 
57.<!-- 启动定时器 --> 
58.<bean id ="schedulerFactory" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
59. <property name="triggers"> 
60.  <list> 
61.   <ref bean="offMailReportSendTrigger" /> 
62.   <ref bean="offMailDestroyTrigger" /> 
63.  </list> 
64. </property> 
65.</bean> 
   <bean id="sendOffMailTimerBean" class="com.c35.rouen.job.OffMailReportJob">
  <property name="isolationAreaService">
   <ref bean="isolationAreaService" />
  </property>   
    </bean>

<bean id="offMailReportJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
       <property name="targetObject">
           <ref bean="sendOffMailTimerBean"/>
       </property>
       <!-- 调用类中的方法 -->
       <property name="targetMethod">
           <value>doSend</value>
       </property>
       <property name="concurrent">
           <value>false</value>
       </property>           
    </bean>
   
    <bean id="deleteOffMailTimerBean" class="com.c35.rouen.job.OffMailDestroyJob">
  <property name="isolationAreaService">
   <ref bean="isolationAreaService" />
  </property>   
    </bean>   
   
<bean id="offMailDestroyJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
       <property name="targetObject">
           <ref bean="deleteOffMailTimerBean"/>
       </property>
       <!-- call a method in the class-->        </property>           <value>doDelete</value>
       <property name="targetMethod">


       <property name="concurrent">
           <value>false</value>
       </property>           
    </bean>    

    <!-- Send Quarantine Mail Report on the hour every day -->
<bean id="offMailReportSendTrigger"
  class="org. springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail" ref="offMailReportJob"></property>
  <property name="cronExpression">
   <value>0 0 * * * ?</value>
  </property >
</bean>

<!-- Check and delete expired quarantined emails every day at 3am-->
<bean id="offMailDestroyTrigger"
  class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail" ref="offMailDestroyJob"></property>
  <property name="cronExpression">
   <value>0 0 3 * * ?</value>
  </property>
</bean>

<!-- 启动定时器 -->
<bean id ="schedulerFactory" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="triggers">
   <list>
    <ref bean="offMailReportSendTrigger" />
    <ref bean="offMailDestroyTrigger" />
   </list>
  </property>
</bean>





JOB如下:

Java代码 
1.public class OffMailReportJob extends QuartzJobBean {  
2. 
3. private IIsolationAreaService isolationAreaService;  
4.   
5.public IIsolationAreaService getIsolationAreaService() {  
6.  return isolationAreaService;  
7. }  
8. 
9. public void setIsolationAreaService(IIsolationAreaService isolationAreaService) {  
10. this.isolationAreaService = isolationAreaService;  
11. }  
12. 
13. private static JLogger log = LoggerFactory.getLogger(OffMailReportJob.class);  
14.   
15. static int sendIndexFlag =0;//Modified by doSend, do not modify this parameter in other methods  
16. 
17. public void doSend() {  
18.    
19. // The problem of calling the timer twice at the same time needs to find the reason  
20. if (sendIndexFlag > 0 )  
21. return;// Ensure that only one timer runs at the same time. In this way, only one thread is executed when the timer expires.  
22. sendIndexFlag = 1;// Lock  
23.    
24. try {  
25. isolationAreaService .sendOffMailReport();  
26.  } catch (Exception e) {  
27.   log.error(e.getMessage(), e);  
28.  }  
29.    
30.  sendIndexFlag = 0;// 锁定解除  
31. }  
32. 
33. @Override 
34. protected void executeInternal(JobExecutionContext arg0)  
35.   throws JobExecutionException {  
36. } 
public class OffMailReportJob extends QuartzJobBean {

private IIsolationAreaService isolationAreaService;

public IIsolationAreaService getIsolationAreaService() {
  return isolationAreaService;
}

public void setIsolationAreaService(IIsolationAreaService isolationAreaService) {
  this.isolationAreaService = isolationAreaService;
}

private static JLogger log = LoggerFactory.getLogger(OffMailReportJob.class);

static int sendIndexFlag =0;//Modified by doSend, do not modify this parameter in other methods

public void doSend() {
 
  // The problem that the timer is called twice at the same time needs to be found Reason
  if (sendIndexFlag > 0)
   return;// Ensure that only one timer runs at the same time, in this way, only one thread is executed when the timer expires
  sendIndexFlag = 1;// lock
 
  try {
   isolationAreaService.sendOffMailReport() ;
  } catch (Exception e) {
   log.error(e.getMessage(), e);
  }
 
  sendIndexFlag = 0;// lock release
}

@Override
protected void executeInternal(JobExecutionContext arg0)
   throws JobExecutionException {
}







can be solved by modifying this way The problem was checked and checked in the

background , and the root of the problem was found:

When the web container starts, it will be loaded once when the applicationContext.xml file is read.
Second time: Spring itself loads applicationContext.xml once.
Generally speaking, our quartz configuration is written in the applicationContext.xml file.
The solution is very simple.
First extract the quartz configuration information and save it as a separate file, such as applicationContext-quartz.xml
and then modify the web.xml so that the file can be loaded when the web container starts,
so that quartz will only be loaded when the web container starts. Once, Spring won't load again.




COPY FROM :http://max-1106.iteye.com/blog/899055

</div>

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326986819&siteId=291194637