程序中新建Thread部署到WebSphere中產生的問題

Thread replayReqProducerThread=new Thread(new AdjustmentReplayRequestProducer(...);

replayReqProducerThread.start();

public class AdjustmentReplayRequestProducer implements Runnable{

public void run() {

while(true){...}

} }


以上代碼希望在整個context中創建單例的Producer,不停地從database獲取數據,放入隊列,供消費者獲取。但是部署到Websphere後,每次重啓application後,都會多處一個Producer。原因是停止Webapplication時,由於該Thread沒有納入到Spring Context或任何其他Websphere管理的容器中,所以沒有被銷燬。

正確的做法是:1)由Spring管理的ThreadPool來創建該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)在代碼中加入

while(true){

if (Thread.currentThread().isInterrupted()) {

logger.info("producerTask is interrupted, will exit ...");

                        break; }  ...

}

Thread.sleep(retrieveFcstAdjmtFromDBInterval);

} catch (InterruptedException e) {

logger.info("error occured: ", e);

Thread.currentThread().interrupt();

}


因爲WebSphere在Stop application時,會stop Spring contxt.

com.ibm.ws.runtime.component.ApplicationMgrImpl AUDIT WSVR0217I: Stopping application: ruby-ae-replay_war

com.ibm.ws.webcontainer.webapp INFO com.ibm.ws.webcontainer.webapp.WebApp log SRVE0292I: Servlet Message - [ruby-ae-replay_war#ruby-ae-replay.war]:.Closing Spring root WebApplicationContext

Spring 會調用replayProducerTaskExecutor的destroy的this.executor.shutdownNow()

ThreadPoolExecutor的shutdownNow中有interruptWorkers

 for (Worker w : workers) w.interruptIfStarted();->t.interrupt();

整個流程過後,該Produer會退出loop 循環。進而被回收

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章