Skip to content

Commit a0dccec

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

1 file changed

Lines changed: 21 additions & 4 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: 21 additions & 4 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;
@@ -103,7 +106,8 @@
103106
*
104107
*/
105108
public class TestChannelBinder extends
106-
AbstractMessageChannelBinder<ConsumerProperties, ProducerProperties, TestChannelBinderProvisioner> {
109+
AbstractMessageChannelBinder<ConsumerProperties, ProducerProperties, TestChannelBinderProvisioner>
110+
implements ApplicationListener<ApplicationEvent> {
107111

108112
@Autowired
109113
private BeanFactory beanFactory;
@@ -157,7 +161,7 @@ protected MessageProducer createConsumerEndpoint(ConsumerDestination destination
157161
.getChannel();
158162

159163
IntegrationMessageListeningContainer messageListenerContainer = new IntegrationMessageListeningContainer();
160-
IntegrationBinderInboundChannelAdapter adapter = new IntegrationBinderInboundChannelAdapter(
164+
IntegrationBinderInboundChannelAdapter adapter = new IntegrationBinderInboundChannelAdapter(siBinderInputChannel,
161165
messageListenerContainer);
162166
adapter.setBeanFactory(this.beanFactory);
163167
String groupName = StringUtils.hasText(group) ? group : "anonymous";
@@ -173,7 +177,6 @@ protected MessageProducer createConsumerEndpoint(ConsumerDestination destination
173177
}
174178

175179
siBinderInputChannel.subscribe(messageListenerContainer);
176-
177180
return adapter;
178181
}
179182

@@ -225,9 +228,12 @@ private static class IntegrationBinderInboundChannelAdapter
225228

226229
private RecoveryCallback<?> recoveryCallback;
227230

228-
IntegrationBinderInboundChannelAdapter(
231+
private final SubscribableChannel subscribableChannel;
232+
233+
IntegrationBinderInboundChannelAdapter(SubscribableChannel subscribableChannel,
229234
IntegrationMessageListeningContainer listenerContainer) {
230235
this.listenerContainer = listenerContainer;
236+
this.subscribableChannel = subscribableChannel;
231237
}
232238

233239
// Temporarily unused until DLQ strategy for this binder becomes a requirement
@@ -239,6 +245,12 @@ public void setRetryTemplate(RetryTemplate retryTemplate) {
239245
this.retryTemplate = retryTemplate;
240246
}
241247

248+
@Override
249+
protected void doStop() {
250+
super.doStop();
251+
this.subscribableChannel.unsubscribe(listenerContainer);
252+
}
253+
242254
@Override
243255
protected void onInit() {
244256
if (this.retryTemplate != null) {
@@ -294,4 +306,9 @@ private void processMessage(Message<?> message) {
294306

295307
}
296308

309+
@Override
310+
public void onApplicationEvent(ApplicationEvent event) {
311+
System.out.println("Event: " + event);
312+
}
313+
297314
}

0 commit comments

Comments
 (0)