@@ -438,10 +438,22 @@ protected FrameHandler createFrameHandler(Address addr)
438438
439439 String hostName = addr .getHost ();
440440 int portNumber = portOrDefault (addr .getPort ());
441- Socket socket = factory .createSocket ();
442- configureSocket (socket );
443- socket .connect (new InetSocketAddress (hostName , portNumber ), connectionTimeout );
444- return createFrameHandler (socket );
441+ Socket socket = null ;
442+ try {
443+ socket = factory .createSocket ();
444+ configureSocket (socket );
445+ socket .connect (new InetSocketAddress (hostName , portNumber ),
446+ connectionTimeout );
447+ return createFrameHandler (socket );
448+ } catch (IOException ioe ) {
449+ quietTrySocketClose (socket );
450+ throw ioe ;
451+ }
452+ }
453+
454+ private static void quietTrySocketClose (Socket socket ) {
455+ if (socket != null )
456+ try { socket .close (); } catch (Exception _) {/*ignore exceptions*/ }
445457 }
446458
447459 protected FrameHandler createFrameHandler (Socket sock )
@@ -488,9 +500,10 @@ public Connection newConnection(ExecutorService executor, Address[] addrs)
488500 {
489501 IOException lastException = null ;
490502 for (Address addr : addrs ) {
503+ AMQConnection conn = null ;
491504 try {
492505 FrameHandler frameHandler = createFrameHandler (addr );
493- AMQConnection conn =
506+ conn =
494507 new AMQConnection (username ,
495508 password ,
496509 frameHandler ,
@@ -504,6 +517,7 @@ public Connection newConnection(ExecutorService executor, Address[] addrs)
504517 conn .start ();
505518 return conn ;
506519 } catch (IOException e ) {
520+ quietTryConnectionClose (conn );
507521 lastException = e ;
508522 }
509523 }
@@ -512,6 +526,12 @@ public Connection newConnection(ExecutorService executor, Address[] addrs)
512526 : new IOException ("failed to connect" );
513527 }
514528
529+ private static void quietTryConnectionClose (AMQConnection connection )
530+ {
531+ if (connection != null )
532+ try { connection .close (); } catch (Exception _) {/*ignore exceptions*/ }
533+ }
534+
515535 /**
516536 * Create a new broker connection
517537 * @return an interface to the connection
0 commit comments