[英]Executorservice shutdown and restart with Spring
我在我的应用程序中使用ExecutorService
并使用了固定线程池。 我已经在我的spring xml文件中将其声明为
<bean id="executorService"
class="java.util.concurrent.Executors"
factory-method="newFixedThreadPool"
destroy-method="shutdownNow">
<constructor-arg value="5"/>
</bean>
现在,在我的代码中有一种情况,我必须强制关闭(使用shutdownNow
) ExecutorService
,然后重新启动它。 我对Spring真的很陌生,却不知道该怎么做。 到目前为止,我只是向服务提交任务,然后执行shutdownNow
,但是我的要求是我想重新启动服务。
我是否建议您仅在bean后面自己管理ExecutorService
。 这样,您可以为Bean提供api,以关闭并重新创建全新的Executor服务。
public class ExecutorServiceHolder {
private ExecutorService executorService;
//set this through normal Spring config
private int numThreads;
@PostConstruct //once the properties have been, initialize your executorservice..
public void afterPropertiesSet() {
this.executorService = Executors.newFixedThreadPool(numThreads);
}
//This is just pseudo-code, you may want to enhance with more thorough checking
//to make sure that the previous pool is shutdown..
public synchronized void shutDownAndRestart() {
this.executorService.shutdownNow();
this.executorService = Executors.newFixedThreadPool(numThreads);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.