All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.cattleframework.cloud.config.nacos.resource.NacosRuleConfigResource Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cattleframework.cloud.config.nacos.resource;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;
import org.cattleframework.cloud.config.rule.processor.RuleConverter;
import org.cattleframework.cloud.config.rule.property.RulePropertyDelegate;
import org.cattleframework.cloud.config.rule.resource.BaseRuleConfigResource;
import org.cattleframework.exception.CattleException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;

/**
 * Nacos规则配置资源
 * 
 * @author orange
 *
 * @param  类的类型
 */
public class NacosRuleConfigResource extends BaseRuleConfigResource {

    private static final Logger logger = LoggerFactory.getLogger(NacosRuleConfigResource.class);

    private final Listener configListener;

    private final String groupId;

    private final String dataId;

    private final ConfigService configService;

    private final ExecutorService executor;

    public NacosRuleConfigResource(NacosConfigManager nacosConfigManager, ThreadFactory threadFactory, String groupId,
	    String dataId, long readTimeout, RulePropertyDelegate rulePropertyDelegate,
	    RuleConverter ruleConverter) {
	super(rulePropertyDelegate, ruleConverter);
	if (StringUtils.isBlank(groupId) || StringUtils.isBlank(dataId)) {
	    throw new CattleException(String.format("错误参数:groupId=[%s],dataId=[%s]", groupId, dataId));
	}
	executor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1),
		threadFactory, new ThreadPoolExecutor.DiscardOldestPolicy());
	this.groupId = groupId;
	this.dataId = dataId;
	configService = nacosConfigManager.getConfigService();
	configListener = new Listener() {

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

	    @Override
	    public void receiveConfigInfo(String configInfo) {
		updateValue(configInfo);
	    }
	};
	try {
	    String content = configService.getConfig(dataId, groupId, readTimeout);
	    if (StringUtils.isNotBlank(content)) {
		updateValue(content);
	    }
	} catch (NacosException e) {
	    logger.error(String.format("获得Nacos规则配置资源(Data ID:%s,Group:%s)时,发生异常:%s", dataId, groupId, e.getMessage()),
		    e);
	}
	try {
	    logger.debug("建立Nacos规则配置资源(dataId:{},groupId:{})", dataId, groupId);
	    configService.addListener(dataId, groupId, configListener);
	} catch (NacosException e) {
	    logger.error(String.format("建立Nacos规则配置资源(Data ID:%s,Group:%s)时,发生异常:%s", dataId, groupId, e.getMessage()),
		    e);
	}
    }

    @Override
    public void close() {
	if (configService != null) {
	    configService.removeListener(dataId, groupId, configListener);
	}
	executor.shutdownNow();
	logger.debug("关闭Nacos规则配置资源(Data ID:{},Group:{})", dataId, groupId);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy