21
21
import java .security .NoSuchAlgorithmException ;
22
22
import java .util .Map ;
23
23
import java .util .concurrent .ExecutorService ;
24
- import java .util .concurrent .Executors ;
25
24
26
25
import java .net .InetSocketAddress ;
27
26
import java .net .Socket ;
43
42
*/
44
43
45
44
public class ConnectionFactory implements Cloneable {
46
-
45
+
47
46
/** Default Executor threads */
48
- private static final int DEFAULT_NUM_CONSUMER_THREADS = 5 ;
47
+ @ Deprecated
48
+ public static final int DEFAULT_NUM_CONSUMER_THREADS = 5 ;
49
49
/** Default user name */
50
- private static final String DEFAULT_USER = "guest" ;
50
+ public static final String DEFAULT_USER = "guest" ;
51
51
/** Default password */
52
- private static final String DEFAULT_PASS = "guest" ;
52
+ public static final String DEFAULT_PASS = "guest" ;
53
53
/** Default virtual host */
54
- private static final String DEFAULT_VHOST = "/" ;
54
+ public static final String DEFAULT_VHOST = "/" ;
55
55
/** Default maximum channel number;
56
56
* zero for unlimited */
57
- private static final int DEFAULT_CHANNEL_MAX = 0 ;
57
+ public static final int DEFAULT_CHANNEL_MAX = 0 ;
58
58
/** Default maximum frame size;
59
59
* zero means no limit */
60
- private static final int DEFAULT_FRAME_MAX = 0 ;
60
+ public static final int DEFAULT_FRAME_MAX = 0 ;
61
61
/** Default heart-beat interval;
62
62
* zero means no heart-beats */
63
- private static final int DEFAULT_HEARTBEAT = 0 ;
63
+ public static final int DEFAULT_HEARTBEAT = 0 ;
64
64
/** The default host */
65
- private static final String DEFAULT_HOST = "localhost" ;
65
+ public static final String DEFAULT_HOST = "localhost" ;
66
66
/** 'Use the default port' port */
67
- private static final int USE_DEFAULT_PORT = -1 ;
67
+ public static final int USE_DEFAULT_PORT = -1 ;
68
68
/** The default non-ssl port */
69
- private static final int DEFAULT_AMQP_PORT = AMQP .PROTOCOL .PORT ;
69
+ public static final int DEFAULT_AMQP_PORT = AMQP .PROTOCOL .PORT ;
70
70
/** The default ssl port */
71
- private static final int DEFAULT_AMQP_OVER_SSL_PORT = 5671 ;
71
+ public static final int DEFAULT_AMQP_OVER_SSL_PORT = 5671 ;
72
72
/** The default connection timeout;
73
73
* zero means wait indefinitely */
74
- private static final int DEFAULT_CONNECTION_TIMEOUT = 0 ;
74
+ public static final int DEFAULT_CONNECTION_TIMEOUT = 0 ;
75
+
75
76
/** The default SSL protocol */
76
77
private static final String DEFAULT_SSL_PROTOCOL = "SSLv3" ;
77
78
78
- private int numConsumerThreads = DEFAULT_NUM_CONSUMER_THREADS ;
79
79
private String username = DEFAULT_USER ;
80
80
private String password = DEFAULT_PASS ;
81
81
private String virtualHost = DEFAULT_VHOST ;
@@ -90,13 +90,15 @@ public class ConnectionFactory implements Cloneable {
90
90
private SaslConfig saslConfig = DefaultSaslConfig .PLAIN ;
91
91
92
92
/** @return number of consumer threads in default {@link ExecutorService} */
93
+ @ Deprecated
93
94
public int getNumConsumerThreads () {
94
- return numConsumerThreads ;
95
+ return DEFAULT_NUM_CONSUMER_THREADS ;
95
96
}
96
97
97
98
/** @param numConsumerThreads threads in created private executor service */
99
+ @ Deprecated
98
100
public void setNumConsumerThreads (int numConsumerThreads ) {
99
- this . numConsumerThreads = numConsumerThreads ;
101
+ throw new IllegalArgumentException ( "setNumConsumerThreads not supported -- create explicit ExecutorService instead." ) ;
100
102
}
101
103
102
104
/** @return the default host to use for connections */
@@ -436,10 +438,22 @@ protected FrameHandler createFrameHandler(Address addr)
436
438
437
439
String hostName = addr .getHost ();
438
440
int portNumber = portOrDefault (addr .getPort ());
439
- Socket socket = factory .createSocket ();
440
- configureSocket (socket );
441
- socket .connect (new InetSocketAddress (hostName , portNumber ), connectionTimeout );
442
- 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*/ }
443
457
}
444
458
445
459
protected FrameHandler createFrameHandler (Socket sock )
@@ -464,21 +478,31 @@ protected void configureSocket(Socket socket) throws IOException{
464
478
socket .setTcpNoDelay (true );
465
479
}
466
480
481
+ /**
482
+ * Create a new broker connection
483
+ * @param addrs an array of known broker addresses (hostname/port pairs) to try in order
484
+ * @return an interface to the connection
485
+ * @throws IOException if it encounters a problem
486
+ */
487
+ public Connection newConnection (Address [] addrs ) throws IOException {
488
+ return newConnection (null , addrs );
489
+ }
490
+
467
491
/**
468
492
* Create a new broker connection
469
493
* @param executor thread execution service for consumers on the connection
470
494
* @param addrs an array of known broker addresses (hostname/port pairs) to try in order
471
495
* @return an interface to the connection
472
496
* @throws IOException if it encounters a problem
473
497
*/
474
- private Connection newConnection (ExecutorService executor , Address [] addrs )
498
+ public Connection newConnection (ExecutorService executor , Address [] addrs )
475
499
throws IOException
476
500
{
477
501
IOException lastException = null ;
478
502
for (Address addr : addrs ) {
479
503
try {
480
504
FrameHandler frameHandler = createFrameHandler (addr );
481
- AMQConnection conn =
505
+ AMQConnection conn =
482
506
new AMQConnection (username ,
483
507
password ,
484
508
frameHandler ,
@@ -506,7 +530,7 @@ private Connection newConnection(ExecutorService executor, Address[] addrs)
506
530
* @throws IOException if it encounters a problem
507
531
*/
508
532
public Connection newConnection () throws IOException {
509
- return newConnection (Executors . newFixedThreadPool ( this . numConsumerThreads ) ,
533
+ return newConnection (null ,
510
534
new Address [] {new Address (getHost (), getPort ())}
511
535
);
512
536
}
0 commit comments