Solve the Problem of repeated execution of the Spring Z timer of Spring at the same time

Source: Internet
Author: User

When I recently used the quartz timer of Spring, I found that after the time, the task is always executed twice, both in tomcat or jboss.
Print out their hashcode and find it is different. That is to say, when the web container is started, two quartz threads are repeatedly started.
The study found that quartz does load twice:
First time: When the web container is started, it is loaded once when the applicationContext. xml file is read.
Second: Spring loads applicationContext. xml once.
My quartz configuration is written in the applicationContext. xml file.

The solution is simple.
First extract quartz configuration information, save it into a file, such as applicationContext-quartz.xml
Modify the web. xml file so that the file can be loaded when the web Container starts.

In this way, quartz is loaded only once when the web container is started, and Spring is no longer loaded.
 
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>
<! -- Enable listener -->
<Listener>
<Listener-class>
Org. springframework. web. context. ContextLoaderListener
</Listener-class>
</Listener>
 
Write the timer configuration into a configuration file, and load it only once when the web Container starts.
 
This problem also occurs under the TimerTask timer of Spring, and the solution is also the same. I hope it will be helpful to you.
 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.