如何将ListeningExecuterService公开为Spring bean

时间:2015-08-25 19:05:14

标签: java spring guava

想知道是否有办法将ListeningExecuterService公开为Spring Singleton bean

1 个答案:

答案 0 :(得分:0)

经过一些研究发现了一种方式

@Bean(destroyMethod="shutdown")
public ListeningExecutorService listeningExecutorService() {
    ThreadFactory threadFactory = new ThreadFactoryBuilder()
        .setNameFormat(" Listening Executor Service [%d]").build();
    ExecutorService executorService = Executors.newScheduledThreadPool(
            SOMECONSTANT.TASK_EXECUTOR_THREADS, threadFactory);
    return MoreExecutors.listeningDecorator(executorService);
}

这是XML配置

 <!-- ThreadPool for executing   functions asynchronously -->
       <bean id="functionThreadPool" class="com.google.common.util.concurrent.MoreExecutors" factory-method="listeningDecorator">
          <constructor-arg>
                 <bean class="java.util.concurrent.Executors" factory-method="newCachedThreadPool"/>                     
          </constructor-arg>
     </bean>