remove redundant code

pull/326/head
weihu 2 years ago
parent b5ca59cf46
commit 5a6885aaa5

@ -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

@ -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<ApplicationReadyEvent>, 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<String, Object> 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;

@ -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() {

@ -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);

@ -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);
}
}

@ -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<Thread> alives = new HashSet<>(Arrays.asList(activeThreads));
Set<Thread> 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<Thread>() {
@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 <T> void classify(Set<T> src, Set<T> des, ClassifyStandard<T> standard) {
Set<T> 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<T> {
boolean satisfy(T thread);
}
}
Loading…
Cancel
Save