1818
1919import java .util .function .Consumer ;
2020
21+ import org .reactivestreams .Subscription ;
2122import org .springframework .beans .factory .BeanFactory ;
2223import org .springframework .beans .factory .annotation .Autowired ;
2324import org .springframework .cloud .stream .binder .AbstractMessageChannelBinder ;
2829import org .springframework .cloud .stream .binder .test .TestChannelBinderProvisioner .SpringIntegrationProducerDestination ;
2930import org .springframework .cloud .stream .provisioning .ConsumerDestination ;
3031import org .springframework .cloud .stream .provisioning .ProducerDestination ;
32+ import org .springframework .context .ApplicationEvent ;
33+ import org .springframework .context .ApplicationListener ;
3134import org .springframework .core .retry .RetryException ;
3235import org .springframework .integration .IntegrationMessageHeaderAccessor ;
3336import org .springframework .integration .acks .AcknowledgmentCallback ;
103106 *
104107 */
105108public 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