From 934f3cea7effdff3b43ddabc9d0f03535fa44e5f Mon Sep 17 00:00:00 2001 From: Romain Manni-Bucau Date: Thu, 19 Nov 2015 07:48:56 -0800 Subject: [PATCH] release the connection even if broker communication fails --- .../apache/activemq/ActiveMQConnection.java | 50 ++++++++++--------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/activemq-client/src/main/java/org/apache/activemq/ActiveMQConnection.java b/activemq-client/src/main/java/org/apache/activemq/ActiveMQConnection.java index 50a64d8106..aecece175b 100755 --- a/activemq-client/src/main/java/org/apache/activemq/ActiveMQConnection.java +++ b/activemq-client/src/main/java/org/apache/activemq/ActiveMQConnection.java @@ -678,34 +678,36 @@ public class ActiveMQConnection implements Connection, TopicConnection, QueueCon this.activeTempDestinations.clear(); - if (isConnectionInfoSentToBroker) { - // If we announced ourselves to the broker.. Try to let the broker - // know that the connection is being shutdown. - RemoveInfo removeCommand = info.createRemoveCommand(); - removeCommand.setLastDeliveredSequenceId(lastDeliveredSequenceId); - try { - doSyncSendPacket(removeCommand, closeTimeout); - } catch (JMSException e) { - if (e.getCause() instanceof RequestTimedOutIOException) { - // expected - } else { - throw e; + try { + if (isConnectionInfoSentToBroker) { + // If we announced ourselves to the broker.. Try to let the broker + // know that the connection is being shutdown. + RemoveInfo removeCommand = info.createRemoveCommand(); + removeCommand.setLastDeliveredSequenceId(lastDeliveredSequenceId); + try { + doSyncSendPacket(removeCommand, closeTimeout); + } catch (JMSException e) { + if (e.getCause() instanceof RequestTimedOutIOException) { + // expected + } else { + throw e; + } } + doAsyncSendPacket(new ShutdownInfo()); } - doAsyncSendPacket(new ShutdownInfo()); - } + } finally { // release anyway even if previous communication fails + started.set(false); - started.set(false); - - // TODO if we move the TaskRunnerFactory to the connection - // factory - // then we may need to call - // factory.onConnectionClose(this); - if (sessionTaskRunner != null) { - sessionTaskRunner.shutdown(); + // TODO if we move the TaskRunnerFactory to the connection + // factory + // then we may need to call + // factory.onConnectionClose(this); + if (sessionTaskRunner != null) { + sessionTaskRunner.shutdown(); + } + closed.set(true); + closing.set(false); } - closed.set(true); - closing.set(false); } } } finally {