From 884bdffcd3acece1c7f6f8985fcd70e41f05253c Mon Sep 17 00:00:00 2001 From: shuiqingliu Date: Thu, 14 Sep 2023 11:19:05 +0800 Subject: [PATCH] fix: register a destroy hook when the application event trigger if we register a destroy hook for RemoteConfigFileRepo on the class instance , the static thread pool may be terminated by the bootstrap context closing ,Therefore,it's better to register the destroy hook when the application is prepared, or when the application fails to start, in order to properly destroy resources --- CHANGELOG.md | 1 + .../PolarisContextAutoConfiguration.java | 6 ++ ...olarisContextApplicationEventListener.java | 57 +++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/listener/PolarisContextApplicationEventListener.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 06c1f8175..6cf03617d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,4 @@ # Change Log --- +- [fix:the polaris config relation non-daemon thread should stop when application fails to start.](https://github.com/Tencent/spring-cloud-tencent/pull/1106) \ No newline at end of file diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java index 1593fdd5f..febf139fd 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java @@ -25,6 +25,7 @@ import com.tencent.cloud.polaris.context.ModifyAddress; import com.tencent.cloud.polaris.context.PolarisConfigModifier; import com.tencent.cloud.polaris.context.PolarisSDKContextManager; import com.tencent.cloud.polaris.context.ServiceRuleManager; +import com.tencent.cloud.polaris.context.listener.PolarisContextApplicationEventListener; import com.tencent.polaris.api.exception.PolarisException; import com.tencent.polaris.client.api.SDKContext; @@ -58,4 +59,9 @@ public class PolarisContextAutoConfiguration { public ServiceRuleManager serviceRuleManager(PolarisSDKContextManager polarisSDKContextManager) { return new ServiceRuleManager(polarisSDKContextManager.getSDKContext(), polarisSDKContextManager.getConsumerAPI()); } + + @Bean + public PolarisContextApplicationEventListener polarisContextApplicationEventListener(PolarisSDKContextManager polarisSDKContextManager) { + return new PolarisContextApplicationEventListener(polarisSDKContextManager); + } } diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/listener/PolarisContextApplicationEventListener.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/listener/PolarisContextApplicationEventListener.java new file mode 100644 index 000000000..0006f71b9 --- /dev/null +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/listener/PolarisContextApplicationEventListener.java @@ -0,0 +1,57 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.context.listener; + +import com.tencent.cloud.polaris.context.PolarisSDKContextManager; +import com.tencent.polaris.configuration.client.internal.RemoteConfigFileRepo; + +import org.springframework.boot.context.event.ApplicationFailedEvent; +import org.springframework.boot.context.event.ApplicationPreparedEvent; +import org.springframework.context.ApplicationEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.lang.NonNull; + +/* + * Polaris config non-daemon thread stop listener + * + * @author shuiqingliu + * @since 2023/8/29 + **/ +public class PolarisContextApplicationEventListener implements ApplicationListener { + + private final PolarisSDKContextManager polarisSDKContextManager; + + public PolarisContextApplicationEventListener(PolarisSDKContextManager polarisSDKContextManager) { + this.polarisSDKContextManager = polarisSDKContextManager; + } + + @Override + public void onApplicationEvent(@NonNull ApplicationEvent event) { + if (event instanceof ApplicationPreparedEvent) { + RemoteConfigFileRepo.registerRepoDestroyHook(polarisSDKContextManager.getSDKContext()); + } + + if (event instanceof ApplicationFailedEvent) { + RemoteConfigFileRepo.registerRepoDestroyHook(polarisSDKContextManager.getSDKContext()); + //implicit invoke 'destroy' when the spring application fails to start, in order to stop non-daemon threads. + polarisSDKContextManager.getSDKContext().destroy(); + } + } + +}