使用ThreadPoolTask​​Executor

时间:2017-05-12 09:53:39

标签: spring-integration

我正在使用Spring Integration和Spring Boot在基于Spring Guides的定位机器上进行一些开发。我正在使用Gradle来构建和运行应用程序。以下代码用于引导Spring,我可以通过按Enter键来终止应用程序。

public class Application {
    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext ctx = new SpringApplication(Application.class).run(args);
        System.out.println("Hit Enter to terminate");
        System.in.read();
        ctx.close();
    } 
}

这很好但是当我在集成流程中引入ThreadPoolTaskExecutor时,应用程序永远不会终止。我必须使用^ C来杀死应用程序。我使用的代码如下。

    ...
    channel(MessageChannels.executor(myTaskExecutor()))
    ... 

    @Bean
    public ThreadPoolTaskExecutor myTaskExecutor() {
        ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
        pool.setCorePoolSize(10);
        pool.setMaxPoolSize(20);
        pool.setWaitForTasksToCompleteOnShutdown(true);
        pool.setAwaitTerminationSeconds(1);
        pool.initialize();
        return pool;
    }

我有:

  1. 试图在关闭上下文之前和之后关闭执行程序(使用shutdown())方法。
  2. 也在onApplicationEvent(ContextClosedEvent事件)方法中尝试了上述代码。
  3. 暂时评论在线程中运行的代码,以确保不以任何方式保留线程。
  4. 还有什么我需要做的吗?

0 个答案:

没有答案