Search in sources :

Example 11 with Listener

use of com.alibaba.nacos.api.config.listener.Listener in project incubator-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 12 with Listener

use of com.alibaba.nacos.api.config.listener.Listener in project 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)

Example 13 with Listener

use of com.alibaba.nacos.api.config.listener.Listener in project nacos by alibaba.

the class CacheDataTest method testListener.

@Test
public void testListener() {
    ConfigFilterChainManager filter = new ConfigFilterChainManager(new Properties());
    final CacheData cacheData1 = new CacheData(filter, "name1", "key", "group", "tenant");
    Listener listener = new Listener() {

        @Override
        public Executor getExecutor() {
            return null;
        }

        @Override
        public void receiveConfigInfo(String configInfo) {
        }
    };
    cacheData1.addListener(listener);
    Assert.assertEquals(1, cacheData1.getListeners().size());
    Assert.assertEquals(listener, cacheData1.getListeners().get(0));
    cacheData1.removeListener(listener);
    Assert.assertEquals(0, cacheData1.getListeners().size());
}
Also used : Listener(com.alibaba.nacos.api.config.listener.Listener) ConfigFilterChainManager(com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager) Properties(java.util.Properties) Test(org.junit.Test)

Example 14 with Listener

use of com.alibaba.nacos.api.config.listener.Listener in project nacos by alibaba.

the class ClientWorkerTest method testAddListenerWithoutTenant.

@Test
public void testAddListenerWithoutTenant() throws NacosException {
    Properties prop = new Properties();
    ConfigFilterChainManager filter = new ConfigFilterChainManager(new Properties());
    ServerListManager agent = Mockito.mock(ServerListManager.class);
    ClientWorker clientWorker = new ClientWorker(filter, agent, prop);
    String dataId = "a";
    String group = "b";
    Listener listener = new AbstractListener() {

        @Override
        public void receiveConfigInfo(String configInfo) {
        }
    };
    clientWorker.addListeners(dataId, group, Arrays.asList(listener));
    List<Listener> listeners = clientWorker.getCache(dataId, group).getListeners();
    Assert.assertEquals(1, listeners.size());
    Assert.assertEquals(listener, listeners.get(0));
    clientWorker.removeListener(dataId, group, listener);
    listeners = clientWorker.getCache(dataId, group).getListeners();
    Assert.assertEquals(0, listeners.size());
    CacheData cacheData = clientWorker.addCacheDataIfAbsent(dataId, group);
    Assert.assertEquals(cacheData, clientWorker.getCache(dataId, group));
}
Also used : AbstractListener(com.alibaba.nacos.api.config.listener.AbstractListener) Listener(com.alibaba.nacos.api.config.listener.Listener) ConfigFilterChainManager(com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager) AbstractListener(com.alibaba.nacos.api.config.listener.AbstractListener) Properties(java.util.Properties) Test(org.junit.Test)

Example 15 with Listener

use of com.alibaba.nacos.api.config.listener.Listener in project nacos by alibaba.

the class NacosConfigServiceTest method testAddListener.

@Test
public void testAddListener() throws NacosException {
    String dataId = "1";
    String group = "2";
    Listener listener = new Listener() {

        @Override
        public Executor getExecutor() {
            return null;
        }

        @Override
        public void receiveConfigInfo(String configInfo) {
        }
    };
    nacosConfigService.addListener(dataId, group, listener);
    Mockito.verify(mockWoker, Mockito.times(1)).addTenantListeners(dataId, group, Arrays.asList(listener));
}
Also used : Listener(com.alibaba.nacos.api.config.listener.Listener) Test(org.junit.Test)

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