Skip to content

Commit

Permalink
GH-1497: Use RANDOM as default addressShuffleMode
Browse files Browse the repository at this point in the history
Resolves #1497

GH-1497: Remove deprecated setShuffleAddresses method

GH-1497: Fix failing setAddressesTwoHosts

Doc Polishing.
  • Loading branch information
rvvaeke authored and garyrussell committed Sep 6, 2022
1 parent 5e6595d commit c929cd5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -139,7 +139,7 @@ public void handleRecovery(Recoverable recoverable) {

private List<Address> addresses;

private AddressShuffleMode addressShuffleMode = AddressShuffleMode.NONE;
private AddressShuffleMode addressShuffleMode = AddressShuffleMode.RANDOM;

private int closeTimeout = DEFAULT_CLOSE_TIMEOUT;

Expand Down Expand Up @@ -523,21 +523,6 @@ protected String getBeanName() {
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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -24,6 +24,7 @@
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;
Expand Down Expand Up @@ -65,6 +66,7 @@
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;
Expand Down Expand Up @@ -1657,8 +1659,10 @@ public void setAddressesTwoHosts() throws Exception {
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<List<Address>>) a -> a.size() == 2 && a.contains(new Address("mq1")) && a.contains(new Address("mq2"))),
anyString());
verifyNoMoreInteractions(mock);
}

Expand Down
9 changes: 5 additions & 4 deletions src/reference/asciidoc/amqp.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.

====
Expand All @@ -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;
}
----
Expand Down
5 changes: 5 additions & 0 deletions src/reference/asciidoc/whats-new.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ See <<receiving-batch>> for more infoprmation.

`MessageConverter` s can now return `Optional.empty()` for a null value; this is currently implemented by the `Jackson2JsonMessageConverter`.
See <<Jackson2JsonMessageConverter-from-message>> 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 <<cluster>> for more information.

0 comments on commit c929cd5

Please sign in to comment.