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.