diff --git a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/AbstractConnectionFactory.java b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/AbstractConnectionFactory.java index cc0d1c67..f17411c9 100644 --- a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/AbstractConnectionFactory.java +++ b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/AbstractConnectionFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -139,7 +139,7 @@ public abstract class AbstractConnectionFactory implements ConnectionFactory, Di private List
addresses; - private AddressShuffleMode addressShuffleMode = AddressShuffleMode.NONE; + private AddressShuffleMode addressShuffleMode = AddressShuffleMode.RANDOM; private int closeTimeout = DEFAULT_CLOSE_TIMEOUT; @@ -523,21 +523,6 @@ public abstract class AbstractConnectionFactory implements ConnectionFactory, Di return this.beanName; } - /** - * When {@link #setAddresses(String) addresses} are provided and there is more than - * one, set to true to shuffle the list before opening a new connection so that the - * connection to the broker will be attempted in random order. - * @param shuffleAddresses true to shuffle the list. - * @since 2.1.8 - * @deprecated since 2.3 in favor of - * @see Collections#shuffle(List) - * {@link #setAddressShuffleMode(AddressShuffleMode)}. - */ - @Deprecated - public void setShuffleAddresses(boolean shuffleAddresses) { - setAddressShuffleMode(AddressShuffleMode.RANDOM); - } - /** * Set the mode for shuffling addresses. * @param addressShuffleMode the address shuffle mode. diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryTests.java index 7534b465..5126ea9a 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +24,7 @@ import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.BDDMockito.given; @@ -65,6 +66,7 @@ import org.apache.commons.logging.Log; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; +import org.mockito.ArgumentMatcher; import org.mockito.InOrder; import org.springframework.amqp.AmqpConnectException; @@ -1657,8 +1659,10 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest ccf.createConnection(); verify(mock).isAutomaticRecoveryEnabled(); verify(mock).setAutomaticRecoveryEnabled(false); - verify(mock).newConnection(isNull(), - eq(Arrays.asList(new Address("mq1"), new Address("mq2"))), anyString()); + verify(mock).newConnection( + isNull(), + argThat((ArgumentMatcher>) a -> a.size() == 2 && a.contains(new Address("mq1")) && a.contains(new Address("mq2"))), + anyString()); verifyNoMoreInteractions(mock); } diff --git a/src/reference/asciidoc/amqp.adoc b/src/reference/asciidoc/amqp.adoc index 86c73dce..0d4931b6 100644 --- a/src/reference/asciidoc/amqp.adoc +++ b/src/reference/asciidoc/amqp.adoc @@ -609,9 +609,10 @@ public CachingConnectionFactory ccf() { ---- ==== -The underlying connection factory will attempt to connect to each host, in order, whenever a new connection is established. -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. -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. +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. +To revert to the previous behavior of attempting to connect from first to last, set the `addressShuffleMode` property to `AddressShuffleMode.NONE`. + +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. 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. ==== @@ -621,7 +622,7 @@ You may wish to use this mode with the https://github.com/rabbitmq/rabbitmq-shar public CachingConnectionFactory ccf() { CachingConnectionFactory ccf = new CachingConnectionFactory(); ccf.setAddresses("host1:5672,host2:5672,host3:5672"); - ccf.setAddressShuffleMode(AddressShuffleMode.RANDOM); + ccf.setAddressShuffleMode(AddressShuffleMode.INORDER); return ccf; } ---- diff --git a/src/reference/asciidoc/whats-new.adoc b/src/reference/asciidoc/whats-new.adoc index 0fbd0fcb..6825e4b5 100644 --- a/src/reference/asciidoc/whats-new.adoc +++ b/src/reference/asciidoc/whats-new.adoc @@ -31,3 +31,8 @@ See <> for more infoprmation. `MessageConverter` s can now return `Optional.empty()` for a null value; this is currently implemented by the `Jackson2JsonMessageConverter`. See <> for more information. + +==== Connection Factory Changes + +The default `addressShuffleMode` in `AbstractConnectionFactory` is now `RANDOM`. This results in connecting to a random host when multiple addresses are provided. +See <> for more information. \ No newline at end of file