Search in sources :

Example 1 with AbstractConfigChangeListener

use of com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener in project nacos by alibaba.

the class ConfigAuth_ITCase method readWithWritePermission.

@Test
public void readWithWritePermission() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    properties.put(PropertyKeyConst.NAMESPACE, namespace1);
    properties.put(PropertyKeyConst.USERNAME, username2);
    properties.put(PropertyKeyConst.PASSWORD, password2);
    iconfig = NacosFactory.createConfigService(properties);
    final String content = "test" + System.currentTimeMillis();
    iconfig.addListener(dataId, group, new AbstractConfigChangeListener() {

        @Override
        public void receiveConfigChange(ConfigChangeEvent event) {
            ConfigChangeItem cci = event.getChangeItem("content");
            System.out.println("content:" + cci);
            if (!content.equals(cci.getNewValue())) {
                return;
            }
            latch.countDown();
        }
    });
    TimeUnit.SECONDS.sleep(3L);
    boolean result = iconfig.publishConfig(dataId, group, content);
    Assert.assertTrue(result);
    TimeUnit.SECONDS.sleep(5L);
    try {
        iconfig.getConfig(dataId, group, TIME_OUT);
        fail();
    } catch (NacosException ne) {
        Assert.assertEquals(HttpStatus.SC_FORBIDDEN, ne.getErrCode());
    }
    latch.await(5L, TimeUnit.SECONDS);
    Assert.assertTrue(latch.getCount() > 0);
}
Also used : ConfigChangeEvent(com.alibaba.nacos.api.config.ConfigChangeEvent) ConfigChangeItem(com.alibaba.nacos.api.config.ConfigChangeItem) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractConfigChangeListener(com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener) NacosException(com.alibaba.nacos.api.exception.NacosException) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with AbstractConfigChangeListener

use of com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener in project nacos by alibaba.

the class ConfigAuth_ITCase method readWithReadPermission.

@Test
public void readWithReadPermission() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    AtomicInteger ai = new AtomicInteger(0);
    properties.put(PropertyKeyConst.USERNAME, username1);
    properties.put(PropertyKeyConst.PASSWORD, password1);
    iconfig = NacosFactory.createConfigService(properties);
    final String content = "test" + System.currentTimeMillis();
    System.out.println(content);
    iconfig.addListener(dataId, group, new AbstractConfigChangeListener() {

        @Override
        public void receiveConfigChange(ConfigChangeEvent event) {
            ConfigChangeItem cci = event.getChangeItem("content");
            System.out.println("content:" + cci);
            if (!content.equals(cci.getNewValue())) {
                return;
            }
            latch.countDown();
        }
    });
    TimeUnit.SECONDS.sleep(3L);
    properties.put(PropertyKeyConst.USERNAME, username2);
    properties.put(PropertyKeyConst.PASSWORD, password2);
    ConfigService configService = NacosFactory.createConfigService(properties);
    boolean result = configService.publishConfig(dataId, group, content);
    Assert.assertTrue(result);
    TimeUnit.SECONDS.sleep(5L);
    String res = iconfig.getConfig(dataId, group, TIME_OUT);
    Assert.assertEquals(content, res);
    latch.await(5L, TimeUnit.SECONDS);
    Assert.assertEquals(0, latch.getCount());
}
Also used : ConfigService(com.alibaba.nacos.api.config.ConfigService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConfigChangeEvent(com.alibaba.nacos.api.config.ConfigChangeEvent) ConfigChangeItem(com.alibaba.nacos.api.config.ConfigChangeItem) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractConfigChangeListener(com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with AbstractConfigChangeListener

use of com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener 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);
}
Also used : AbstractConfigChangeListener(com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener) AbstractSharedListener(com.alibaba.nacos.api.config.listener.AbstractSharedListener) Listener(com.alibaba.nacos.api.config.listener.Listener) ConfigResponse(com.alibaba.nacos.client.config.filter.impl.ConfigResponse) NacosException(com.alibaba.nacos.api.exception.NacosException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) AbstractSharedListener(com.alibaba.nacos.api.config.listener.AbstractSharedListener) ConfigChangeEvent(com.alibaba.nacos.api.config.ConfigChangeEvent) AbstractConfigChangeListener(com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener) Map(java.util.Map)

Example 4 with AbstractConfigChangeListener

use of com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener in project nacos by alibaba.

the class ConfigAuth_ITCase method ReadWriteWithFullPermission.

@Test
public void ReadWriteWithFullPermission() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    AtomicInteger ai = new AtomicInteger(0);
    properties.put(PropertyKeyConst.USERNAME, username3);
    properties.put(PropertyKeyConst.PASSWORD, password3);
    iconfig = NacosFactory.createConfigService(properties);
    final String content = "test" + System.currentTimeMillis();
    iconfig.addListener(dataId, group, new AbstractConfigChangeListener() {

        @Override
        public void receiveConfigChange(ConfigChangeEvent event) {
            ConfigChangeItem cci = event.getChangeItem("content");
            System.out.println("content:" + cci);
            if (!content.equals(cci.getNewValue())) {
                return;
            }
            latch.countDown();
        }
    });
    TimeUnit.SECONDS.sleep(3L);
    boolean result = iconfig.publishConfig(dataId, group, content);
    Assert.assertTrue(result);
    TimeUnit.SECONDS.sleep(5L);
    String res = iconfig.getConfig(dataId, group, TIME_OUT);
    Assert.assertEquals(content, res);
    latch.await(5L, TimeUnit.SECONDS);
    Assert.assertEquals(0, latch.getCount());
    result = iconfig.removeConfig(dataId, group);
    Assert.assertTrue(result);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConfigChangeEvent(com.alibaba.nacos.api.config.ConfigChangeEvent) ConfigChangeItem(com.alibaba.nacos.api.config.ConfigChangeItem) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractConfigChangeListener(com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with AbstractConfigChangeListener

use of com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener in project nacos by alibaba.

the class ConfigLongPollReturnChanges_CITCase method testDelete.

@Test
public void testDelete() throws InterruptedException, NacosException {
    CountDownLatch latch = new CountDownLatch(1);
    final String dataId = "test" + System.currentTimeMillis();
    final String group = "DEFAULT_GROUP";
    final String oldData = "old data";
    boolean result = configService.publishConfig(dataId, group, oldData);
    Assert.assertTrue(result);
    configService.addListener(dataId, group, new AbstractConfigChangeListener() {

        @Override
        public void receiveConfigChange(ConfigChangeEvent event) {
            try {
                ConfigChangeItem cci = event.getChangeItem("content");
                Assert.assertEquals(oldData, cci.getOldValue());
                Assert.assertNull(cci.getNewValue());
                Assert.assertEquals(PropertyChangeType.DELETED, cci.getType());
                System.out.println(cci);
            } finally {
                latch.countDown();
            }
        }
    });
    configService.removeConfig(dataId, group);
    latch.await(10_000L, TimeUnit.MILLISECONDS);
}
Also used : ConfigChangeEvent(com.alibaba.nacos.api.config.ConfigChangeEvent) ConfigChangeItem(com.alibaba.nacos.api.config.ConfigChangeItem) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractConfigChangeListener(com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

ConfigChangeEvent (com.alibaba.nacos.api.config.ConfigChangeEvent)7 AbstractConfigChangeListener (com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener)7 ConfigChangeItem (com.alibaba.nacos.api.config.ConfigChangeItem)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 Test (org.junit.Test)6 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)6 NacosException (com.alibaba.nacos.api.exception.NacosException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ConfigService (com.alibaba.nacos.api.config.ConfigService)1 AbstractSharedListener (com.alibaba.nacos.api.config.listener.AbstractSharedListener)1 Listener (com.alibaba.nacos.api.config.listener.Listener)1 ConfigResponse (com.alibaba.nacos.client.config.filter.impl.ConfigResponse)1 Map (java.util.Map)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1