/** * Create a new broker connection, picking the first available address from * the list. * * If <a href="http://www.rabbitmq.com/api-guide.html#recovery">automatic connection recovery</a> * is enabled, the connection returned by this method will be {@link Recoverable}. Future * reconnection attempts will pick a random accessible address from the provided list. * * @param executor thread execution service for consumers on the connection * @param addrs an array of known broker addresses (hostname/port pairs) to try in order * @return an interface to the connection * @throws java.io.IOException if it encounters a problem * @see <a href="http://www.rabbitmq.com/api-guide.html#recovery">Automatic Recovery</a> */ public Connection newConnection(ExecutorService executor, Address[] addrs) throws IOException, TimeoutException { FrameHandlerFactory fhFactory = createFrameHandlerFactory(); ConnectionParams params = params(executor);
if (isAutomaticRecoveryEnabled()) { // see com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory#newConnection AutorecoveringConnection conn = new AutorecoveringConnection(params, fhFactory, addrs); conn.init(); return conn; } else { IOException lastException = null; for (Address addr : addrs) { try { FrameHandler handler = fhFactory.create(addr); AMQConnection conn = new AMQConnection(params, handler); conn.start(); return conn; } catch (IOException e) { lastException = e; } } throw (lastException != null) ? lastException : new IOException("failed to connect"); } }