From 1035b9cad72caa48eea176007f7ad6b469601727 Mon Sep 17 00:00:00 2001 From: shuiqingliu Date: Thu, 25 May 2023 17:18:52 +0800 Subject: [PATCH] fix: the polaris config relation non-daemon thread should stop when application fails to start. --- CHANGELOG.md | 1 + .../PolarisConfigAutoConfiguration.java | 7 +++ ...arisConfigNonDaemonThreadStopListener.java | 50 +++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/listener/PolarisConfigNonDaemonThreadStopListener.java diff --git a/CHANGELOG.md b/CHANGELOG.md index c216697b8..1e5dca581 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,3 +27,4 @@ - [refactor:optimize discovery properties initialization.](https://github.com/Tencent/spring-cloud-tencent/pull/1077) - [fix:upgrade spring version.](https://github.com/Tencent/spring-cloud-tencent/pull/1086) - [fix:Update README-zh.md](https://github.com/Tencent/spring-cloud-tencent/pull/1090). +- [fix:the polaris config relation non-daemon thread should stop when application fails to start.](https://github.com/Tencent/spring-cloud-tencent/pull/1100) diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigAutoConfiguration.java index be91d2287..8b9bc3845 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigAutoConfiguration.java @@ -28,10 +28,12 @@ import com.tencent.cloud.polaris.config.annotation.PolarisConfigAnnotationProces import com.tencent.cloud.polaris.config.condition.ConditionalOnReflectRefreshType; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.listener.PolarisConfigChangeEventListener; +import com.tencent.cloud.polaris.config.listener.PolarisConfigNonDaemonThreadStopListener; import com.tencent.cloud.polaris.config.listener.PolarisConfigRefreshOptimizationListener; import com.tencent.cloud.polaris.config.spring.annotation.SpringValueProcessor; import com.tencent.cloud.polaris.config.spring.property.PlaceholderHelper; import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry; +import com.tencent.cloud.polaris.context.PolarisSDKContextManager; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -62,6 +64,11 @@ public class PolarisConfigAutoConfiguration { return new PolarisConfigChangeEventListener(); } + @Bean + public PolarisConfigNonDaemonThreadStopListener polarisLongPullingStopListener(PolarisSDKContextManager polarisSDKContextManager) { + return new PolarisConfigNonDaemonThreadStopListener(polarisSDKContextManager); + } + @Bean @Primary @ConditionalOnReflectRefreshType diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/listener/PolarisConfigNonDaemonThreadStopListener.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/listener/PolarisConfigNonDaemonThreadStopListener.java new file mode 100644 index 000000000..58e3f66b6 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/listener/PolarisConfigNonDaemonThreadStopListener.java @@ -0,0 +1,50 @@ +/* + * 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.config.listener; + +import com.tencent.cloud.polaris.context.PolarisSDKContextManager; + +import org.springframework.boot.context.event.ApplicationFailedEvent; +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 PolarisConfigNonDaemonThreadStopListener implements ApplicationListener { + + private final PolarisSDKContextManager polarisSDKContextManager; + + public PolarisConfigNonDaemonThreadStopListener(PolarisSDKContextManager polarisSDKContextManager) { + this.polarisSDKContextManager = polarisSDKContextManager; + } + + @Override + public void onApplicationEvent(@NonNull ApplicationEvent event) { + if (event instanceof ApplicationFailedEvent) { + //implicit invoke 'destroy' when the spring application fails to start, in order to stop non-daemon threads. + polarisSDKContextManager.getSDKContext().destroy(); + } + } + +}