Skip to content

Commit

Permalink
fix: change the timing of sdk context destruction. (#1124)
Browse files Browse the repository at this point in the history
  • Loading branch information
shuiqingliu authored Sep 20, 2023
1 parent 0db7b3b commit 7e7c5df
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@
import com.tencent.cloud.polaris.config.annotation.PolarisConfigAnnotationProcessor;
import com.tencent.cloud.polaris.config.condition.ConditionalOnReflectRefreshType;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.listener.PolarisConfigApplicationEventListener;
import com.tencent.cloud.polaris.config.listener.PolarisConfigChangeEventListener;
import com.tencent.cloud.polaris.config.listener.PolarisConfigRefreshOptimizationListener;
import com.tencent.cloud.polaris.config.logger.PolarisConfigLoggerApplicationListener;
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;
Expand Down Expand Up @@ -122,10 +120,5 @@ public PolarisConfigRefreshScopeAnnotationDetector polarisConfigRefreshScopeAnno
public PolarisConfigRefreshOptimizationListener polarisConfigRefreshOptimizationListener() {
return new PolarisConfigRefreshOptimizationListener();
}

@Bean
public PolarisConfigApplicationEventListener polarisContextApplicationEventListener(PolarisSDKContextManager polarisSDKContextManager) {
return new PolarisConfigApplicationEventListener(polarisSDKContextManager);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;

Expand Down Expand Up @@ -58,4 +59,9 @@ public ModifyAddress polarisConfigModifier(PolarisContextProperties properties)
public ServiceRuleManager serviceRuleManager(PolarisSDKContextManager polarisSDKContextManager) {
return new ServiceRuleManager(polarisSDKContextManager.getSDKContext(), polarisSDKContextManager.getConsumerAPI());
}

@Bean
public PolarisContextApplicationEventListener contextApplicationEventListener(PolarisSDKContextManager polarisSDKContextManager) {
return new PolarisContextApplicationEventListener(polarisSDKContextManager);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,33 @@
*
*/

package com.tencent.cloud.polaris.config.listener;
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
/**
* @author frankjlli
* @since 2023/9/18
**/
public class PolarisConfigApplicationEventListener implements ApplicationListener<ApplicationEvent> {

public class PolarisContextApplicationEventListener implements ApplicationListener<ApplicationEvent> {

private final PolarisSDKContextManager polarisSDKContextManager;

public PolarisConfigApplicationEventListener(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();
}
}

}

0 comments on commit 7e7c5df

Please sign in to comment.