avoid deadlock on stop()

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@639531 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Hiram R. Chirino 2008-03-21 03:29:33 +00:00
parent 47183aeeaa
commit 0302ffded7

View File

@ -844,9 +844,10 @@ public class TransportConnection implements Connection, Task, CommandVisitor {
return manageable;
}
public synchronized void start() throws Exception {
public void start() throws Exception {
starting = true;
try {
synchronized(this) {
transport.start();
if (taskRunnerFactory != null) {
@ -859,6 +860,7 @@ public class TransportConnection implements Connection, Task, CommandVisitor {
active = true;
this.processDispatch(connector.getBrokerInfo());
connector.onStarted(this);
}
} catch (Exception e) {
// Force clean up on an error starting up.
stop();
@ -875,6 +877,13 @@ public class TransportConnection implements Connection, Task, CommandVisitor {
}
}
public void stop() throws Exception {
synchronized (this) {
pendingStop = true;
if (starting) {
LOG.debug("stop() called in the middle of start(). Delaying...");
return;
}
}
stopAsync();
while( !stopped.await(5, TimeUnit.SECONDS) ) {
LOG.info("The connection to '" + transport.getRemoteAddress()+ "' is taking a long time to shutdown.");
@ -884,13 +893,6 @@ public class TransportConnection implements Connection, Task, CommandVisitor {
public void stopAsync() {
// If we're in the middle of starting
// then go no further... for now.
synchronized (this) {
pendingStop = true;
if (starting) {
LOG.debug("stop() called in the middle of start(). Delaying...");
return;
}
}
if (stopping.compareAndSet(false, true)) {
// Let all the connection contexts know we are shutting down
@ -947,10 +949,7 @@ public class TransportConnection implements Connection, Task, CommandVisitor {
}
if (taskRunner != null) {
taskRunner.wakeup();
// Give it a change to stop gracefully.
dispatchStoppedLatch.await(5, TimeUnit.SECONDS);
taskRunner.shutdown();
taskRunner.shutdown(1);
}
active = false;