36
36
import static java .util .concurrent .TimeUnit .*;
37
37
38
38
/**
39
- * Convenience "factory" class to facilitate opening a {@link Connection} to an AMQP broker.
39
+ * Convenience factory class to facilitate opening a {@link Connection} to a RabbitMQ node.
40
+ *
41
+ * Most connection and socket settings are configured using this factory.
42
+ * Some settings that apply to connections can also be configured here
43
+ * and will apply to all connections produced by this factory.
40
44
*/
41
45
42
46
public class ConnectionFactory implements Cloneable {
@@ -94,7 +98,7 @@ public class ConnectionFactory implements Cloneable {
94
98
private int handshakeTimeout = DEFAULT_HANDSHAKE_TIMEOUT ;
95
99
private int shutdownTimeout = DEFAULT_SHUTDOWN_TIMEOUT ;
96
100
private Map <String , Object > _clientProperties = AMQConnection .defaultClientProperties ();
97
- private SocketFactory factory = SocketFactory . getDefault () ;
101
+ private SocketFactory socketFactory = null ;
98
102
private SaslConfig saslConfig = DefaultSaslConfig .PLAIN ;
99
103
private ExecutorService sharedExecutor ;
100
104
private ThreadFactory threadFactory = Executors .defaultThreadFactory ();
@@ -119,7 +123,7 @@ public class ConnectionFactory implements Cloneable {
119
123
private FrameHandlerFactory frameHandlerFactory ;
120
124
private NioParams nioParams = new NioParams ();
121
125
122
- private SSLContext sslContext ;
126
+ private SslContextFactory sslContextFactory ;
123
127
124
128
/**
125
129
* Continuation timeout on RPC calls.
@@ -442,18 +446,19 @@ public void setSaslConfig(SaslConfig saslConfig) {
442
446
* Retrieve the socket factory used to make connections with.
443
447
*/
444
448
public SocketFactory getSocketFactory () {
445
- return this .factory ;
449
+ return this .socketFactory ;
446
450
}
447
451
448
452
/**
449
- * Set the socket factory used to make connections with . Can be
450
- * used to enable SSL connections by passing in a
453
+ * Set the socket factory used to create sockets for new connections . Can be
454
+ * used to customize TLS-related settings by passing in a
451
455
* javax.net.ssl.SSLSocketFactory instance.
452
- *
456
+ * Note this applies only to blocking IO, not to
457
+ * NIO, as the NIO API doesn't use the SocketFactory API.
453
458
* @see #useSslProtocol
454
459
*/
455
460
public void setSocketFactory (SocketFactory factory ) {
456
- this .factory = factory ;
461
+ this .socketFactory = factory ;
457
462
}
458
463
459
464
/**
@@ -556,7 +561,7 @@ public void setExceptionHandler(ExceptionHandler exceptionHandler) {
556
561
}
557
562
558
563
public boolean isSSL (){
559
- return getSocketFactory () instanceof SSLSocketFactory ;
564
+ return getSocketFactory () instanceof SSLSocketFactory || sslContextFactory != null ;
560
565
}
561
566
562
567
/**
@@ -572,6 +577,10 @@ public void useSslProtocol()
572
577
/**
573
578
* Convenience method for setting up a SSL socket factory/engine, using
574
579
* the supplied protocol and a very trusting TrustManager.
580
+ * The produced {@link SSLContext} instance will be shared by all
581
+ * the connections created by this connection factory. Use
582
+ * {@link #setSslContextFactory(SslContextFactory)} for more flexibility.
583
+ * @see #setSslContextFactory(SslContextFactory)
575
584
*/
576
585
public void useSslProtocol (String protocol )
577
586
throws NoSuchAlgorithmException , KeyManagementException
@@ -582,8 +591,11 @@ public void useSslProtocol(String protocol)
582
591
/**
583
592
* Convenience method for setting up an SSL socket factory/engine.
584
593
* Pass in the SSL protocol to use, e.g. "TLSv1" or "TLSv1.2".
585
- *
594
+ * The produced {@link SSLContext} instance will be shared with all
595
+ * the connections created by this connection factory. Use
596
+ * {@link #setSslContextFactory(SslContextFactory)} for more flexibility.
586
597
* @param protocol SSL protocol to use.
598
+ * @see #setSslContextFactory(SslContextFactory)
587
599
*/
588
600
public void useSslProtocol (String protocol , TrustManager trustManager )
589
601
throws NoSuchAlgorithmException , KeyManagementException
@@ -594,14 +606,17 @@ public void useSslProtocol(String protocol, TrustManager trustManager)
594
606
}
595
607
596
608
/**
597
- * Convenience method for setting up an SSL socket factory /engine.
609
+ * Convenience method for setting up an SSL socket socketFactory /engine.
598
610
* Pass in an initialized SSLContext.
599
- *
611
+ * The {@link SSLContext} instance will be shared with all
612
+ * the connections created by this connection factory. Use
613
+ * {@link #setSslContextFactory(SslContextFactory)} for more flexibility.
600
614
* @param context An initialized SSLContext
615
+ * @see #setSslContextFactory(SslContextFactory)
601
616
*/
602
617
public void useSslProtocol (SSLContext context ) {
618
+ this .sslContextFactory = name -> context ;
603
619
setSocketFactory (context .getSocketFactory ());
604
- this .sslContext = context ;
605
620
}
606
621
607
622
public static String computeDefaultTlsProcotol (String [] supportedProtocols ) {
@@ -667,11 +682,11 @@ protected synchronized FrameHandlerFactory createFrameHandlerFactory() throws IO
667
682
if (this .nioParams .getNioExecutor () == null && this .nioParams .getThreadFactory () == null ) {
668
683
this .nioParams .setThreadFactory (getThreadFactory ());
669
684
}
670
- this .frameHandlerFactory = new SocketChannelFrameHandlerFactory (connectionTimeout , nioParams , isSSL (), sslContext );
685
+ this .frameHandlerFactory = new SocketChannelFrameHandlerFactory (connectionTimeout , nioParams , isSSL (), sslContextFactory );
671
686
}
672
687
return this .frameHandlerFactory ;
673
688
} else {
674
- return new SocketFrameHandlerFactory (connectionTimeout , factory , socketConf , isSSL (), this .shutdownExecutor );
689
+ return new SocketFrameHandlerFactory (connectionTimeout , socketFactory , socketConf , isSSL (), this .shutdownExecutor , sslContextFactory );
675
690
}
676
691
677
692
}
@@ -915,7 +930,7 @@ public Connection newConnection(ExecutorService executor, AddressResolver addres
915
930
Exception lastException = null ;
916
931
for (Address addr : addrs ) {
917
932
try {
918
- FrameHandler handler = fhFactory .create (addr );
933
+ FrameHandler handler = fhFactory .create (addr , clientProvidedName );
919
934
AMQConnection conn = createConnection (params , handler , metricsCollector );
920
935
conn .start ();
921
936
this .metricsCollector .newConnection (conn );
@@ -1124,4 +1139,20 @@ public void setChannelRpcTimeout(int channelRpcTimeout) {
1124
1139
public int getChannelRpcTimeout () {
1125
1140
return channelRpcTimeout ;
1126
1141
}
1142
+
1143
+ /**
1144
+ * The factory to create SSL contexts.
1145
+ * This provides more flexibility to create {@link SSLContext}s
1146
+ * for different connections than sharing the {@link SSLContext}
1147
+ * with all the connections produced by the connection factory
1148
+ * (which is the case with the {@link #useSslProtocol()} methods).
1149
+ * This way, different connections with a different certificate
1150
+ * for each of them is a possible scenario.
1151
+ * @param sslContextFactory
1152
+ * @see #useSslProtocol(SSLContext)
1153
+ * @since 5.0.0
1154
+ */
1155
+ public void setSslContextFactory (SslContextFactory sslContextFactory ) {
1156
+ this .sslContextFactory = sslContextFactory ;
1157
+ }
1127
1158
}
0 commit comments