【问题标题】:destruction of @bean created executor销毁@bean 创建的执行器
【发布时间】:2018-04-05 08:15:35
【问题描述】:

我在 Spring 4 中有以下 annotationconfig

    @Bean(name = "replicationThreadExecutor")
public Executor replicationThreadExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(threadPoolCoreSize);
    executor.setMaxPoolSize(threadPoolCoreSize);
    executor.initialize();
    return executor;
}

我关心的是Executor 的破坏。 Spring 会认识到 Executor 的动态类型是 DisposableBean 的一个实例并销毁它,还是我需要将返回类型更改为 ThreadPoolTaskExecutor 才能工作?

【问题讨论】:

  • 作为基于 Java 的配置的经验法则,尽可能具体,因为在创建应用程序上下文时检查这些配置。因此,在这种情况下,可能是未注册回调(尽管 DisposableBean 可能会起作用)。

标签: spring spring-annotations spring-async


【解决方案1】:

如果您在 ConfigurableApplicationContext 上调用 registerShutdownHook 方法,则会调用单例 bean 上的所有销毁方法

【讨论】:

  • 如果进程死了,线程池也会随之而去。我(可能)遇到的问题是应用程序的重新部署。
  • 重新部署有什么问题,您期望重新部署时的行为是什么
  • 在重新部署时,我希望 spring 上下文被销毁,并希望线程池被彻底销毁。
  • 在为所有单例bean重新部署spring容器调用destroy方法时,即使对于web应用程序,如果你想要一个客户行为,你可以配置一个实现ApplicationListener并等待ContextClosedEvent的spring bean,你可以添加一些行为关于销毁线程池
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-14
  • 2014-02-24
相关资源
最近更新 更多