use of com.alibaba.nacos.api.config.listener.Listener in project nacos by alibaba.
the class AbstractConfigAPI_CITCase method nacos_addListener_1.
/**
* @throws Exception
* @TCDescription : nacos_添加对dataId的监听,在服务端修改配置后,获取监听后的修改的配置
*/
@Test(timeout = 5 * TIME_OUT)
public void nacos_addListener_1() throws Exception {
final AtomicInteger count = new AtomicInteger(0);
final String content = "test-abc";
boolean result = iconfig.publishConfig(dataId, group, content);
Assert.assertTrue(result);
Listener ml = new Listener() {
@Override
public void receiveConfigInfo(String configInfo) {
System.out.println("receive23:" + configInfo);
count.incrementAndGet();
Assert.assertEquals(content, configInfo);
}
@Override
public Executor getExecutor() {
return null;
}
};
iconfig.addListener(dataId, group, ml);
while (count.get() == 0) {
Thread.sleep(2000);
}
Assert.assertTrue(count.get() >= 1);
iconfig.removeListener(dataId, group, ml);
}
use of com.alibaba.nacos.api.config.listener.Listener in project nacos by alibaba.
the class AbstractConfigAPI_CITCase method nacos_addListener_5.
/**
* @TCDescription : nacos_在主动拉取配置后并注册Listener,在更新配置后才触发Listener监听事件(使用特定接口)
* @TestStep : TODO Test steps
* @ExpectResult : TODO expect results
* @author chuntaojun
* @since 3.6.8
*/
@Test
public void nacos_addListener_5() throws InterruptedException, NacosException {
final AtomicInteger count = new AtomicInteger(0);
final String dataId = "nacos_addListener_5";
final String group = "nacos_addListener_5";
final String content = "test-abc";
final String newContent = "new-test-def";
boolean result = iconfig.publishConfig(dataId, group, content);
Assert.assertTrue(result);
Thread.sleep(2000);
Listener ml = new AbstractListener() {
@Override
public void receiveConfigInfo(String configInfo) {
count.incrementAndGet();
Assert.assertEquals(newContent, configInfo);
}
};
String receiveContent = iconfig.getConfigAndSignListener(dataId, group, 1000, ml);
System.out.println(receiveContent);
result = iconfig.publishConfig(dataId, group, newContent);
Assert.assertTrue(result);
Assert.assertEquals(content, receiveContent);
Thread.sleep(2000);
Assert.assertEquals(1, count.get());
iconfig.removeListener(dataId, group, ml);
}
use of com.alibaba.nacos.api.config.listener.Listener in project nacos by alibaba.
the class ConfigExample method main.
public static void main(String[] args) throws NacosException, InterruptedException {
String serverAddr = "localhost";
String dataId = "test";
String group = "DEFAULT_GROUP";
Properties properties = new Properties();
properties.put("serverAddr", serverAddr);
ConfigService configService = NacosFactory.createConfigService(properties);
String content = configService.getConfig(dataId, group, 5000);
System.out.println(content);
configService.addListener(dataId, group, new Listener() {
@Override
public void receiveConfigInfo(String configInfo) {
System.out.println("receive:" + configInfo);
}
@Override
public Executor getExecutor() {
return null;
}
});
boolean isPublishOk = configService.publishConfig(dataId, group, "content");
System.out.println(isPublishOk);
Thread.sleep(3000);
content = configService.getConfig(dataId, group, 5000);
System.out.println(content);
boolean isRemoveOk = configService.removeConfig(dataId, group);
System.out.println(isRemoveOk);
Thread.sleep(3000);
content = configService.getConfig(dataId, group, 5000);
System.out.println(content);
Thread.sleep(300000);
}
use of com.alibaba.nacos.api.config.listener.Listener in project skywalking by apache.
the class NacosConfigWatcherRegister method removeUninterestedKeys.
private void removeUninterestedKeys(final Set<String> interestedKeys) {
final String group = settings.getGroup();
final Set<String> uninterestedKeys = new HashSet<>(listenersByKey.keySet());
uninterestedKeys.removeAll(interestedKeys);
uninterestedKeys.forEach(k -> {
final Listener listener = listenersByKey.remove(k);
if (listener != null) {
configService.removeListener(k, group, listener);
}
});
}
use of com.alibaba.nacos.api.config.listener.Listener in project incubator-skywalking by apache.
the class NacosConfigWatcherRegister method registerKeyListeners.
private void registerKeyListeners(final Set<String> keys) {
final String group = settings.getGroup();
for (final String dataId : keys) {
if (listenersByKey.containsKey(dataId)) {
continue;
}
try {
listenersByKey.putIfAbsent(dataId, new Listener() {
@Override
public Executor getExecutor() {
return null;
}
@Override
public void receiveConfigInfo(String configInfo) {
onDataIdValueChanged(dataId, configInfo);
}
});
configService.addListener(dataId, group, listenersByKey.get(dataId));
// the key is newly added, read the config for the first time
final String config = configService.getConfig(dataId, group, 1000);
onDataIdValueChanged(dataId, config);
} catch (NacosException e) {
log.warn("Failed to register Nacos listener for dataId: {}", dataId);
}
}
}
Aggregations