Skip to content

Commit 0cb9836

Browse files
committed
GH-3234 Ensure there are no duplicate subscribers in TestChannelBinder
Resolves #3234 Signed-off-by: Oleg Zhurakousky <ozhurakousky@vmware.com> Polishing
1 parent 9f44a65 commit 0cb9836

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

  • core/spring-cloud-stream-test-binder/src/main/java/org/springframework/cloud/stream/binder/test

core/spring-cloud-stream-test-binder/src/main/java/org/springframework/cloud/stream/binder/test/TestChannelBinder.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.function.Consumer;
2020

21+
import org.reactivestreams.Subscription;
2122
import org.springframework.beans.factory.BeanFactory;
2223
import org.springframework.beans.factory.annotation.Autowired;
2324
import org.springframework.cloud.stream.binder.AbstractMessageChannelBinder;
@@ -28,6 +29,8 @@
2829
import org.springframework.cloud.stream.binder.test.TestChannelBinderProvisioner.SpringIntegrationProducerDestination;
2930
import org.springframework.cloud.stream.provisioning.ConsumerDestination;
3031
import org.springframework.cloud.stream.provisioning.ProducerDestination;
32+
import org.springframework.context.ApplicationEvent;
33+
import org.springframework.context.ApplicationListener;
3134
import org.springframework.core.retry.RetryException;
3235
import org.springframework.integration.IntegrationMessageHeaderAccessor;
3336
import org.springframework.integration.acks.AcknowledgmentCallback;
@@ -157,7 +160,7 @@ protected MessageProducer createConsumerEndpoint(ConsumerDestination destination
157160
.getChannel();
158161

159162
IntegrationMessageListeningContainer messageListenerContainer = new IntegrationMessageListeningContainer();
160-
IntegrationBinderInboundChannelAdapter adapter = new IntegrationBinderInboundChannelAdapter(
163+
IntegrationBinderInboundChannelAdapter adapter = new IntegrationBinderInboundChannelAdapter(siBinderInputChannel,
161164
messageListenerContainer);
162165
adapter.setBeanFactory(this.beanFactory);
163166
String groupName = StringUtils.hasText(group) ? group : "anonymous";
@@ -173,7 +176,6 @@ protected MessageProducer createConsumerEndpoint(ConsumerDestination destination
173176
}
174177

175178
siBinderInputChannel.subscribe(messageListenerContainer);
176-
177179
return adapter;
178180
}
179181

@@ -225,9 +227,12 @@ private static class IntegrationBinderInboundChannelAdapter
225227

226228
private RecoveryCallback<?> recoveryCallback;
227229

228-
IntegrationBinderInboundChannelAdapter(
230+
private final SubscribableChannel subscribableChannel;
231+
232+
IntegrationBinderInboundChannelAdapter(SubscribableChannel subscribableChannel,
229233
IntegrationMessageListeningContainer listenerContainer) {
230234
this.listenerContainer = listenerContainer;
235+
this.subscribableChannel = subscribableChannel;
231236
}
232237

233238
// Temporarily unused until DLQ strategy for this binder becomes a requirement
@@ -239,6 +244,12 @@ public void setRetryTemplate(RetryTemplate retryTemplate) {
239244
this.retryTemplate = retryTemplate;
240245
}
241246

247+
@Override
248+
protected void doStop() {
249+
super.doStop();
250+
this.subscribableChannel.unsubscribe(listenerContainer);
251+
}
252+
242253
@Override
243254
protected void onInit() {
244255
if (this.retryTemplate != null) {

0 commit comments

Comments
 (0)