Search in sources :

Example 1 with CloudConfigObserverEntity

use of org.noear.solon.cloud.service.CloudConfigObserverEntity in project solon by noear.

the class CloudConfigServiceWaterImp method attention.

@Override
public void attention(String group, String key, CloudConfigHandler observer) {
    if (observerMap.containsKey(observer)) {
        return;
    }
    if (Utils.isEmpty(group)) {
        group = Solon.cfg().appGroup();
        if (Utils.isEmpty(group)) {
            group = DEFAULT_GROUP;
        }
    }
    CloudConfigObserverEntity entity = new CloudConfigObserverEntity(group, key, observer);
    observerMap.put(observer, entity);
}
Also used : CloudConfigObserverEntity(org.noear.solon.cloud.service.CloudConfigObserverEntity)

Example 2 with CloudConfigObserverEntity

use of org.noear.solon.cloud.service.CloudConfigObserverEntity in project solon by noear.

the class CloudConfigServiceZkImp method attention.

@Override
public void attention(String group, String key, CloudConfigHandler observer) {
    if (observerMap.containsKey(observer)) {
        return;
    }
    if (Utils.isEmpty(group)) {
        group = Solon.cfg().appGroup();
    }
    CloudConfigObserverEntity entity = new CloudConfigObserverEntity(group, key, observer);
    observerMap.put(observer, entity);
    client.watchNodeData(String.format("%s/%s/%s", PATH_ROOT, group, key), event -> {
        entity.handle(pull(entity.group, entity.key));
    });
}
Also used : CloudConfigObserverEntity(org.noear.solon.cloud.service.CloudConfigObserverEntity)

Example 3 with CloudConfigObserverEntity

use of org.noear.solon.cloud.service.CloudConfigObserverEntity in project solon by noear.

the class CloudConfigServiceConsulImpl method run0.

private void run0() {
    Map<String, Config> cfgTmp = new HashMap<>();
    for (Map.Entry<CloudConfigHandler, CloudConfigObserverEntity> kv : observerMap.entrySet()) {
        CloudConfigObserverEntity entity = kv.getValue();
        String cfgKey = entity.group + "/" + entity.key;
        GetValue newV = real.getKVValue(cfgKey, token).getValue();
        if (newV != null) {
            Config oldV = configMap.get(cfgKey);
            if (oldV == null) {
                oldV = new Config(entity.group, entity.key, newV.getDecodedValue(), newV.getModifyIndex());
                configMap.put(cfgKey, oldV);
                cfgTmp.put(cfgKey, oldV);
            } else if (newV.getModifyIndex() > oldV.version()) {
                oldV.updateValue(newV.getDecodedValue(), newV.getModifyIndex());
                cfgTmp.put(cfgKey, oldV);
            }
        }
    }
    for (Config cfg2 : cfgTmp.values()) {
        observerMap.forEach((k, v) -> {
            if (cfg2.group().equals(v.group) && cfg2.key().equals(v.key)) {
                v.handle(cfg2);
            }
        });
    }
}
Also used : CloudConfigObserverEntity(org.noear.solon.cloud.service.CloudConfigObserverEntity) HashMap(java.util.HashMap) Config(org.noear.solon.cloud.model.Config) CloudConfigHandler(org.noear.solon.cloud.CloudConfigHandler) HashMap(java.util.HashMap) Map(java.util.Map) GetValue(com.ecwid.consul.v1.kv.model.GetValue)

Example 4 with CloudConfigObserverEntity

use of org.noear.solon.cloud.service.CloudConfigObserverEntity in project solon by noear.

the class CloudConfigServiceNacosImp method attention.

/**
 * 关注配置
 */
@Override
public void attention(String group, String key, CloudConfigHandler observer) {
    if (observerMap.containsKey(observer)) {
        return;
    }
    if (Utils.isEmpty(group)) {
        group = Solon.cfg().appGroup();
    }
    CloudConfigObserverEntity entity = new CloudConfigObserverEntity(group, key, observer);
    observerMap.put(observer, entity);
    try {
        group = groupReview(group);
        real.addListener(key, group, new Listener() {

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

            @Override
            public void receiveConfigInfo(String value) {
                entity.handle(new Config(entity.group, entity.key, value, 0));
            }
        });
    } catch (NacosException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : CloudConfigObserverEntity(org.noear.solon.cloud.service.CloudConfigObserverEntity) Listener(com.alibaba.nacos.api.config.listener.Listener) Executor(java.util.concurrent.Executor) Config(org.noear.solon.cloud.model.Config) NacosException(com.alibaba.nacos.api.exception.NacosException)

Aggregations

CloudConfigObserverEntity (org.noear.solon.cloud.service.CloudConfigObserverEntity)4 Config (org.noear.solon.cloud.model.Config)2 Listener (com.alibaba.nacos.api.config.listener.Listener)1 NacosException (com.alibaba.nacos.api.exception.NacosException)1 GetValue (com.ecwid.consul.v1.kv.model.GetValue)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Executor (java.util.concurrent.Executor)1 CloudConfigHandler (org.noear.solon.cloud.CloudConfigHandler)1