Skip to content

Commit c929cd5

Browse files
rvvaekegaryrussell
authored andcommitted
GH-1497: Use RANDOM as default addressShuffleMode
Resolves #1497 GH-1497: Remove deprecated setShuffleAddresses method GH-1497: Fix failing setAddressesTwoHosts Doc Polishing.
1 parent 5e6595d commit c929cd5

4 files changed

Lines changed: 19 additions & 24 deletions

File tree

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/AbstractConnectionFactory.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -139,7 +139,7 @@ public void handleRecovery(Recoverable recoverable) {
139139

140140
private List<Address> addresses;
141141

142-
private AddressShuffleMode addressShuffleMode = AddressShuffleMode.NONE;
142+
private AddressShuffleMode addressShuffleMode = AddressShuffleMode.RANDOM;
143143

144144
private int closeTimeout = DEFAULT_CLOSE_TIMEOUT;
145145

@@ -523,21 +523,6 @@ protected String getBeanName() {
523523
return this.beanName;
524524
}
525525

526-
/**
527-
* When {@link #setAddresses(String) addresses} are provided and there is more than
528-
* one, set to true to shuffle the list before opening a new connection so that the
529-
* connection to the broker will be attempted in random order.
530-
* @param shuffleAddresses true to shuffle the list.
531-
* @since 2.1.8
532-
* @deprecated since 2.3 in favor of
533-
* @see Collections#shuffle(List)
534-
* {@link #setAddressShuffleMode(AddressShuffleMode)}.
535-
*/
536-
@Deprecated
537-
public void setShuffleAddresses(boolean shuffleAddresses) {
538-
setAddressShuffleMode(AddressShuffleMode.RANDOM);
539-
}
540-
541526
/**
542527
* Set the mode for shuffling addresses.
543528
* @param addressShuffleMode the address shuffle mode.

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryTests.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import static org.mockito.ArgumentMatchers.anyInt;
2525
import static org.mockito.ArgumentMatchers.anyLong;
2626
import static org.mockito.ArgumentMatchers.anyString;
27+
import static org.mockito.ArgumentMatchers.argThat;
2728
import static org.mockito.ArgumentMatchers.eq;
2829
import static org.mockito.ArgumentMatchers.isNull;
2930
import static org.mockito.BDDMockito.given;
@@ -65,6 +66,7 @@
6566
import org.junit.jupiter.api.Disabled;
6667
import org.junit.jupiter.api.Test;
6768
import org.mockito.ArgumentCaptor;
69+
import org.mockito.ArgumentMatcher;
6870
import org.mockito.InOrder;
6971

7072
import org.springframework.amqp.AmqpConnectException;
@@ -1657,8 +1659,10 @@ public void setAddressesTwoHosts() throws Exception {
16571659
ccf.createConnection();
16581660
verify(mock).isAutomaticRecoveryEnabled();
16591661
verify(mock).setAutomaticRecoveryEnabled(false);
1660-
verify(mock).newConnection(isNull(),
1661-
eq(Arrays.asList(new Address("mq1"), new Address("mq2"))), anyString());
1662+
verify(mock).newConnection(
1663+
isNull(),
1664+
argThat((ArgumentMatcher<List<Address>>) a -> a.size() == 2 && a.contains(new Address("mq1")) && a.contains(new Address("mq2"))),
1665+
anyString());
16621666
verifyNoMoreInteractions(mock);
16631667
}
16641668

src/reference/asciidoc/amqp.adoc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -609,9 +609,10 @@ public CachingConnectionFactory ccf() {
609609
----
610610
====
611611

612-
The underlying connection factory will attempt to connect to each host, in order, whenever a new connection is established.
613-
Starting with version 2.1.8, the connection order can be made random by setting the `addressShuffleMode` property to `RANDOM`; the shuffle will be applied before creating any new connection.
614-
Starting with version 2.6, the `INORDER` shuffle mode was added, which means the first address is moved to the end after a connection is created.
612+
Starting with version 3.0, the underlying connection factory will attempt to connect to a host, by choosing a random address, whenever a new connection is established.
613+
To revert to the previous behavior of attempting to connect from first to last, set the `addressShuffleMode` property to `AddressShuffleMode.NONE`.
614+
615+
Starting with version 2.3, the `INORDER` shuffle mode was added, which means the first address is moved to the end after a connection is created.
615616
You may wish to use this mode with the https://github.com/rabbitmq/rabbitmq-sharding[RabbitMQ Sharding Plugin] with `CacheMode.CONNECTION` and suitable concurrency if you wish to consume from all shards on all nodes.
616617

617618
====
@@ -621,7 +622,7 @@ You may wish to use this mode with the https://github.com/rabbitmq/rabbitmq-shar
621622
public CachingConnectionFactory ccf() {
622623
CachingConnectionFactory ccf = new CachingConnectionFactory();
623624
ccf.setAddresses("host1:5672,host2:5672,host3:5672");
624-
ccf.setAddressShuffleMode(AddressShuffleMode.RANDOM);
625+
ccf.setAddressShuffleMode(AddressShuffleMode.INORDER);
625626
return ccf;
626627
}
627628
----

src/reference/asciidoc/whats-new.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,8 @@ See <<receiving-batch>> for more infoprmation.
3131

3232
`MessageConverter` s can now return `Optional.empty()` for a null value; this is currently implemented by the `Jackson2JsonMessageConverter`.
3333
See <<Jackson2JsonMessageConverter-from-message>> for more information.
34+
35+
==== Connection Factory Changes
36+
37+
The default `addressShuffleMode` in `AbstractConnectionFactory` is now `RANDOM`. This results in connecting to a random host when multiple addresses are provided.
38+
See <<cluster>> for more information.

0 commit comments

Comments
 (0)