Issues arising from new thread deployment to WebSphere in the program

Source: Internet
Author: User

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

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.