diff options
| author | Aidan Skinner <aidan@apache.org> | 2008-02-19 16:53:57 +0000 |
|---|---|---|
| committer | Aidan Skinner <aidan@apache.org> | 2008-02-19 16:53:57 +0000 |
| commit | 45072deb936db16404f3746eeb9dcb74e4ecd3df (patch) | |
| tree | 5d00f77fc816ec19d80a3b5b2ec54ced54c462e4 /java/client | |
| parent | 321153d64eccde228d4dd8d9c9833e2b259d8c05 (diff) | |
| download | qpid-python-45072deb936db16404f3746eeb9dcb74e4ecd3df.tar.gz | |
Qpid-594: make AMQConnection listen for exceptions that are thrown asynchronously in it's constructor and do something appropriate with them
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/branches/M2.1@629158 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/client')
3 files changed, 55 insertions, 3 deletions
diff --git a/java/client/src/main/java/org/apache/qpid/client/AMQConnection.java b/java/client/src/main/java/org/apache/qpid/client/AMQConnection.java index c9928a084e..79d92f7705 100644 --- a/java/client/src/main/java/org/apache/qpid/client/AMQConnection.java +++ b/java/client/src/main/java/org/apache/qpid/client/AMQConnection.java @@ -39,6 +39,7 @@ import org.apache.qpid.jms.Connection; import org.apache.qpid.jms.ConnectionListener; import org.apache.qpid.jms.ConnectionURL; import org.apache.qpid.jms.FailoverPolicy; +import org.apache.qpid.protocol.AMQConstant; import org.apache.qpid.url.URLSyntaxException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -233,6 +234,26 @@ public class AMQConnection extends Closeable implements Connection, QueueConnect public AMQConnection(ConnectionURL connectionURL, SSLConfiguration sslConfig) throws AMQException { + final ArrayList<JMSException> exceptions = new ArrayList<JMSException>(); + + class Listener implements ExceptionListener + { + public void onException(JMSException e) + { + exceptions.add(e); + } + } + + try + { + setExceptionListener(new Listener()); + } + catch (JMSException e) + { + // Shouldn't happen + throw new AMQException(null, null, e); + } + if (_logger.isInfoEnabled()) { _logger.info("Connection:" + connectionURL); @@ -289,8 +310,6 @@ public class AMQConnection extends Closeable implements Connection, QueueConnect try { makeBrokerConnection(_failoverPolicy.getNextBrokerDetails()); - lastException = null; - _connected = true; } catch (Exception e) { @@ -318,7 +337,23 @@ public class AMQConnection extends Closeable implements Connection, QueueConnect { String message = null; - if (lastException != null) + if (exceptions.size() > 0) + { + JMSException e = exceptions.get(exceptions.size() - 1); + int code = -1; + try + { + code = new Integer(e.getErrorCode()).intValue(); + } + catch (NumberFormatException nfe) + { + // Ignore this, we have some error codes and messages swapped around + } + + throw new AMQConnectionFailureException(AMQConstant.getConstant(code), + e.getMessage(), e); + } + else if (lastException != null) { if (lastException.getCause() != null) { diff --git a/java/client/src/main/java/org/apache/qpid/client/protocol/AMQProtocolHandler.java b/java/client/src/main/java/org/apache/qpid/client/protocol/AMQProtocolHandler.java index 8a1e78d2e0..f70c1faa84 100644 --- a/java/client/src/main/java/org/apache/qpid/client/protocol/AMQProtocolHandler.java +++ b/java/client/src/main/java/org/apache/qpid/client/protocol/AMQProtocolHandler.java @@ -361,6 +361,7 @@ public class AMQProtocolHandler extends IoHandlerAdapter // this will attemp failover sessionClosed(session); + _connection.exceptionReceived(cause); } else { diff --git a/java/client/src/test/java/org/apache/qpid/test/unit/client/connection/ConnectionTest.java b/java/client/src/test/java/org/apache/qpid/test/unit/client/connection/ConnectionTest.java index 56394fee27..7103397ad4 100644 --- a/java/client/src/test/java/org/apache/qpid/test/unit/client/connection/ConnectionTest.java +++ b/java/client/src/test/java/org/apache/qpid/test/unit/client/connection/ConnectionTest.java @@ -165,6 +165,22 @@ public class ConnectionTest extends TestCase } } + public void testUnresolvedVirtualHostFailure() throws Exception + { + try + { + new AMQConnection("amqp://guest:guest@clientid/rubbishhost?brokerlist='" + _broker + "?retries='0''"); + fail("Connection should not be established"); + } + catch (AMQException amqe) + { + if (!(amqe instanceof AMQConnectionFailureException)) + { + fail("Correct exception not thrown. Excpected 'AMQConnectionFailureException' got: " + amqe); + } + } + } + public void testClientIdCannotBeChanged() throws Exception { Connection connection = new AMQConnection(_broker, "guest", "guest", |
