From 5a6885aaa503daa755b8e19ce9f8e0da3d33ff18 Mon Sep 17 00:00:00 2001 From: weihu Date: Wed, 6 Jul 2022 08:28:19 +0800 Subject: [PATCH] remove redundant code --- .../PolarisConfigAutoConfiguration.java | 4 +- .../PolarisPropertySourceAutoRefresher.java | 35 +----- .../spring/property/SpringValueRegistry.java | 25 ++--- ...arisPropertiesSourceAutoRefresherTest.java | 5 +- .../cloud/common/util/JacksonUtils.java | 4 +- .../common/util/PolarisThreadFactory.java | 102 ------------------ 6 files changed, 18 insertions(+), 157 deletions(-) delete mode 100644 spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/PolarisThreadFactory.java 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 6a9aa9896..caf93cba0 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 @@ -30,7 +30,6 @@ import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry; import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.cloud.context.refresh.ContextRefresher; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -49,11 +48,10 @@ public class PolarisConfigAutoConfiguration { public PolarisPropertySourceAutoRefresher polarisPropertySourceAutoRefresher( PolarisConfigProperties polarisConfigProperties, PolarisPropertySourceManager polarisPropertySourceManager, - ContextRefresher contextRefresher, SpringValueRegistry springValueRegistry, PlaceholderHelper placeholderHelper) { return new PolarisPropertySourceAutoRefresher(polarisConfigProperties, - polarisPropertySourceManager, contextRefresher, springValueRegistry, placeholderHelper); + polarisPropertySourceManager, springValueRegistry, placeholderHelper); } @Bean diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySourceAutoRefresher.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySourceAutoRefresher.java index 1cc324b5b..bc25c688b 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySourceAutoRefresher.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySourceAutoRefresher.java @@ -21,7 +21,6 @@ package com.tencent.cloud.polaris.config.adapter; import java.lang.reflect.Field; import java.util.Collection; import java.util.List; -import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; import com.tencent.cloud.common.util.JacksonUtils; @@ -37,7 +36,6 @@ import org.springframework.beans.BeansException; import org.springframework.beans.TypeConverter; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.boot.context.event.ApplicationReadyEvent; -import org.springframework.cloud.context.refresh.ContextRefresher; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationListener; @@ -54,36 +52,23 @@ public class PolarisPropertySourceAutoRefresher implements ApplicationListener, ApplicationContextAware { private static final Logger LOGGER = LoggerFactory.getLogger(PolarisPropertySourceAutoRefresher.class); - private final PolarisConfigProperties polarisConfigProperties; - private final PolarisPropertySourceManager polarisPropertySourceManager; - private final boolean typeConverterHasConvertIfNecessaryWithFieldParameter; - private TypeConverter typeConverter; - - private ApplicationContext applicationContext; - private final SpringValueRegistry springValueRegistry; - private ConfigurableBeanFactory beanFactory; - private final PlaceholderHelper placeholderHelper; - private final ContextRefresher contextRefresher; - private final AtomicBoolean registered = new AtomicBoolean(false); public PolarisPropertySourceAutoRefresher( PolarisConfigProperties polarisConfigProperties, PolarisPropertySourceManager polarisPropertySourceManager, - ContextRefresher contextRefresher, SpringValueRegistry springValueRegistry, PlaceholderHelper placeholderHelper) { this.polarisConfigProperties = polarisConfigProperties; this.polarisPropertySourceManager = polarisPropertySourceManager; - this.contextRefresher = contextRefresher; this.springValueRegistry = springValueRegistry; this.placeholderHelper = placeholderHelper; this.typeConverterHasConvertIfNecessaryWithFieldParameter = testTypeConverterHasConvertIfNecessaryWithFieldParameter(); @@ -92,7 +77,6 @@ public class PolarisPropertySourceAutoRefresher @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - this.applicationContext = applicationContext; this.beanFactory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory(); this.typeConverter = this.beanFactory.getTypeConverter(); } @@ -127,8 +111,6 @@ public class PolarisPropertySourceAutoRefresher polarisPropertySource.getGroup(), polarisPropertySource.getFileName()); - Map source = polarisPropertySource - .getSource(); for (String changedKey : configKVFileChangeEvent.changedKeys()) { // 1. check whether the changed key is relevant @@ -177,20 +159,9 @@ public class PolarisPropertySourceAutoRefresher value = parseJsonValue((String) value, springValue.getTargetType()); } else { - if (springValue.isField()) { - // org.springframework.beans.TypeConverter#convertIfNecessary(java.lang.Object, java.lang.Class, java.lang.reflect.Field) is available from Spring 3.2.0+ - if (typeConverterHasConvertIfNecessaryWithFieldParameter) { - value = this.typeConverter - .convertIfNecessary(value, springValue.getTargetType(), springValue.getField()); - } - else { - value = this.typeConverter.convertIfNecessary(value, springValue.getTargetType()); - } - } - else { - value = this.typeConverter.convertIfNecessary(value, springValue.getTargetType(), - springValue.getMethodParameter()); - } + value = springValue.isField() ? this.typeConverter.convertIfNecessary(value, springValue.getTargetType()) : + this.typeConverter.convertIfNecessary(value, springValue.getTargetType(), + springValue.getMethodParameter()); } return value; diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueRegistry.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueRegistry.java index ae52282e6..e8dd12909 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueRegistry.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueRegistry.java @@ -29,7 +29,7 @@ import com.google.common.collect.LinkedListMultimap; import com.google.common.collect.Maps; import com.google.common.collect.Multimap; import com.google.common.collect.Multimaps; -import com.tencent.cloud.common.util.PolarisThreadFactory; +import com.tencent.polaris.client.util.NamedThreadFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -74,19 +74,16 @@ public class SpringValueRegistry { } private void initialize() { - Executors.newSingleThreadScheduledExecutor(PolarisThreadFactory.create("SpringValueRegistry", true)) - .scheduleAtFixedRate( - new Runnable() { - @Override - public void run() { - try { - scanAndClean(); - } - catch (Throwable ex) { - logger.error(ex.getMessage(), ex); - } - } - }, CLEAN_INTERVAL_IN_SECONDS, CLEAN_INTERVAL_IN_SECONDS, TimeUnit.SECONDS); + Executors.newSingleThreadScheduledExecutor( + new NamedThreadFactory("polaris-spring-value-registry")).scheduleAtFixedRate( + () -> { + try { + scanAndClean(); + } + catch (Throwable ex) { + logger.error(ex.getMessage(), ex); + } + }, CLEAN_INTERVAL_IN_SECONDS, CLEAN_INTERVAL_IN_SECONDS, TimeUnit.SECONDS); } private void scanAndClean() { diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertiesSourceAutoRefresherTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertiesSourceAutoRefresherTest.java index ff9e405d1..40159828f 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertiesSourceAutoRefresherTest.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertiesSourceAutoRefresherTest.java @@ -62,9 +62,6 @@ public class PolarisPropertiesSourceAutoRefresherTest { private PolarisConfigProperties polarisConfigProperties; @Mock private PolarisPropertySourceManager polarisPropertySourceManager; - @Mock - private ContextRefresher contextRefresher; - @Mock private SpringValueRegistry springValueRegistry; @@ -74,7 +71,7 @@ public class PolarisPropertiesSourceAutoRefresherTest { @Test public void testConfigFileChanged() throws Exception { PolarisPropertySourceAutoRefresher refresher = new PolarisPropertySourceAutoRefresher(polarisConfigProperties, - polarisPropertySourceManager, contextRefresher, springValueRegistry, placeholderHelper); + polarisPropertySourceManager, springValueRegistry, placeholderHelper); ConfigurableApplicationContext applicationContext = mock(ConfigurableApplicationContext.class); ConfigurableListableBeanFactory beanFactory = mock(ConfigurableListableBeanFactory.class); diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/JacksonUtils.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/JacksonUtils.java index 13f6a6140..280892272 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/JacksonUtils.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/JacksonUtils.java @@ -89,8 +89,8 @@ public final class JacksonUtils { return OM.readValue(content, valueType); } catch (Exception e) { - LOG.error("Object to Json failed. {}", content, e); - throw new RuntimeException("Object to Json failed.", e); + LOG.error("json {} to class {} failed. ", content, valueType, e); + throw new RuntimeException("json to class failed.", e); } } diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/PolarisThreadFactory.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/PolarisThreadFactory.java deleted file mode 100644 index cb23adc12..000000000 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/PolarisThreadFactory.java +++ /dev/null @@ -1,102 +0,0 @@ -package com.tencent.cloud.common.util; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicLong; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - *@author : wh - *@date : 2022/6/28 09:26 - *@description: - */ -public final class PolarisThreadFactory implements ThreadFactory { - private static Logger log = LoggerFactory.getLogger(PolarisThreadFactory.class); - - private final AtomicLong threadNumber = new AtomicLong(1); - - private final String namePrefix; - - private final boolean daemon; - - private static final ThreadGroup threadGroup = new ThreadGroup("Polaris"); - - public static ThreadGroup getThreadGroup() { - return threadGroup; - } - - public static ThreadFactory create(String namePrefix, boolean daemon) { - return new PolarisThreadFactory(namePrefix, daemon); - } - - public static boolean waitAllShutdown(int timeoutInMillis) { - ThreadGroup group = getThreadGroup(); - Thread[] activeThreads = new Thread[group.activeCount()]; - group.enumerate(activeThreads); - Set alives = new HashSet<>(Arrays.asList(activeThreads)); - Set dies = new HashSet<>(); - log.info("Current ACTIVE thread count is: {}", alives.size()); - long expire = System.currentTimeMillis() + timeoutInMillis; - while (System.currentTimeMillis() < expire) { - classify(alives, dies, new ClassifyStandard() { - @Override - public boolean satisfy(Thread thread) { - return !thread.isAlive() || thread.isInterrupted() || thread.isDaemon(); - } - }); - if (alives.size() > 0) { - log.info("Alive polaris threads: {}", alives); - try { - TimeUnit.SECONDS.sleep(2); - } - catch (InterruptedException ex) { - // ignore - } - } - else { - log.info("All polaris threads are shutdown."); - return true; - } - } - log.warn("Some polaris threads are still alive but expire time has reached, alive threads: {}", - alives); - return false; - } - - - private static void classify(Set src, Set des, ClassifyStandard standard) { - Set set = new HashSet<>(); - for (T t : src) { - if (standard.satisfy(t)) { - set.add(t); - } - } - src.removeAll(set); - des.addAll(set); - } - - private PolarisThreadFactory(String namePrefix, boolean daemon) { - this.namePrefix = namePrefix; - this.daemon = daemon; - } - - public Thread newThread(Runnable runnable) { - Thread thread = new Thread(threadGroup, runnable, - threadGroup.getName() + "-" + namePrefix + "-" + threadNumber.getAndIncrement()); - thread.setDaemon(daemon); - if (thread.getPriority() != Thread.NORM_PRIORITY) { - thread.setPriority(Thread.NORM_PRIORITY); - } - return thread; - } - - private interface ClassifyStandard { - boolean satisfy(T thread); - } - -}