Search in sources :

Example 31 with Listener

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);
}
Also used : AbstractListener(com.alibaba.nacos.api.config.listener.AbstractListener) Listener(com.alibaba.nacos.api.config.listener.Listener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 32 with Listener

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);
}
Also used : AbstractListener(com.alibaba.nacos.api.config.listener.AbstractListener) Listener(com.alibaba.nacos.api.config.listener.Listener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractListener(com.alibaba.nacos.api.config.listener.AbstractListener) Test(org.junit.Test)

Example 33 with Listener

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);
}
Also used : ConfigService(com.alibaba.nacos.api.config.ConfigService) Listener(com.alibaba.nacos.api.config.listener.Listener) Executor(java.util.concurrent.Executor) Properties(java.util.Properties)

Example 34 with Listener

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);
        }
    });
}
Also used : Listener(com.alibaba.nacos.api.config.listener.Listener) HashSet(java.util.HashSet)

Example 35 with 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);
        }
    }
}
Also used : Listener(com.alibaba.nacos.api.config.listener.Listener) Executor(java.util.concurrent.Executor) NacosException(com.alibaba.nacos.api.exception.NacosException)

Aggregations

Listener (com.alibaba.nacos.api.config.listener.Listener)45 Executor (java.util.concurrent.Executor)22 Test (org.junit.Test)14 Properties (java.util.Properties)13 ConfigService (com.alibaba.nacos.api.config.ConfigService)11 AbstractListener (com.alibaba.nacos.api.config.listener.AbstractListener)8 NacosException (com.alibaba.nacos.api.exception.NacosException)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 ConfigFilterChainManager (com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager)4 ConnectionEventListener (com.alibaba.nacos.common.remote.client.ConnectionEventListener)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 NacosConfigProperties (com.alibaba.cloud.nacos.NacosConfigProperties)2 AbstractSharedListener (com.alibaba.nacos.api.config.listener.AbstractSharedListener)2 ConfigResponse (com.alibaba.nacos.client.config.filter.impl.ConfigResponse)2 DoorGoodConfig (com.hummer.doorgod.service.domain.configuration.DoorGoodConfig)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 List (java.util.List)2 PostConstruct (javax.annotation.PostConstruct)2