20
20
import java .time .temporal .ChronoUnit ;
21
21
import java .util .ArrayList ;
22
22
import java .util .List ;
23
+ import java .util .Optional ;
23
24
24
25
import org .springframework .amqp .core .AcknowledgeMode ;
25
26
import org .springframework .amqp .rabbit .connection .CachingConnectionFactory .CacheMode ;
45
46
@ ConfigurationProperties (prefix = "spring.rabbitmq" )
46
47
public class RabbitProperties {
47
48
49
+ private static final int DEFAULT_PORT = 5672 ;
50
+
51
+ private static final int DEFAULT_PORT_SECURE = 5671 ;
52
+
48
53
/**
49
- * RabbitMQ host.
54
+ * RabbitMQ host. Ignored if an address is set.
50
55
*/
51
56
private String host = "localhost" ;
52
57
53
58
/**
54
- * RabbitMQ port.
59
+ * RabbitMQ port. Ignored if an address is set. Default to 5672, or 5671 if SSL is
60
+ * enabled.
55
61
*/
56
- private int port = 5672 ;
62
+ private Integer port ;
57
63
58
64
/**
59
65
* Login user to authenticate to the broker.
@@ -76,7 +82,8 @@ public class RabbitProperties {
76
82
private String virtualHost ;
77
83
78
84
/**
79
- * Comma-separated list of addresses to which the client should connect.
85
+ * Comma-separated list of addresses to which the client should connect. When set, the
86
+ * host and port are ignored.
80
87
*/
81
88
private String addresses ;
82
89
@@ -143,7 +150,7 @@ public void setHost(String host) {
143
150
this .host = host ;
144
151
}
145
152
146
- public int getPort () {
153
+ public Integer getPort () {
147
154
return this .port ;
148
155
}
149
156
@@ -156,13 +163,16 @@ public int getPort() {
156
163
*/
157
164
public int determinePort () {
158
165
if (CollectionUtils .isEmpty (this .parsedAddresses )) {
159
- return getPort ();
166
+ Integer port = getPort ();
167
+ if (port != null ) {
168
+ return port ;
169
+ }
170
+ return (Optional .ofNullable (getSsl ().getEnabled ()).orElse (false )) ? DEFAULT_PORT_SECURE : DEFAULT_PORT ;
160
171
}
161
- Address address = this .parsedAddresses .get (0 );
162
- return address .port ;
172
+ return this .parsedAddresses .get (0 ).port ;
163
173
}
164
174
165
- public void setPort (int port ) {
175
+ public void setPort (Integer port ) {
166
176
this .port = port ;
167
177
}
168
178
@@ -177,7 +187,7 @@ public String getAddresses() {
177
187
*/
178
188
public String determineAddresses () {
179
189
if (CollectionUtils .isEmpty (this .parsedAddresses )) {
180
- return this .host + ":" + this . port ;
190
+ return this .host + ":" + determinePort () ;
181
191
}
182
192
List <String > addressStrings = new ArrayList <>();
183
193
for (Address parsedAddress : this .parsedAddresses ) {
@@ -194,7 +204,7 @@ public void setAddresses(String addresses) {
194
204
private List <Address > parseAddresses (String addresses ) {
195
205
List <Address > parsedAddresses = new ArrayList <>();
196
206
for (String address : StringUtils .commaDelimitedListToStringArray (addresses )) {
197
- parsedAddresses .add (new Address (address , getSsl ().isEnabled ( )));
207
+ parsedAddresses .add (new Address (address , Optional . ofNullable ( getSsl ().getEnabled ()). orElse ( false )));
198
208
}
199
209
return parsedAddresses ;
200
210
}
@@ -327,9 +337,10 @@ public Template getTemplate() {
327
337
public class Ssl {
328
338
329
339
/**
330
- * Whether to enable SSL support.
340
+ * Whether to enable SSL support. Determined automatically if an address is
341
+ * provided with the protocol (amqp:// vs. amqps://).
331
342
*/
332
- private boolean enabled ;
343
+ private Boolean enabled ;
333
344
334
345
/**
335
346
* Path to the key store that holds the SSL certificate.
@@ -376,7 +387,7 @@ public class Ssl {
376
387
*/
377
388
private boolean verifyHostname = true ;
378
389
379
- public boolean isEnabled () {
390
+ public Boolean getEnabled () {
380
391
return this .enabled ;
381
392
}
382
393
@@ -385,17 +396,18 @@ public boolean isEnabled() {
385
396
* enabled flag if no addresses have been set.
386
397
* @return whether ssl is enabled
387
398
* @see #setAddresses(String)
388
- * @see #isEnabled ()
399
+ * @see #getEnabled() ()
389
400
*/
390
401
public boolean determineEnabled () {
402
+ boolean defaultEnabled = Optional .ofNullable (getEnabled ()).orElse (false );
391
403
if (CollectionUtils .isEmpty (RabbitProperties .this .parsedAddresses )) {
392
- return isEnabled () ;
404
+ return defaultEnabled ;
393
405
}
394
406
Address address = RabbitProperties .this .parsedAddresses .get (0 );
395
- return address .determineSslEnabled (isEnabled () );
407
+ return address .determineSslEnabled (defaultEnabled );
396
408
}
397
409
398
- public void setEnabled (boolean enabled ) {
410
+ public void setEnabled (Boolean enabled ) {
399
411
this .enabled = enabled ;
400
412
}
401
413
@@ -953,12 +965,8 @@ private static final class Address {
953
965
954
966
private static final String PREFIX_AMQP = "amqp://" ;
955
967
956
- private static final int DEFAULT_PORT = 5672 ;
957
-
958
968
private static final String PREFIX_AMQP_SECURE = "amqps://" ;
959
969
960
- private static final int DEFAULT_PORT_SECURE = 5671 ;
961
-
962
970
private String host ;
963
971
964
972
private int port ;
0 commit comments