[英]Executorservice shutdown and restart with Spring
I am using an ExecutorService
in my application and with a fixed thread pool. 我在我的应用程序中使用
ExecutorService
并使用了固定线程池。 I have declared it in my spring xml file as 我已经在我的spring xml文件中将其声明为
<bean id="executorService"
class="java.util.concurrent.Executors"
factory-method="newFixedThreadPool"
destroy-method="shutdownNow">
<constructor-arg value="5"/>
</bean>
Now there is a situation in my code where I have to force shutdown (using shutdownNow
) the ExecutorService
and then restart it. 现在,在我的代码中有一种情况,我必须强制关闭(使用
shutdownNow
) ExecutorService
,然后重新启动它。 I am really new with Spring and am unaware of how to do it. 我对Spring真的很陌生,却不知道该怎么做。 As of now I just submit tasks to the service and then do a
shutdownNow
, but my requirement says I want to restart the service. 到目前为止,我只是向服务提交任务,然后执行
shutdownNow
,但是我的要求是我想重新启动服务。
May I recommend simply managing the ExecutorService
yourself behind a bean. 我是否建议您仅在bean后面自己管理
ExecutorService
。 This way you can provide api for your bean to shutdown and recreate a brand new Executor service. 这样,您可以为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.