Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make ConfigChangeEvent immutable #3399

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.dubbo.configcenter.ConfigChangeEvent;
import org.apache.dubbo.configcenter.ConfigChangeType;
import org.apache.dubbo.configcenter.ConfigurationListener;
import org.apache.dubbo.configcenter.DefaultConfigChangeEvent;
import org.apache.dubbo.configcenter.DynamicConfiguration;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
Expand Down Expand Up @@ -121,7 +122,7 @@ private synchronized void init(String ruleKey) {
configuration.addListener(routerKey, this);
String rule = configuration.getConfig(routerKey);
if (rule != null) {
this.process(new ConfigChangeEvent(routerKey, rule));
this.process(new DefaultConfigChangeEvent(routerKey, rule));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.dubbo.configcenter.ConfigChangeEvent;
import org.apache.dubbo.configcenter.ConfigChangeType;
import org.apache.dubbo.configcenter.ConfigurationListener;
import org.apache.dubbo.configcenter.DefaultConfigChangeEvent;
import org.apache.dubbo.configcenter.DynamicConfiguration;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
Expand Down Expand Up @@ -236,7 +237,7 @@ public <T> void notify(List<Invoker<T>> invokers) {
application = providerApplication;
String rawRule = configuration.getConfig(key);
if (rawRule != null) {
this.process(new ConfigChangeEvent(key, rawRule));
this.process(new DefaultConfigChangeEvent(key, rawRule));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,15 @@
package org.apache.dubbo.configcenter;

/**
* Config change event.
* This interface only has getters, so it is immutable.
*
* @see ConfigChangeType
*/
public class ConfigChangeEvent {
private String key;
public interface ConfigChangeEvent {

private String value;
private ConfigChangeType changeType;
String getKey();

public ConfigChangeEvent(String key, String value) {
this(key, value, ConfigChangeType.MODIFIED);
}
String getValue();

public ConfigChangeEvent(String key, String value, ConfigChangeType changeType) {
this.key = key;
this.value = value;
this.changeType = changeType;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public ConfigChangeType getChangeType() {
return changeType;
}

public void setChangeType(ConfigChangeType changeType) {
this.changeType = changeType;
}
ConfigChangeType getChangeType();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.dubbo.configcenter;

/**
* Default implementation of Config change event.
*
* @see ConfigChangeType
*/
public class DefaultConfigChangeEvent implements ConfigChangeEvent {
private String key;

private String value;
private ConfigChangeType changeType;

public DefaultConfigChangeEvent(String key, String value) {
this(key, value, ConfigChangeType.MODIFIED);
}

public DefaultConfigChangeEvent(String key, String value, ConfigChangeType changeType) {
this.key = key;
this.value = value;
this.changeType = changeType;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public ConfigChangeType getChangeType() {
return changeType;
}

public void setChangeType(ConfigChangeType changeType) {
this.changeType = changeType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.dubbo.configcenter.ConfigChangeEvent;
import org.apache.dubbo.configcenter.ConfigChangeType;
import org.apache.dubbo.configcenter.ConfigurationListener;
import org.apache.dubbo.configcenter.DefaultConfigChangeEvent;
import org.apache.dubbo.configcenter.DynamicConfiguration;

import com.ctrip.framework.apollo.Config;
Expand Down Expand Up @@ -176,11 +177,8 @@ public void onChange(com.ctrip.framework.apollo.model.ConfigChangeEvent changeEv
return;
}

listeners.forEach(listener -> {
ConfigChangeEvent event = new ConfigChangeEvent(key, change.getNewValue(), getChangeType(change));
listener.process(event);
}
);
ConfigChangeEvent event = new DefaultConfigChangeEvent(key, change.getNewValue(), getChangeType(change));
listeners.forEach(listener -> listener.process(event));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.apache.dubbo.configcenter.ConfigChangeEvent;
import org.apache.dubbo.configcenter.ConfigChangeType;
import org.apache.dubbo.configcenter.ConfigurationListener;

import org.apache.dubbo.configcenter.DefaultConfigChangeEvent;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
Expand Down Expand Up @@ -83,7 +83,7 @@ public void childEvent(CuratorFramework aClient, TreeCacheEvent event) throws Ex
return;
}

ConfigChangeEvent configChangeEvent = new ConfigChangeEvent(key, new String(value, StandardCharsets.UTF_8), changeType);
ConfigChangeEvent configChangeEvent = new DefaultConfigChangeEvent(key, new String(value, StandardCharsets.UTF_8), changeType);
Set<ConfigurationListener> listeners = keyListeners.get(key);
if (CollectionUtils.isNotEmpty(listeners)) {
listeners.forEach(listener -> listener.process(configChangeEvent));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.dubbo.configcenter.ConfigChangeEvent;
import org.apache.dubbo.configcenter.ConfigChangeType;
import org.apache.dubbo.configcenter.ConfigurationListener;
import org.apache.dubbo.configcenter.DefaultConfigChangeEvent;
import org.apache.dubbo.configcenter.DynamicConfiguration;
import org.apache.dubbo.rpc.cluster.Configurator;
import org.apache.dubbo.rpc.cluster.configurator.parser.ConfigParser;
Expand All @@ -43,7 +44,7 @@ protected final void initWith(String key) {
dynamicConfiguration.addListener(key, this);
String rawConfig = dynamicConfiguration.getConfig(key);
if (!StringUtils.isEmpty(rawConfig)) {
process(new ConfigChangeEvent(key, rawConfig));
process(new DefaultConfigChangeEvent(key, rawConfig));
}
}

Expand Down