Search in sources :

Example 1 with AbstractSharedListener

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

use of com.alibaba.nacos.api.config.listener.AbstractSharedListener in project spring-cloud-alibaba by alibaba.

the class NacosContextRefresher method registerNacosListener.

private void registerNacosListener(final String groupKey, final String dataKey) {
    String key = NacosPropertySourceRepository.getMapKey(dataKey, groupKey);
    Listener listener = listenerMap.computeIfAbsent(key, lst -> new AbstractSharedListener() {

        @Override
        public void innerReceive(String dataId, String group, String configInfo) {
            refreshCountIncrement();
            nacosRefreshHistory.addRefreshRecord(dataId, group, configInfo);
            // todo feature: support single refresh for listening
            applicationContext.publishEvent(new RefreshEvent(this, null, "Refresh Nacos config"));
            if (log.isDebugEnabled()) {
                log.debug(String.format("Refresh Nacos config group=%s,dataId=%s,configInfo=%s", group, dataId, configInfo));
            }
        }
    });
    try {
        configService.addListener(dataKey, groupKey, listener);
    } catch (NacosException e) {
        log.warn(String.format("register fail for nacos listener ,dataId=[%s],group=[%s]", dataKey, groupKey), e);
    }
}
Also used : AbstractSharedListener(com.alibaba.nacos.api.config.listener.AbstractSharedListener) ApplicationListener(org.springframework.context.ApplicationListener) AbstractSharedListener(com.alibaba.nacos.api.config.listener.AbstractSharedListener) Listener(com.alibaba.nacos.api.config.listener.Listener) RefreshEvent(org.springframework.cloud.endpoint.event.RefreshEvent) NacosException(com.alibaba.nacos.api.exception.NacosException)

Aggregations

AbstractSharedListener (com.alibaba.nacos.api.config.listener.AbstractSharedListener)2 Listener (com.alibaba.nacos.api.config.listener.Listener)2 NacosException (com.alibaba.nacos.api.exception.NacosException)2 ConfigChangeEvent (com.alibaba.nacos.api.config.ConfigChangeEvent)1 ConfigResponse (com.alibaba.nacos.client.config.filter.impl.ConfigResponse)1 AbstractConfigChangeListener (com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener)1 Map (java.util.Map)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 RefreshEvent (org.springframework.cloud.endpoint.event.RefreshEvent)1 ApplicationListener (org.springframework.context.ApplicationListener)1