Skip to content

Commit 514fa92

Browse files
garyrussellartembilan
authored andcommitted
GH-1034: DMLC: Detect target channel changed
Fixes #1034 If the connection factory refreshed the target connection, the DMLC is not made aware of it and so we never consume from the new channel. **cherry-pick to all 2.x** # Conflicts: # spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/DirectMessageListenerContainerMockTests.java
1 parent 6423921 commit 514fa92

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/listener/DirectMessageListenerContainer.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.springframework.amqp.core.Message;
4848
import org.springframework.amqp.core.MessageProperties;
4949
import org.springframework.amqp.core.Queue;
50+
import org.springframework.amqp.rabbit.connection.ChannelProxy;
5051
import org.springframework.amqp.rabbit.connection.Connection;
5152
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
5253
import org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils;
@@ -472,7 +473,8 @@ private void checkConsumers(long now) {
472473
synchronized (this.consumersMonitor) {
473474
consumersToCancel = this.consumers.stream()
474475
.filter(consumer -> {
475-
boolean open = consumer.getChannel().isOpen() && !consumer.isAckFailed();
476+
boolean open = consumer.getChannel().isOpen() && !consumer.isAckFailed()
477+
&& !consumer.targetChanged();
476478
if (open && this.messagesPerAck > 1) {
477479
try {
478480
consumer.ackIfNecessary(now);
@@ -870,6 +872,8 @@ final class SimpleConsumer extends DefaultConsumer {
870872

871873
private final long ackTimeout = DirectMessageListenerContainer.this.ackTimeout;
872874

875+
private final Channel targetChannel;
876+
873877
private int pendingAcks;
874878

875879
private long lastAck = System.currentTimeMillis();
@@ -888,11 +892,17 @@ final class SimpleConsumer extends DefaultConsumer {
888892

889893
private volatile boolean ackFailed;
890894

891-
private SimpleConsumer(Connection connection, Channel channel, String queue) {
895+
SimpleConsumer(Connection connection, Channel channel, String queue) {
892896
super(channel);
893897
this.connection = connection;
894898
this.queue = queue;
895899
this.ackRequired = !getAcknowledgeMode().isAutoAck() && !getAcknowledgeMode().isManual();
900+
if (channel instanceof ChannelProxy) {
901+
this.targetChannel = ((ChannelProxy) channel).getTargetChannel();
902+
}
903+
else {
904+
this.targetChannel = null;
905+
}
896906
}
897907

898908
private String getQueue() {
@@ -929,6 +939,15 @@ boolean isAckFailed() {
929939
return this.ackFailed;
930940
}
931941

942+
/**
943+
* True if the channel is a proxy and the underlying channel has changed.
944+
* @return true if the condition exists.
945+
*/
946+
boolean targetChanged() {
947+
return this.targetChannel != null
948+
&& !((ChannelProxy) getChannel()).getTargetChannel().equals(this.targetChannel);
949+
}
950+
932951
/**
933952
* Increment and return the current epoch for this consumer; consumersMonitor must
934953
* be held.

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/DirectMessageListenerContainerMockTests.java

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2018 the original author or authors.
2+
* Copyright 2017-2019 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.
@@ -43,6 +43,7 @@
4343
import org.junit.Test;
4444
import org.mockito.Mockito;
4545

46+
import org.springframework.amqp.core.AcknowledgeMode;
4647
import org.springframework.amqp.core.MessageListener;
4748
import org.springframework.amqp.rabbit.connection.ChannelProxy;
4849
import org.springframework.amqp.rabbit.connection.Connection;
@@ -177,7 +178,7 @@ else if (i.getArgument(0).equals(17L)) {
177178
}
178179
Thread.sleep(200);
179180
consumer.get().handleDelivery("consumerTag", envelope(16), props, body);
180-
// should get 2 acks #10 and #6 (timeout)
181+
// should get 2 acks #10 and #16 (timeout)
181182
assertTrue(latch2.await(10, TimeUnit.SECONDS));
182183
consumer.get().handleDelivery("consumerTag", envelope(17), props, body);
183184
verify(channel).basicAck(10L, true);
@@ -311,6 +312,56 @@ public void testMonitorCancelsAfterBadAckEvenIfChannelReportsOpen() throws Excep
311312
container.stop();
312313
}
313314

315+
@Test
316+
public void testMonitorCancelsAfterTargetChannelChanges() throws Exception {
317+
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
318+
Connection connection = mock(Connection.class);
319+
ChannelProxy channel = mock(ChannelProxy.class);
320+
Channel rabbitChannel1 = mock(Channel.class);
321+
Channel rabbitChannel2 = mock(Channel.class);
322+
AtomicReference<Channel> target = new AtomicReference<>(rabbitChannel1);
323+
willAnswer(inv -> {
324+
return target.get();
325+
}).given(channel).getTargetChannel();
326+
327+
given(connectionFactory.createConnection()).willReturn(connection);
328+
given(connection.createChannel(anyBoolean())).willReturn(channel);
329+
given(channel.isOpen()).willReturn(true);
330+
given(channel.queueDeclarePassive(Mockito.anyString()))
331+
.willAnswer(invocation -> mock(AMQP.Queue.DeclareOk.class));
332+
AtomicReference<Consumer> consumer = new AtomicReference<>();
333+
final CountDownLatch latch1 = new CountDownLatch(1);
334+
final CountDownLatch latch2 = new CountDownLatch(1);
335+
willAnswer(inv -> {
336+
consumer.set(inv.getArgument(6));
337+
latch1.countDown();
338+
return "consumerTag";
339+
}).given(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(),
340+
anyMap(), any(Consumer.class));
341+
342+
willAnswer(inv -> {
343+
consumer.get().handleCancelOk("consumerTag");
344+
latch2.countDown();
345+
return null;
346+
}).given(channel).basicCancel("consumerTag");
347+
348+
DirectMessageListenerContainer container = new DirectMessageListenerContainer(connectionFactory);
349+
container.setQueueNames("test");
350+
container.setPrefetchCount(2);
351+
container.setMonitorInterval(100);
352+
container.setMessageListener(msg -> {
353+
target.set(rabbitChannel2);
354+
});
355+
container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
356+
container.afterPropertiesSet();
357+
container.start();
358+
359+
assertTrue(latch1.await(10, TimeUnit.SECONDS));
360+
consumer.get().handleDelivery("consumerTag", envelope(1L), new BasicProperties(), new byte[1]);
361+
assertTrue(latch2.await(10, TimeUnit.SECONDS));
362+
container.stop();
363+
}
364+
314365
private Envelope envelope(long tag) {
315366
return new Envelope(tag, false, "", "");
316367
}

0 commit comments

Comments
 (0)