Endpoints can only 'stop()' once now. Added stopInternalActions, and stopExtraActions

This commit is contained in:
nathan 2014-11-16 15:34:43 +01:00
parent 1a088a4199
commit 95a346e815
2 changed files with 31 additions and 5 deletions

View File

@ -118,7 +118,10 @@ public abstract class EndPoint {
private List<ChannelFuture> shutdownChannelList = new ArrayList<ChannelFuture>(); private List<ChannelFuture> shutdownChannelList = new ArrayList<ChannelFuture>();
private final CountDownLatch blockUntilDone = new CountDownLatch(1); private final CountDownLatch blockUntilDone = new CountDownLatch(1);
private final CountDownLatch blockWhileShutdown = new CountDownLatch(1);
protected final Object shutdownInProgress = new Object(); protected final Object shutdownInProgress = new Object();
protected AtomicBoolean stopCalled = new AtomicBoolean(false);
protected AtomicBoolean isConnected = new AtomicBoolean(false); protected AtomicBoolean isConnected = new AtomicBoolean(false);
@ -324,8 +327,15 @@ public abstract class EndPoint {
/** /**
* Safely closes all associated resources/threads/connections * Safely closes all associated resources/threads/connections
* <p>
* Override stopExtraActions() if you want to provide extra behavior to stopping the endpoint
*/ */
public void stop() { public final void stop() {
// only permit us to "stop" once!
if (!this.stopCalled.compareAndSet(false, true)) {
return;
}
// check to make sure we are in our OWN thread, otherwise, this thread will never exit -- because it will wait indefinitely // check to make sure we are in our OWN thread, otherwise, this thread will never exit -- because it will wait indefinitely
// for itself to finish (since it blocks itself). // for itself to finish (since it blocks itself).
// This occurs when calling stop from within a listener callback. // This occurs when calling stop from within a listener callback.
@ -343,11 +353,19 @@ public abstract class EndPoint {
@Override @Override
public void run() { public void run() {
EndPoint.this.stopInThread(); EndPoint.this.stopInThread();
EndPoint.this.blockWhileShutdown.countDown();
} }
}); });
thread.setDaemon(false); thread.setDaemon(false);
thread.setName(stopTreadName); thread.setName(stopTreadName);
thread.start(); thread.start();
// we want to wait for this to finish before we continue
try {
this.blockWhileShutdown.await();
} catch (InterruptedException e) {
this.logger.error("Thread interrupted while waiting for shutdown to finish!");
}
} }
} }
@ -371,7 +389,7 @@ public abstract class EndPoint {
} }
} }
stopExtraActions(); stopEndpointInternal();
// Sometimes there might be "lingering" connections (ie, halfway though registration) that need to be closed. // Sometimes there might be "lingering" connections (ie, halfway though registration) that need to be closed.
long maxShutdownWaitTimeInMilliSeconds = EndPoint.maxShutdownWaitTimeInMilliSeconds; long maxShutdownWaitTimeInMilliSeconds = EndPoint.maxShutdownWaitTimeInMilliSeconds;
@ -415,6 +433,8 @@ public abstract class EndPoint {
} }
// when the eventloop closes, the associated selectors are ALSO closed! // when the eventloop closes, the associated selectors are ALSO closed!
stopExtraActions();
} }
// tell the blocked "bind" method that it may continue (and exit) // tell the blocked "bind" method that it may continue (and exit)
@ -422,9 +442,15 @@ public abstract class EndPoint {
} }
/** /**
* Extra actions to perform when stopping this endpoint. * Extra INTERNAL actions to perform when stopping this endpoint.
*/ */
void stopExtraActions() { void stopEndpointInternal() {
}
/**
* Extra EXTERNAL actions to perform when stopping this endpoint.
*/
public void stopExtraActions() {
} }
public String getName() { public String getName() {

View File

@ -245,7 +245,7 @@ public abstract class EndPointWithSerialization extends EndPoint {
* Extra actions to perform when stopping this endpoint. * Extra actions to perform when stopping this endpoint.
*/ */
@Override @Override
final void stopExtraActions() { final void stopEndpointInternal() {
this.connectionManager.stop(); this.connectionManager.stop();
} }