use of com.alibaba.nacos.api.config.listener.Listener in project nacos by alibaba.
the class CacheData method safeNotifyListener.
private void safeNotifyListener(final String dataId, final String group, final String content, final String type, final String md5, final String encryptedDataKey, final ManagerListenerWrap listenerWrap) {
final Listener listener = listenerWrap.listener;
if (listenerWrap.inNotifying) {
LOGGER.warn("[{}] [notify-currentSkip] dataId={}, group={}, md5={}, listener={}, listener is not finish yet,will try next time.", name, dataId, group, md5, listener);
return;
}
Runnable job = () -> {
long start = System.currentTimeMillis();
ClassLoader myClassLoader = Thread.currentThread().getContextClassLoader();
ClassLoader appClassLoader = listener.getClass().getClassLoader();
try {
if (listener instanceof AbstractSharedListener) {
AbstractSharedListener adapter = (AbstractSharedListener) listener;
adapter.fillContext(dataId, group);
LOGGER.info("[{}] [notify-context] dataId={}, group={}, md5={}", name, dataId, group, md5);
}
// Before executing the callback, set the thread classloader to the classloader of
// the specific webapp to avoid exceptions or misuses when calling the spi interface in
// the callback method (this problem occurs only in multi-application deployment).
Thread.currentThread().setContextClassLoader(appClassLoader);
ConfigResponse cr = new ConfigResponse();
cr.setDataId(dataId);
cr.setGroup(group);
cr.setContent(content);
cr.setEncryptedDataKey(encryptedDataKey);
configFilterChainManager.doFilter(null, cr);
String contentTmp = cr.getContent();
listenerWrap.inNotifying = true;
listener.receiveConfigInfo(contentTmp);
// compare lastContent and content
if (listener instanceof AbstractConfigChangeListener) {
Map data = ConfigChangeHandler.getInstance().parseChangeData(listenerWrap.lastContent, content, type);
ConfigChangeEvent event = new ConfigChangeEvent(data);
((AbstractConfigChangeListener) listener).receiveConfigChange(event);
listenerWrap.lastContent = content;
}
listenerWrap.lastCallMd5 = md5;
LOGGER.info("[{}] [notify-ok] dataId={}, group={}, md5={}, listener={} ,cost={} millis.", name, dataId, group, md5, listener, (System.currentTimeMillis() - start));
} catch (NacosException ex) {
LOGGER.error("[{}] [notify-error] dataId={}, group={}, md5={}, listener={} errCode={} errMsg={}", name, dataId, group, md5, listener, ex.getErrCode(), ex.getErrMsg());
} catch (Throwable t) {
LOGGER.error("[{}] [notify-error] dataId={}, group={}, md5={}, listener={} tx={}", name, dataId, group, md5, listener, t.getCause());
} finally {
listenerWrap.inNotifying = false;
Thread.currentThread().setContextClassLoader(myClassLoader);
}
};
final long startNotify = System.currentTimeMillis();
try {
if (null != listener.getExecutor()) {
listener.getExecutor().execute(job);
} else {
try {
INTERNAL_NOTIFIER.submit(job);
} catch (RejectedExecutionException rejectedExecutionException) {
LOGGER.warn("[{}] [notify-blocked] dataId={}, group={}, md5={}, listener={}, no available internal notifier,will sync notifier ", name, dataId, group, md5, listener);
job.run();
} catch (Throwable throwable) {
LOGGER.error("[{}] [notify-blocked] dataId={}, group={}, md5={}, listener={}, submit internal async task fail,throwable= ", name, dataId, group, md5, listener, throwable);
job.run();
}
}
} catch (Throwable t) {
LOGGER.error("[{}] [notify-error] dataId={}, group={}, md5={}, listener={} throwable={}", name, dataId, group, md5, listener, t.getCause());
}
final long finishNotify = System.currentTimeMillis();
LOGGER.info("[{}] [notify-listener] time cost={}ms in ClientWorker, dataId={}, group={}, md5={}, listener={} ", name, (finishNotify - startNotify), dataId, group, md5, listener);
}
use of com.alibaba.nacos.api.config.listener.Listener in project nacos by alibaba.
the class ClientWorker method addListeners.
/**
* Add listeners for data.
*
* @param dataId dataId of data
* @param group group of data
* @param listeners listeners
*/
public void addListeners(String dataId, String group, List<? extends Listener> listeners) {
group = blank2defaultGroup(group);
CacheData cache = addCacheDataIfAbsent(dataId, group);
synchronized (cache) {
for (Listener listener : listeners) {
cache.addListener(listener);
}
cache.setSyncWithServer(false);
agent.notifyListenConfig();
}
}
use of com.alibaba.nacos.api.config.listener.Listener in project dynamic-threadpool by acmenlt.
the class NacosCloudRefresherHandler method afterPropertiesSet.
@Override
public void afterPropertiesSet() throws Exception {
Map<String, String> nacosConfig = bootstrapCoreProperties.getNacos();
nacosConfigManager.getConfigService().addListener(nacosConfig.get("data-id"), nacosConfig.get("group"), new Listener() {
@Override
public Executor getExecutor() {
return dynamicRefreshExecutorService;
}
@Override
public void receiveConfigInfo(String configInfo) {
dynamicRefresh(configInfo);
}
});
}
use of com.alibaba.nacos.api.config.listener.Listener in project dynamic-threadpool by acmenlt.
the class NacosRefresherHandler method afterPropertiesSet.
@Override
public void afterPropertiesSet() throws Exception {
Map<String, String> nacosConfig = bootstrapCoreProperties.getNacos();
configService.addListener(nacosConfig.get("data-id"), nacosConfig.get("group"), new Listener() {
@Override
public Executor getExecutor() {
return dynamicRefreshExecutorService;
}
@Override
public void receiveConfigInfo(String configInfo) {
dynamicRefresh(configInfo);
}
});
}
use of com.alibaba.nacos.api.config.listener.Listener in project nacos by alibaba.
the class CacheDataTest method testCheckListenerMd5.
@Test
public void testCheckListenerMd5() {
ConfigFilterChainManager filter = new ConfigFilterChainManager(new Properties());
final CacheData data = new CacheData(filter, "name1", "key", "group", "tenant");
final List<String> list = new ArrayList<>();
Listener listener = new Listener() {
@Override
public Executor getExecutor() {
return Runnable::run;
}
@Override
public void receiveConfigInfo(String configInfo) {
list.add(configInfo);
}
};
data.addListener(listener);
data.checkListenerMd5();
Assert.assertTrue(data.checkListenersMd5Consistent());
Assert.assertEquals(0, list.size());
data.setContent("new");
Assert.assertFalse(data.checkListenersMd5Consistent());
data.checkListenerMd5();
Assert.assertEquals(1, list.size());
Assert.assertEquals("new", list.get(0));
}
Aggregations