Skip to content
Snippets Groups Projects
Unverified Commit 80573c9d authored by 曹华栋's avatar 曹华栋 Committed by GitHub
Browse files

optimize: optimize fileListener to decrease cpu time usage (#3222)

parent ce41bce9
Branches
Tags
No related merge requests found
...@@ -20,6 +20,7 @@ import java.io.UnsupportedEncodingException; ...@@ -20,6 +20,7 @@ import java.io.UnsupportedEncodingException;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
...@@ -73,6 +74,8 @@ public class FileConfiguration extends AbstractConfiguration { ...@@ -73,6 +74,8 @@ public class FileConfiguration extends AbstractConfiguration {
private final String name; private final String name;
private final FileListener fileListener = new FileListener();
private final boolean allowDynamicRefresh; private final boolean allowDynamicRefresh;
/** /**
...@@ -230,8 +233,7 @@ public class FileConfiguration extends AbstractConfiguration { ...@@ -230,8 +233,7 @@ public class FileConfiguration extends AbstractConfiguration {
listenedConfigMap.put(dataId, ConfigurationFactory.getInstance().getConfig(dataId)); listenedConfigMap.put(dataId, ConfigurationFactory.getInstance().getConfig(dataId));
// Start config change listener for the dataId. // Start config change listener for the dataId.
FileListener fileListener = new FileListener(dataId, listener); fileListener.addListener(dataId, listener);
fileListener.onProcessEvent(new ConfigurationChangeEvent());
} }
@Override @Override
...@@ -333,8 +335,8 @@ public class FileConfiguration extends AbstractConfiguration { ...@@ -333,8 +335,8 @@ public class FileConfiguration extends AbstractConfiguration {
*/ */
class FileListener implements ConfigurationChangeListener { class FileListener implements ConfigurationChangeListener {
private final String dataId; private final Map<String, Set<ConfigurationChangeListener>> dataIdMap = new HashMap<>();
private final ConfigurationChangeListener listener;
private final ExecutorService executor = new ThreadPoolExecutor(CORE_LISTENER_THREAD, MAX_LISTENER_THREAD, 0L, private final ExecutorService executor = new ThreadPoolExecutor(CORE_LISTENER_THREAD, MAX_LISTENER_THREAD, 0L,
TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(),
new NamedThreadFactory("fileListener", MAX_LISTENER_THREAD)); new NamedThreadFactory("fileListener", MAX_LISTENER_THREAD));
...@@ -342,17 +344,22 @@ public class FileConfiguration extends AbstractConfiguration { ...@@ -342,17 +344,22 @@ public class FileConfiguration extends AbstractConfiguration {
/** /**
* Instantiates a new FileListener. * Instantiates a new FileListener.
* *
* @param dataId the data id
* @param listener the listener
*/ */
public FileListener(String dataId, ConfigurationChangeListener listener) { FileListener() {}
this.dataId = dataId;
this.listener = listener; public synchronized void addListener(String dataId, ConfigurationChangeListener listener) {
// only the first time add listener will trigger on process event
if (dataIdMap.isEmpty()) {
fileListener.onProcessEvent(new ConfigurationChangeEvent());
}
dataIdMap .computeIfAbsent(dataId, value -> new HashSet<>()).add(listener);
} }
@Override @Override
public void onChangeEvent(ConfigurationChangeEvent event) { public void onChangeEvent(ConfigurationChangeEvent event) {
while (true) { while (true) {
for (String dataId : dataIdMap.keySet()) {
try { try {
String currentConfig = String currentConfig =
ConfigurationFactory.getInstance().getLatestConfig(dataId, null, DEFAULT_CONFIG_TIMEOUT); ConfigurationFactory.getInstance().getLatestConfig(dataId, null, DEFAULT_CONFIG_TIMEOUT);
...@@ -361,11 +368,15 @@ public class FileConfiguration extends AbstractConfiguration { ...@@ -361,11 +368,15 @@ public class FileConfiguration extends AbstractConfiguration {
if (ObjectUtils.notEqual(currentConfig, oldConfig)) { if (ObjectUtils.notEqual(currentConfig, oldConfig)) {
listenedConfigMap.put(dataId, currentConfig); listenedConfigMap.put(dataId, currentConfig);
event.setDataId(dataId).setNewValue(currentConfig).setOldValue(oldConfig); event.setDataId(dataId).setNewValue(currentConfig).setOldValue(oldConfig);
for (ConfigurationChangeListener listener : dataIdMap.get(dataId)) {
listener.onChangeEvent(event); listener.onChangeEvent(event);
} }
} }
}
} catch (Exception exx) { } catch (Exception exx) {
LOGGER.error("fileListener execute error:{}", exx.getMessage()); LOGGER.error("fileListener execute error, dataId :{}", dataId, exx);
}
} }
try { try {
Thread.sleep(LISTENER_CONFIG_INTERVAL); Thread.sleep(LISTENER_CONFIG_INTERVAL);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment