ApolloConfigurationTestProvider.java 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.skywalking.oap.server.configuration.apollo;

21 22 23
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import lombok.extern.slf4j.Slf4j;
24 25 26
import org.apache.skywalking.oap.server.configuration.api.ConfigChangeWatcher;
import org.apache.skywalking.oap.server.configuration.api.ConfigurationModule;
import org.apache.skywalking.oap.server.configuration.api.DynamicConfigurationService;
27
import org.apache.skywalking.oap.server.configuration.api.GroupConfigChangeWatcher;
28 29 30 31 32
import org.apache.skywalking.oap.server.library.module.ModuleConfig;
import org.apache.skywalking.oap.server.library.module.ModuleDefine;
import org.apache.skywalking.oap.server.library.module.ModuleProvider;
import org.apache.skywalking.oap.server.library.module.ModuleStartException;
import org.apache.skywalking.oap.server.library.module.ServiceNotProvidedException;
33

34
@Slf4j
35 36
public class ApolloConfigurationTestProvider extends ModuleProvider {
    ConfigChangeWatcher watcher;
37
    GroupConfigChangeWatcher groupWatcher;
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

    @Override
    public String name() {
        return "default";
    }

    @Override
    public Class<? extends ModuleDefine> module() {
        return ApolloConfigurationTestModule.class;
    }

    @Override
    public ModuleConfig createConfigBeanIfAbsent() {
        return new ModuleConfig() {
        };
    }

    @Override
    public void prepare() throws ServiceNotProvidedException, ModuleStartException {
        watcher = new ConfigChangeWatcher(ApolloConfigurationTestModule.NAME, this, "testKey") {
            private volatile String testValue;

            @Override
            public void notify(ConfigChangeWatcher.ConfigChangeEvent value) {
62
                log.info("ConfigChangeWatcher.ConfigChangeEvent: {}", value);
63 64 65 66 67 68 69 70 71 72 73 74
                if (EventType.DELETE.equals(value.getEventType())) {
                    testValue = null;
                } else {
                    testValue = value.getNewValue();
                }
            }

            @Override
            public String value() {
                return testValue;
            }
        };
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

        groupWatcher = new GroupConfigChangeWatcher(ApolloConfigurationTestModule.NAME, this, "testKeyGroup") {
            private Map<String, String> config = new ConcurrentHashMap<>();

            @Override
            public void notifyGroup(Map<String, ConfigChangeEvent> groupItems) {
                log.info("GroupConfigChangeWatcher.ConfigChangeEvents: {}", groupItems);
                groupItems.forEach((groupItemName, event) -> {
                    if (EventType.DELETE.equals(event.getEventType())) {
                        config.remove(groupItemName);
                    } else {
                        config.put(groupItemName, event.getNewValue());
                    }
                });
            }

            @Override
            public Map<String, String> groupItems() {
                return config;
            }
        };
96 97 98
    }

    @Override
99
    public void start() throws ServiceNotProvidedException {
100
        getManager().find(ConfigurationModule.NAME)
101 102 103
                    .provider()
                    .getService(DynamicConfigurationService.class)
                    .registerConfigChangeWatcher(watcher);
104 105 106 107 108

        getManager().find(ConfigurationModule.NAME)
                    .provider()
                    .getService(DynamicConfigurationService.class)
                    .registerConfigChangeWatcher(groupWatcher);
109 110 111
    }

    @Override
112
    public void notifyAfterCompleted() throws ServiceNotProvidedException {
113 114 115 116 117 118 119 120 121 122

    }

    @Override
    public String[] requiredModules() {
        return new String[] {
            ConfigurationModule.NAME
        };
    }
}