diff --git a/activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/client/AmqpConnection.java b/activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/client/AmqpConnection.java index c6c994daaf..c252d1fb05 100644 --- a/activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/client/AmqpConnection.java +++ b/activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/client/AmqpConnection.java @@ -165,13 +165,21 @@ public class AmqpConnection extends AmqpAbstractResource implements } }); - if (connectTimeout <= 0) { - future.sync(); - } else { - future.sync(connectTimeout, TimeUnit.MILLISECONDS); - if (getEndpoint().getRemoteState() != EndpointState.ACTIVE) { - throw new IOException("Failed to connect after configured timeout."); + try { + if (connectTimeout <= 0) { + future.sync(); + } else { + future.sync(connectTimeout, TimeUnit.MILLISECONDS); + if (getEndpoint().getRemoteState() != EndpointState.ACTIVE) { + throw new IOException("Failed to connect after configured timeout."); + } } + } catch (Throwable error) { + try { + close(); + } catch (Throwable ignore) {} + + throw error; } } }