Skip to content

Commit ad612a8

Browse files
committed
GH-1289: Confirms and Returns with Routing CF
Resolves #1289 `RoutingConnectionFactory` did not support correlated confirms or returns. Target factories (and default) must have the same settings. **cherry-pick to 2.2.x, 2.1.x** GH-1289: Fix test for back port - `CorrelationData` needs an id (`null` by default before 2.3).
1 parent 819587e commit ad612a8

File tree

3 files changed

+95
-8
lines changed

3 files changed

+95
-8
lines changed

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

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -22,6 +22,7 @@
2222
import java.util.concurrent.ConcurrentHashMap;
2323

2424
import org.springframework.amqp.AmqpException;
25+
import org.springframework.beans.factory.InitializingBean;
2526
import org.springframework.lang.Nullable;
2627
import org.springframework.util.Assert;
2728

@@ -35,7 +36,8 @@
3536
* @author Gary Russell
3637
* @since 1.3
3738
*/
38-
public abstract class AbstractRoutingConnectionFactory implements ConnectionFactory, RoutingConnectionFactory {
39+
public abstract class AbstractRoutingConnectionFactory implements ConnectionFactory, RoutingConnectionFactory,
40+
InitializingBean {
3941

4042
private final Map<Object, ConnectionFactory> targetConnectionFactories =
4143
new ConcurrentHashMap<Object, ConnectionFactory>();
@@ -46,6 +48,10 @@ public abstract class AbstractRoutingConnectionFactory implements ConnectionFact
4648

4749
private boolean lenientFallback = true;
4850

51+
private Boolean confirms;
52+
53+
private Boolean returns;
54+
4955
/**
5056
* Specify the map of target ConnectionFactories, with the lookup key as key.
5157
* <p>The key can be of arbitrary type; this class implements the
@@ -58,6 +64,7 @@ public void setTargetConnectionFactories(Map<Object, ConnectionFactory> targetCo
5864
Assert.noNullElements(targetConnectionFactories.values().toArray(),
5965
"'targetConnectionFactories' cannot have null values.");
6066
this.targetConnectionFactories.putAll(targetConnectionFactories);
67+
targetConnectionFactories.values().stream().forEach(cf -> checkConfirmsAndReturns(cf));
6168
}
6269

6370
/**
@@ -69,6 +76,7 @@ public void setTargetConnectionFactories(Map<Object, ConnectionFactory> targetCo
6976
*/
7077
public void setDefaultTargetConnectionFactory(ConnectionFactory defaultTargetConnectionFactory) {
7178
this.defaultTargetConnectionFactory = defaultTargetConnectionFactory;
79+
checkConfirmsAndReturns(defaultTargetConnectionFactory);
7280
}
7381

7482
/**
@@ -93,9 +101,37 @@ public boolean isLenientFallback() {
93101
return this.lenientFallback;
94102
}
95103

104+
@Override
105+
public boolean isPublisherConfirms() {
106+
return this.confirms;
107+
}
108+
109+
@Override
110+
public boolean isPublisherReturns() {
111+
return this.returns;
112+
}
113+
114+
@Override
115+
public void afterPropertiesSet() throws Exception {
116+
Assert.notNull(this.confirms, "At least one target factory (or default) is required");
117+
}
118+
119+
private void checkConfirmsAndReturns(ConnectionFactory cf) {
120+
if (this.confirms == null) {
121+
this.confirms = cf.isPublisherConfirms();
122+
}
123+
if (this.returns == null) {
124+
this.returns = cf.isPublisherReturns();
125+
}
126+
Assert.isTrue(this.confirms.booleanValue() == cf.isPublisherConfirms(),
127+
"Target connection factories must have the same setting for publisher confirms");
128+
Assert.isTrue(this.returns.booleanValue() == cf.isPublisherReturns(),
129+
"Target connection factories must have the same setting for publisher returns");
130+
}
131+
96132
@Override
97133
public Connection createConnection() throws AmqpException {
98-
return this.determineTargetConnectionFactory().createConnection();
134+
return determineTargetConnectionFactory().createConnection();
99135
}
100136

101137
/**

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplatePublisherCallbacksIntegrationTests2.java

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 the original author or authors.
2+
* Copyright 2016-2020 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.
@@ -18,6 +18,7 @@
1818

1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertTrue;
21+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2122

2223
import java.io.IOException;
2324
import java.util.concurrent.CountDownLatch;
@@ -29,6 +30,8 @@
2930
import org.junit.Test;
3031

3132
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
33+
import org.springframework.amqp.rabbit.connection.CorrelationData;
34+
import org.springframework.amqp.rabbit.connection.SimpleRoutingConnectionFactory;
3235
import org.springframework.amqp.rabbit.junit.BrokerRunning;
3336
import org.springframework.amqp.rabbit.junit.BrokerTestUtils;
3437

@@ -45,6 +48,8 @@ public class RabbitTemplatePublisherCallbacksIntegrationTests2 {
4548

4649
private static final String ROUTE = "test.queue";
4750

51+
public static final String ROUTE2 = "test.queue.RabbitTemplatePublisherCallbacksIntegrationTests2.route";
52+
4853
private CachingConnectionFactory connectionFactoryWithConfirmsEnabled;
4954

5055
private RabbitTemplate templateWithConfirmsEnabled;
@@ -56,8 +61,6 @@ public class RabbitTemplatePublisherCallbacksIntegrationTests2 {
5661
public void create() {
5762
connectionFactoryWithConfirmsEnabled = new CachingConnectionFactory();
5863
connectionFactoryWithConfirmsEnabled.setHost("localhost");
59-
// When using publisher confirms, the cache size needs to be large enough
60-
// otherwise channels can be closed before confirms are received.
6164
connectionFactoryWithConfirmsEnabled.setChannelCacheSize(100);
6265
connectionFactoryWithConfirmsEnabled.setPort(BrokerTestUtils.getPort());
6366
connectionFactoryWithConfirmsEnabled.setPublisherConfirms(true);
@@ -94,6 +97,51 @@ public void handleDelivery(String consumerTag, Envelope envelope, BasicPropertie
9497
assertMessageCountEquals(0L);
9598
}
9699

100+
@Test
101+
public void routingWithConfirmsNoListener() throws Exception {
102+
routingWithConfirms(false);
103+
}
104+
105+
@Test
106+
public void routingWithConfirmsListener() throws Exception {
107+
routingWithConfirms(true);
108+
}
109+
110+
private void routingWithConfirms(boolean listener) throws Exception {
111+
CountDownLatch latch = new CountDownLatch(1);
112+
SimpleRoutingConnectionFactory rcf = new SimpleRoutingConnectionFactory();
113+
rcf.setDefaultTargetConnectionFactory(this.connectionFactoryWithConfirmsEnabled);
114+
this.templateWithConfirmsEnabled.setConnectionFactory(rcf);
115+
if (listener) {
116+
this.templateWithConfirmsEnabled.setConfirmCallback((correlationData, ack, cause) -> {
117+
latch.countDown();
118+
});
119+
}
120+
this.templateWithConfirmsEnabled.setMandatory(true);
121+
CorrelationData corr = new CorrelationData("foo");
122+
this.templateWithConfirmsEnabled.convertAndSend("", ROUTE2, "foo", corr);
123+
assertTrue(corr.getFuture().get(10, TimeUnit.SECONDS).isAck());
124+
if (listener) {
125+
assertTrue(latch.await(10, TimeUnit.SECONDS));
126+
}
127+
corr = new CorrelationData("bar");
128+
this.templateWithConfirmsEnabled.convertAndSend("", "bad route", "foo", corr);
129+
assertTrue(corr.getFuture().get(10, TimeUnit.SECONDS).isAck());
130+
assertNotNull(corr.getReturnedMessage());
131+
}
132+
133+
@Test
134+
public void routingWithSimpleConfirms() throws Exception {
135+
SimpleRoutingConnectionFactory rcf = new SimpleRoutingConnectionFactory();
136+
rcf.setDefaultTargetConnectionFactory(this.connectionFactoryWithConfirmsEnabled);
137+
this.templateWithConfirmsEnabled.setConnectionFactory(rcf);
138+
assertTrue(this.templateWithConfirmsEnabled.<Boolean>invoke(template -> {
139+
template.convertAndSend("", ROUTE2, "foo");
140+
template.waitForConfirmsOrDie(10_000);
141+
return true;
142+
}));
143+
}
144+
97145
private void assertMessageCountEquals(long wanted) throws InterruptedException {
98146
long messageCount = determineMessageCount();
99147
int n = 0;

src/reference/asciidoc/amqp.adoc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,9 @@ Doing so enables, for example, listening to queues with the same name but in a d
627627

628628
For example, with lookup key qualifier `thing1` and a container listening to queue `thing2`, the lookup key you could register the target connection factory with could be `thing1[thing2]`.
629629

630+
IMPORTANT: The target (and default, if provided) connection factories must have the same settings for publisher confirms and returns.
631+
See <<cf-pub-conf-ret>>.
632+
630633
[[queue-affinity]]
631634
===== Queue Affinity and the `LocalizedQueueConnectionFactory`
632635

@@ -1115,8 +1118,8 @@ The `Confirm` object is a simple bean with 2 properties: `ack` and `reason` (for
11151118
The reason is not populated for broker-generated `nack` instances.
11161119
It is populated for `nack` instances generated by the framework (for example, closing the connection while `ack` instances are outstanding).
11171120

1118-
In addition, when both confirms and returns are enabled, the `CorrelationData` is populated with the returned message.
1119-
It is guaranteed that this occurs before the future is set with the `ack`.
1121+
In addition, when both confirms and returns are enabled, the `CorrelationData` is populated with the returned message, as long as the `CorrelationData` has a unique `id`; this is always the case, by default, starting with version 2.3.
1122+
It is guaranteed that the returned message is set before the future is set with the `ack`.
11201123

11211124
See also <<scoped-operations>> for a simpler mechanism for waiting for publisher confirms.
11221125

0 commit comments

Comments
 (0)