Add check to avoid any NPE from stop without start called.
This commit is contained in:
Timothy Bish 2014-11-25 15:30:13 -05:00
parent 46bc26cea5
commit 0514fcf882
1 changed files with 21 additions and 13 deletions

View File

@ -30,8 +30,8 @@ import org.slf4j.LoggerFactory;
/** /**
* A simple DiscoveryAgent that allows static configuration of the discovered * A simple DiscoveryAgent that allows static configuration of the discovered
* services. * services.
* *
* *
*/ */
public class SimpleDiscoveryAgent implements DiscoveryAgent { public class SimpleDiscoveryAgent implements DiscoveryAgent {
@ -53,33 +53,36 @@ public class SimpleDiscoveryAgent implements DiscoveryAgent {
private int connectFailures; private int connectFailures;
private long reconnectDelay = initialReconnectDelay; private long reconnectDelay = initialReconnectDelay;
private long connectTime = System.currentTimeMillis(); private long connectTime = System.currentTimeMillis();
private AtomicBoolean failed = new AtomicBoolean(false); private final AtomicBoolean failed = new AtomicBoolean(false);
public SimpleDiscoveryEvent(String service) { public SimpleDiscoveryEvent(String service) {
super(service); super(service);
} }
public SimpleDiscoveryEvent(SimpleDiscoveryEvent copy) { public SimpleDiscoveryEvent(SimpleDiscoveryEvent copy) {
super(copy); super(copy);
connectFailures = copy.connectFailures; connectFailures = copy.connectFailures;
reconnectDelay = copy.reconnectDelay; reconnectDelay = copy.reconnectDelay;
connectTime = copy.connectTime; connectTime = copy.connectTime;
failed.set(copy.failed.get()); failed.set(copy.failed.get());
} }
@Override @Override
public String toString() { public String toString() {
return "[" + serviceName + ", failed:" + failed + ", connectionFailures:" + connectFailures + "]"; return "[" + serviceName + ", failed:" + failed + ", connectionFailures:" + connectFailures + "]";
} }
} }
@Override
public void setDiscoveryListener(DiscoveryListener listener) { public void setDiscoveryListener(DiscoveryListener listener) {
this.listener = listener; this.listener = listener;
} }
@Override
public void registerService(String name) throws IOException { public void registerService(String name) throws IOException {
} }
@Override
public void start() throws Exception { public void start() throws Exception {
taskRunner = new TaskRunnerFactory(); taskRunner = new TaskRunnerFactory();
taskRunner.init(); taskRunner.init();
@ -90,10 +93,13 @@ public class SimpleDiscoveryAgent implements DiscoveryAgent {
} }
} }
@Override
public void stop() throws Exception { public void stop() throws Exception {
running.set(false); running.set(false);
taskRunner.shutdown(); if (taskRunner != null) {
taskRunner.shutdown();
}
// TODO: Should we not remove the services on the listener? // TODO: Should we not remove the services on the listener?
@ -121,6 +127,7 @@ public class SimpleDiscoveryAgent implements DiscoveryAgent {
} }
} }
@Override
public void serviceFailed(DiscoveryEvent devent) throws IOException { public void serviceFailed(DiscoveryEvent devent) throws IOException {
final SimpleDiscoveryEvent sevent = (SimpleDiscoveryEvent)devent; final SimpleDiscoveryEvent sevent = (SimpleDiscoveryEvent)devent;
@ -128,9 +135,10 @@ public class SimpleDiscoveryAgent implements DiscoveryAgent {
listener.onServiceRemove(sevent); listener.onServiceRemove(sevent);
taskRunner.execute(new Runnable() { taskRunner.execute(new Runnable() {
@Override
public void run() { public void run() {
SimpleDiscoveryEvent event = new SimpleDiscoveryEvent(sevent); SimpleDiscoveryEvent event = new SimpleDiscoveryEvent(sevent);
// We detect a failed connection attempt because the service // We detect a failed connection attempt because the service
// fails right away. // fails right away.
if (event.connectTime + minConnectTime > System.currentTimeMillis()) { if (event.connectTime + minConnectTime > System.currentTimeMillis()) {