|
53 | 53 | /** |
54 | 54 | * Convenience "factory" class to facilitate opening a {@link Connection} to an AMQP broker. |
55 | 55 | */ |
56 | | - |
57 | 56 | public class ConnectionFactory implements Cloneable { |
58 | 57 |
|
59 | 58 | /** Default user name */ |
@@ -100,34 +99,30 @@ public class ConnectionFactory implements Cloneable { |
100 | 99 |
|
101 | 100 | private static final String FALLBACK_TLS_PROTOCOL = "TLSv1"; |
102 | 101 |
|
103 | | - private String virtualHost = DEFAULT_VHOST; |
104 | | - private String host = DEFAULT_HOST; |
105 | | - private int port = USE_DEFAULT_PORT; |
106 | | - private int requestedChannelMax = DEFAULT_CHANNEL_MAX; |
107 | | - private int requestedFrameMax = DEFAULT_FRAME_MAX; |
108 | | - private int requestedHeartbeat = DEFAULT_HEARTBEAT; |
109 | | - private int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT; |
110 | | - private int handshakeTimeout = DEFAULT_HANDSHAKE_TIMEOUT; |
111 | | - private int shutdownTimeout = DEFAULT_SHUTDOWN_TIMEOUT; |
112 | | - private Map<String, Object> _clientProperties = AMQConnection.defaultClientProperties(); |
113 | | - private SocketFactory factory = SocketFactory.getDefault(); |
114 | | - private SaslConfig saslConfig = DefaultSaslConfig.PLAIN; |
| 102 | + private String virtualHost = DEFAULT_VHOST; |
| 103 | + private String host = DEFAULT_HOST; |
| 104 | + private int port = USE_DEFAULT_PORT; |
| 105 | + private int requestedChannelMax = DEFAULT_CHANNEL_MAX; |
| 106 | + private int requestedFrameMax = DEFAULT_FRAME_MAX; |
| 107 | + private int requestedHeartbeat = DEFAULT_HEARTBEAT; |
| 108 | + private int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT; |
| 109 | + private int handshakeTimeout = DEFAULT_HANDSHAKE_TIMEOUT; |
| 110 | + private int shutdownTimeout = DEFAULT_SHUTDOWN_TIMEOUT; |
| 111 | + private Map<String, Object> _clientProperties = AMQConnection.defaultClientProperties(); |
| 112 | + private SocketFactory factory = SocketFactory.getDefault(); |
| 113 | + private SaslConfig saslConfig = DefaultSaslConfig.PLAIN; |
115 | 114 | private ExecutorService sharedExecutor; |
116 | | - private ThreadFactory threadFactory = Executors.defaultThreadFactory(); |
| 115 | + private ThreadFactory threadFactory = Executors.defaultThreadFactory(); |
117 | 116 | // minimises the number of threads rapid closure of many |
118 | 117 | // connections uses, see rabbitmq/rabbitmq-java-client#86 |
119 | 118 | private ExecutorService shutdownExecutor; |
120 | 119 | private ScheduledExecutorService heartbeatExecutor; |
121 | | - private SocketConfigurator socketConf = new DefaultSocketConfigurator(); |
122 | | - private ExceptionHandler exceptionHandler = new DefaultExceptionHandler(); |
123 | | - private CredentialsProvider credentialsProv = new DefaultCredentialsProvider(); |
124 | | - { |
125 | | - credentialsProv.setUsername(DEFAULT_USER); |
126 | | - credentialsProv.setPassword(DEFAULT_PASS); |
127 | | - } |
| 120 | + private SocketConfigurator socketConf = new DefaultSocketConfigurator(); |
| 121 | + private ExceptionHandler exceptionHandler = new DefaultExceptionHandler(); |
| 122 | + private CredentialsProvider credentialsProvider = new DefaultCredentialsProvider(DEFAULT_USER, DEFAULT_PASS); |
128 | 123 |
|
129 | | - private boolean automaticRecovery = true; |
130 | | - private boolean topologyRecovery = true; |
| 124 | + private boolean automaticRecovery = true; |
| 125 | + private boolean topologyRecovery = true; |
131 | 126 |
|
132 | 127 | // long is used to make sure the users can use both ints |
133 | 128 | // and longs safely. It is unlikely that anybody'd need |
@@ -190,41 +185,48 @@ public void setPort(int port) { |
190 | 185 | * @return the AMQP user name to use when connecting to the broker |
191 | 186 | */ |
192 | 187 | public String getUsername() { |
193 | | - return credentialsProv.getUsername(); |
| 188 | + return credentialsProvider.getUsername(); |
194 | 189 | } |
195 | 190 |
|
196 | 191 | /** |
197 | 192 | * Set the user name. |
198 | 193 | * @param username the AMQP user name to use when connecting to the broker |
199 | 194 | */ |
200 | 195 | public void setUsername(String username) { |
201 | | - credentialsProv.setUsername(username); |
| 196 | + this.credentialsProvider = new DefaultCredentialsProvider( |
| 197 | + username, |
| 198 | + this.credentialsProvider.getPassword() |
| 199 | + ); |
202 | 200 | } |
203 | 201 |
|
204 | 202 | /** |
205 | 203 | * Retrieve the password. |
206 | 204 | * @return the password to use when connecting to the broker |
207 | 205 | */ |
208 | 206 | public String getPassword() { |
209 | | - return credentialsProv.getPassword(); |
| 207 | + return credentialsProvider.getPassword(); |
210 | 208 | } |
211 | 209 |
|
212 | 210 | /** |
213 | 211 | * Set the password. |
214 | 212 | * @param password the password to use when connecting to the broker |
215 | 213 | */ |
216 | 214 | public void setPassword(String password) { |
217 | | - credentialsProv.setPassword(password); |
| 215 | + this.credentialsProvider = new DefaultCredentialsProvider( |
| 216 | + this.credentialsProvider.getUsername(), |
| 217 | + password |
| 218 | + ); |
218 | 219 | } |
219 | 220 |
|
220 | 221 | /** |
221 | 222 | * Set a custom credentials provider. |
| 223 | + * Default implementation uses static username and password. |
222 | 224 | * @param credentialsProvider The custom implementation of CredentialsProvider to use when connecting to the broker. |
223 | 225 | * @see com.rabbitmq.client.impl.DefaultCredentialsProvider |
224 | | - * @see com.rabbitmq.client.impl.AbstractCredentialsProvider |
| 226 | + * @since 4.5.0 |
225 | 227 | */ |
226 | 228 | public void setCredentialsProvider(CredentialsProvider credentialsProvider) { |
227 | | - this.credentialsProv = credentialsProvider; |
| 229 | + this.credentialsProvider = credentialsProvider; |
228 | 230 | } |
229 | 231 |
|
230 | 232 | /** |
@@ -982,7 +984,7 @@ public Connection newConnection(ExecutorService executor, AddressResolver addres |
982 | 984 | public ConnectionParams params(ExecutorService consumerWorkServiceExecutor) { |
983 | 985 | ConnectionParams result = new ConnectionParams(); |
984 | 986 |
|
985 | | - result.setCredentialsProvider(credentialsProv); |
| 987 | + result.setCredentialsProvider(credentialsProvider); |
986 | 988 | result.setConsumerWorkServiceExecutor(consumerWorkServiceExecutor); |
987 | 989 | result.setVirtualHost(virtualHost); |
988 | 990 | result.setClientProperties(getClientProperties()); |
@@ -1081,7 +1083,6 @@ protected AddressResolver createAddressResolver(List<Address> addresses) { |
1081 | 1083 | @Override public ConnectionFactory clone(){ |
1082 | 1084 | try { |
1083 | 1085 | ConnectionFactory clone = (ConnectionFactory)super.clone(); |
1084 | | - clone.credentialsProv = (CredentialsProvider)clone.credentialsProv.clone(); |
1085 | 1086 | return clone; |
1086 | 1087 | } catch (CloneNotSupportedException e) { |
1087 | 1088 | throw new Error(e); |
|
0 commit comments