Thread Replayreqproducerthread=new Thread (new Adjustmentreplayrequestproducer (...);
Replayreqproducerthread.start ();
public class Adjustmentreplayrequestproducer implements runnable{
public void Run () {
while (true) {...}
} }
The above code would like to create a singleton producer throughout the context, and constantly retrieve data from the database and put it into a queue for consumers to obtain. However, after deploying to WebSphere, each time you restart application, there will be more than one producer. The reason is that when WebApplication is stopped, it is not destroyed because the thread is not included in the spring context or any other WebSphere managed container.
The correct approach is: 1) A spring-managed threadpool to create the thread
<bean id= "Replayproducertaskexecutor"
class= "Org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" >
<property name= "Corepoolsize" value= "1"/>
<property name= "Maxpoolsize" value= "1"/>
<property name= "Queuecapacity" value= "1"/>
</bean>
Replayproducertaskexecutor.execute (New Adjustmentreplayrequestproducer (...));
2) Add in the code
while (true) {
if (thread.currentthread (). isinterrupted ()) {
Logger.info ("Producertask is interrupted, would exit ...");
Break ; } ...
}
Thread.Sleep (Retrievefcstadjmtfromdbinterval);
} catch (Interruptedexception e) {
Logger.info ("Error occured:", e);
Thread.CurrentThread (). interrupt ();
}
Because WebSphere stops at stop application, the Spring contxt is stop.
Com.ibm.ws.runtime.component.ApplicationMgrImplAUDITwsvr0217i:stopping application: Ruby-ae-replay_war
Com.ibm.ws.webcontainer.webappINFOcom.ibm.ws.webcontainer.webapp.WebApp log Srve0292i:servlet Message-[Ruby-ae-replay_war#ruby-ae-replay.war]:. Closing Spring Root Webapplicationcontext
Spring calls Replayproducertaskexecutor's Destroy's This.executor.shutdownNow ()
There are interruptworkers in the Shutdownnow of Threadpoolexecutor.
for (Worker w:workers) w.interruptifstarted ();->t.interrupt ();
After the entire process, the Produer exits the loop loop. And then be recycled.
This article is from the "Technology" blog, so be sure to keep this source http://2828408.blog.51cto.com/2818408/1757911
Issues arising from new thread deployment to WebSphere in the program