|
9 | 9 | import org.dromara.dynamictp.core.thread.DtpExecutor; |
10 | 10 | import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; |
11 | 11 | import org.springframework.beans.BeansException; |
12 | | -import org.springframework.beans.factory.config.BeanPostProcessor; |
| 12 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 13 | +import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 14 | +import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; |
| 15 | +import org.springframework.beans.factory.support.GenericBeanDefinition; |
13 | 16 | import org.springframework.context.Lifecycle; |
14 | 17 | import org.springframework.context.SmartLifecycle; |
15 | 18 | import org.springframework.context.annotation.Bean; |
@@ -94,17 +97,25 @@ public Lifecycle taskSchedulerLifeCycle(ThreadPoolTaskScheduler taskScheduler) { |
94 | 97 | } |
95 | 98 |
|
96 | 99 | /** |
97 | | - * DynamicTp DtpLifCycle 后置处理,优雅关闭 |
| 100 | + * BeanDefinitionRegistryPostProcessor |
| 101 | + * 覆盖 DynamicTp DtpLifCycle getPhase,定制关闭顺序,实现优雅停机 |
98 | 102 | */ |
99 | 103 | @Bean |
100 | | - public BeanPostProcessor executorLifecyclePostProcess() { |
101 | | - return new BeanPostProcessor() { |
| 104 | + public BeanDefinitionRegistryPostProcessor executorLifecycleRegistryPostProcessor() { |
| 105 | + return new BeanDefinitionRegistryPostProcessor() { |
102 | 106 | @Override |
103 | | - public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { |
104 | | - if (bean instanceof DtpLifecycle) { |
105 | | - return new ExecutorLifeCycle(); |
106 | | - } |
107 | | - return bean; |
| 107 | + public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { |
| 108 | + // 删除 DynamicTp bean dtpLifecycle |
| 109 | + registry.removeBeanDefinition("dtpLifecycle"); |
| 110 | + // 注册自定义的 Lifecycle |
| 111 | + GenericBeanDefinition definition = new GenericBeanDefinition(); |
| 112 | + definition.setBeanClass(ExecutorLifeCycle.class); |
| 113 | + registry.registerBeanDefinition("dtpLifecycle", definition); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { |
| 118 | + |
108 | 119 | } |
109 | 120 | }; |
110 | 121 | } |
|
0 commit comments