(Turn) Spring's quartz timer is repeatedly executed twice at the same time to solve the problem {not available after testing}

When I used Spring's quartz timer recently, I found that after the time was reached, the task was always repeated twice, both under tomcat or jboss.
Print out their hashcodes and find that they are different, that is to say, when the web container starts, two quartz threads are started repeatedly. 
After researching, it is found that quartz is indeed loaded twice: 
the first time: when the web container starts, when the applicationContext.xml file is read, it will be loaded once. 
Second time: Spring itself loads applicationContext.xml once. 
And my 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 when the web container starts, the file can be loaded 

so that quartz will only be loaded when the web container starts. Once, Spring won't load again.
 
The web.xml configuration is as follows:
 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/classes/applicationContext.xml,/WEB-INF/classes/applicationContext-timertask.xml</param-value>
  </context-param>
  <!-- 开启监听 -->
  <listener>
       <listener-class>
           org.springframework.web.context.ContextLoaderListener
       </listener-class>
   </listener>
 
Write the timer configuration as a separate configuration file, and load it only once when the web container starts
 
This problem will also appear under Spring's TimerTask timer, and the solution is also the same. I hope it will help you guys.
 
 
 
REFS:http://blog.csdn.net/zhujianpengzha/article/details/8140442

Guess you like

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