Search in sources :

Example 26 with Listener

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

the class ClientWorkerTest method testListenerWithTenant.

@Test
public void testListenerWithTenant() 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);
    Listener listener = new AbstractListener() {

        @Override
        public void receiveConfigInfo(String configInfo) {
        }
    };
    String dataId = "a";
    String group = "b";
    clientWorker.addTenantListeners(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.removeTenantListener(dataId, group, listener);
    listeners = clientWorker.getCache(dataId, group).getListeners();
    Assert.assertEquals(0, listeners.size());
    String content = "d";
    clientWorker.addTenantListenersWithContent(dataId, group, content, Arrays.asList(listener));
    listeners = clientWorker.getCache(dataId, group).getListeners();
    Assert.assertEquals(1, listeners.size());
    Assert.assertEquals(listener, listeners.get(0));
    clientWorker.removeTenantListener(dataId, group, listener);
    listeners = clientWorker.getCache(dataId, group).getListeners();
    Assert.assertEquals(0, listeners.size());
    String tenant = "c";
    CacheData cacheData = clientWorker.addCacheDataIfAbsent(dataId, group, tenant);
    Assert.assertEquals(cacheData, clientWorker.getCache(dataId, group, tenant));
    clientWorker.removeCache(dataId, group, tenant);
    Assert.assertNull(clientWorker.getCache(dataId, group, tenant));
}
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 27 with Listener

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

the class NacosConfigServiceTest method testRemoveListener.

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

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

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

Example 28 with Listener

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

the class ClientWorker method addTenantListenersWithContent.

/**
 * Add listeners for tenant with content.
 *
 * @param dataId    dataId of data
 * @param group     group of data
 * @param content   content
 * @param listeners listeners
 * @throws NacosException nacos exception
 */
public void addTenantListenersWithContent(String dataId, String group, String content, List<? extends Listener> listeners) throws NacosException {
    group = blank2defaultGroup(group);
    String tenant = agent.getTenant();
    CacheData cache = addCacheDataIfAbsent(dataId, group, tenant);
    synchronized (cache) {
        cache.setContent(content);
        for (Listener listener : listeners) {
            cache.addListener(listener);
        }
        cache.setSyncWithServer(false);
        agent.notifyListenConfig();
    }
}
Also used : ConnectionEventListener(com.alibaba.nacos.common.remote.client.ConnectionEventListener) Listener(com.alibaba.nacos.api.config.listener.Listener)

Example 29 with Listener

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

the class ClientWorker method addTenantListeners.

/**
 * Add listeners for tenant.
 *
 * @param dataId    dataId of data
 * @param group     group of data
 * @param listeners listeners
 * @throws NacosException nacos exception
 */
public void addTenantListeners(String dataId, String group, List<? extends Listener> listeners) throws NacosException {
    group = blank2defaultGroup(group);
    String tenant = agent.getTenant();
    CacheData cache = addCacheDataIfAbsent(dataId, group, tenant);
    synchronized (cache) {
        for (Listener listener : listeners) {
            cache.addListener(listener);
        }
        cache.setSyncWithServer(false);
        agent.notifyListenConfig();
    }
}
Also used : ConnectionEventListener(com.alibaba.nacos.common.remote.client.ConnectionEventListener) Listener(com.alibaba.nacos.api.config.listener.Listener)

Example 30 with Listener

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

the class ConfigLongPoll_CITCase method test.

@Test
public void test() throws InterruptedException, NacosException {
    configService.addListener("test", "DEFAULT_GROUP", new Listener() {

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

        @Override
        public void receiveConfigInfo(String configInfo) {
            System.out.println(configInfo);
        }
    });
    configService.addListener("test-1", "DEFAULT_GROUP", new Listener() {

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

        @Override
        public void receiveConfigInfo(String configInfo) {
            System.out.println(configInfo);
        }
    });
    TimeUnit.SECONDS.sleep(10);
}
Also used : Listener(com.alibaba.nacos.api.config.listener.Listener) Executor(java.util.concurrent.Executor) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

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