提交 d0e86cde 编写于 作者: A allenxwang

Merge pull request #73 from allenxwang/cp

Added the functionality to notify listeners when server list is changed from BaseLoadBalancer.
......@@ -23,6 +23,7 @@ import java.util.Comparator;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
......@@ -31,6 +32,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.ImmutableList;
import com.netflix.client.ClientFactory;
import com.netflix.client.IClientConfigAware;
import com.netflix.client.PrimeConnections;
......@@ -92,6 +94,8 @@ public class BaseLoadBalancer extends AbstractLoadBalancer implements
private volatile boolean enablePrimingConnections = false;
private IClientConfig config;
private List<ServerListChangeListener> changeListeners = new CopyOnWriteArrayList<ServerListChangeListener>();
/**
* Default constructor which sets name as "default", sets null ping, and
......@@ -203,6 +207,14 @@ public class BaseLoadBalancer extends AbstractLoadBalancer implements
init();
}
public void addServerListChangeListener(ServerListChangeListener listener) {
changeListeners.add(listener);
}
public void removeServerListChangeListener(ServerListChangeListener listener) {
changeListeners.remove(listener);
}
public IClientConfig getClientConfig() {
return config;
}
......@@ -468,6 +480,17 @@ public class BaseLoadBalancer extends AbstractLoadBalancer implements
boolean listChanged = false;
if (!allServerList.equals(allServers)) {
listChanged = true;
if (changeListeners != null && changeListeners.size() > 0) {
List<Server> oldList = ImmutableList.copyOf(allServerList);
List<Server> newList = ImmutableList.copyOf(allServers);
for (ServerListChangeListener l: changeListeners) {
try {
l.serverListChanged(oldList, newList);
} catch (Throwable e) {
logger.error("Error invoking server list change listener", e);
}
}
}
}
if (isEnablePrimingConnections()) {
for (Server server : allServers) {
......
/*
*
* Copyright 2014 Netflix, Inc.
*
* 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 com.netflix.loadbalancer;
import java.util.List;
public interface ServerListChangeListener {
/**
* Invoked by {@link BaseLoadBalancer} when server list is changed
*/
public void serverListChanged(List<Server> oldList, List<Server> newList);
}
/*
*
* Copyright 2014 Netflix, Inc.
*
* 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 com.netflix.loadbalancer;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Test;
import com.google.common.collect.Lists;
public class ServerListChangeListenerTest {
private volatile List<Server> oldList;
private volatile List<Server> newList;
@Test
public void testListener() {
BaseLoadBalancer lb = new BaseLoadBalancer();
lb.addServerListChangeListener(new ServerListChangeListener() {
@Override
public void serverListChanged(List<Server> oldList, List<Server> newList) {
ServerListChangeListenerTest.this.oldList = oldList;
ServerListChangeListenerTest.this.newList = newList;
}
});
List<Server> list1 = Lists.newArrayList(new Server("www.google.com:80"), new Server("www.netflix.com:80"));
List<Server> list2 = Lists.newArrayList(new Server("www.microsoft.com:80"), new Server("www.facebook.com:80"));
lb.setServersList(list1);
assertTrue(oldList.isEmpty());
assertEquals(list1, newList);
lb.setServersList(list2);
assertEquals(list1, oldList);
assertEquals(list2, newList);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册