Search in sources :

Example 21 with Listener

use of com.alibaba.nacos.api.config.listener.Listener 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 22 with Listener

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

the class ClientWorker method addListeners.

/**
 * Add listeners for data.
 *
 * @param dataId    dataId of data
 * @param group     group of data
 * @param listeners listeners
 */
public void addListeners(String dataId, String group, List<? extends Listener> listeners) {
    group = blank2defaultGroup(group);
    CacheData cache = addCacheDataIfAbsent(dataId, group);
    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 23 with Listener

use of com.alibaba.nacos.api.config.listener.Listener in project dynamic-threadpool by acmenlt.

the class NacosCloudRefresherHandler method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    Map<String, String> nacosConfig = bootstrapCoreProperties.getNacos();
    nacosConfigManager.getConfigService().addListener(nacosConfig.get("data-id"), nacosConfig.get("group"), new Listener() {

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

        @Override
        public void receiveConfigInfo(String configInfo) {
            dynamicRefresh(configInfo);
        }
    });
}
Also used : Listener(com.alibaba.nacos.api.config.listener.Listener) Executor(java.util.concurrent.Executor)

Example 24 with Listener

use of com.alibaba.nacos.api.config.listener.Listener in project dynamic-threadpool by acmenlt.

the class NacosRefresherHandler method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    Map<String, String> nacosConfig = bootstrapCoreProperties.getNacos();
    configService.addListener(nacosConfig.get("data-id"), nacosConfig.get("group"), new Listener() {

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

        @Override
        public void receiveConfigInfo(String configInfo) {
            dynamicRefresh(configInfo);
        }
    });
}
Also used : Listener(com.alibaba.nacos.api.config.listener.Listener) Executor(java.util.concurrent.Executor)

Example 25 with Listener

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

the class CacheDataTest method testCheckListenerMd5.

@Test
public void testCheckListenerMd5() {
    ConfigFilterChainManager filter = new ConfigFilterChainManager(new Properties());
    final CacheData data = new CacheData(filter, "name1", "key", "group", "tenant");
    final List<String> list = new ArrayList<>();
    Listener listener = new Listener() {

        @Override
        public Executor getExecutor() {
            return Runnable::run;
        }

        @Override
        public void receiveConfigInfo(String configInfo) {
            list.add(configInfo);
        }
    };
    data.addListener(listener);
    data.checkListenerMd5();
    Assert.assertTrue(data.checkListenersMd5Consistent());
    Assert.assertEquals(0, list.size());
    data.setContent("new");
    Assert.assertFalse(data.checkListenersMd5Consistent());
    data.checkListenerMd5();
    Assert.assertEquals(1, list.size());
    Assert.assertEquals("new", list.get(0));
}
Also used : Listener(com.alibaba.nacos.api.config.listener.Listener) ConfigFilterChainManager(com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager) ArrayList(java.util.ArrayList) Properties(java.util.Properties) 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