Skip to content

Commit a74c9ef

Browse files
artembilangaryrussell
authored andcommitted
INT-4486: Properly implement stop(Runnable)
JIRA: https://jira.spring.io/browse/INT-4486 The `SmartLifecycle.stop(Runnable callback)` must always call the `callback` in the end independently of the internal state * Revise all the `SmartLifecycle` implementations for the proper `callback` handling **Cherry-pick to 5.0.x and 4.3.x**
1 parent 6f262d7 commit a74c9ef

File tree

10 files changed

+64
-35
lines changed

10 files changed

+64
-35
lines changed

spring-integration-amqp/src/main/java/org/springframework/integration/amqp/channel/AbstractSubscribableAmqpChannel.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -238,6 +238,9 @@ public void stop(Runnable callback) {
238238
if (this.container != null) {
239239
this.container.stop(callback);
240240
}
241+
else {
242+
callback.run();
243+
}
241244
}
242245

243246
@Override

spring-integration-amqp/src/main/java/org/springframework/integration/amqp/config/AmqpChannelFactoryBean.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -64,10 +64,11 @@
6464
* @author Mark Fisher
6565
* @author Gary Russell
6666
* @author Artem Bilan
67+
*
6768
* @since 2.1
6869
*/
69-
public class AmqpChannelFactoryBean extends AbstractFactoryBean<AbstractAmqpChannel> implements SmartLifecycle,
70-
DisposableBean, BeanNameAware {
70+
public class AmqpChannelFactoryBean extends AbstractFactoryBean<AbstractAmqpChannel>
71+
implements SmartLifecycle, DisposableBean, BeanNameAware {
7172

7273
private volatile AbstractAmqpChannel channel;
7374

@@ -492,13 +493,14 @@ public void stop(Runnable callback) {
492493
if (this.channel instanceof SmartLifecycle) {
493494
((SmartLifecycle) this.channel).stop(callback);
494495
}
496+
else {
497+
callback.run();
498+
}
495499
}
496500

497501
@Override
498502
protected void destroyInstance(AbstractAmqpChannel instance) throws Exception {
499-
if (instance instanceof DisposableBean) {
500-
((DisposableBean) this.channel).destroy();
501-
}
503+
this.channel.destroy();
502504
}
503505

504506
}

spring-integration-core/src/main/java/org/springframework/integration/config/ConsumerEndpointFactoryBean.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,9 @@ public void stop(Runnable callback) {
341341
if (this.endpoint != null) {
342342
this.endpoint.stop(callback);
343343
}
344+
else {
345+
callback.run();
346+
}
344347
}
345348

346349
}

spring-integration-core/src/main/java/org/springframework/integration/config/SourcePollingChannelAdapterFactoryBean.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -238,6 +238,9 @@ public void stop(Runnable callback) {
238238
if (this.adapter != null) {
239239
this.adapter.stop(callback);
240240
}
241+
else {
242+
callback.run();
243+
}
241244
}
242245

243246
}

spring-integration-core/src/main/java/org/springframework/integration/endpoint/AbstractEndpoint.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -154,6 +154,9 @@ public final void stop(Runnable callback) {
154154
logger.info("stopped " + this);
155155
}
156156
}
157+
else {
158+
callback.run();
159+
}
157160
}
158161
finally {
159162
this.lifecycleLock.unlock();

spring-integration-file/src/main/java/org/springframework/integration/file/config/FileTailInboundChannelAdapterFactoryBean.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -36,6 +36,7 @@
3636
* @author Gary Russell
3737
* @author Artem Bilan
3838
* @author Ali Shahbour
39+
*
3940
* @since 3.0
4041
*
4142
*/
@@ -181,6 +182,9 @@ public void stop(Runnable callback) {
181182
if (this.adapter != null) {
182183
this.adapter.stop(callback);
183184
}
185+
else {
186+
callback.run();
187+
}
184188
}
185189

186190
@Override

spring-integration-jms/src/main/java/org/springframework/integration/jms/SubscribableJmsChannel.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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,9 +43,12 @@
4343
/**
4444
* @author Mark Fisher
4545
* @author Gary Russell
46+
* @author Artem Bilan
47+
*
4648
* @since 2.0
4749
*/
48-
public class SubscribableJmsChannel extends AbstractJmsChannel implements SubscribableChannel, SmartLifecycle, DisposableBean {
50+
public class SubscribableJmsChannel extends AbstractJmsChannel
51+
implements SubscribableChannel, SmartLifecycle, DisposableBean {
4952

5053
private final AbstractMessageListenerContainer container;
5154

@@ -72,13 +75,15 @@ public void setMaxSubscribers(int maxSubscribers) {
7275

7376
@Override
7477
public boolean subscribe(MessageHandler handler) {
75-
Assert.state(this.dispatcher != null, "'MessageDispatcher' must not be null. This channel might not have been initialized");
78+
Assert.state(this.dispatcher != null,
79+
"'MessageDispatcher' must not be null. This channel might not have been initialized");
7680
return this.dispatcher.addHandler(handler);
7781
}
7882

7983
@Override
8084
public boolean unsubscribe(MessageHandler handler) {
81-
Assert.state(this.dispatcher != null, "'MessageDispatcher' must not be null. This channel might not have been initialized");
85+
Assert.state(this.dispatcher != null,
86+
"'MessageDispatcher' must not be null. This channel might not have been initialized");
8287
return this.dispatcher.removeHandler(handler);
8388
}
8489

@@ -126,7 +131,7 @@ private void configureDispatcher(boolean isPubSub) {
126131

127132
@Override
128133
public boolean isAutoStartup() {
129-
return (this.container != null) ? this.container.isAutoStartup() : false;
134+
return (this.container != null) && this.container.isAutoStartup();
130135
}
131136

132137
@Override
@@ -136,7 +141,7 @@ public int getPhase() {
136141

137142
@Override
138143
public boolean isRunning() {
139-
return (this.container != null) ? this.container.isRunning() : false;
144+
return (this.container != null) && this.container.isRunning();
140145
}
141146

142147
@Override
@@ -158,6 +163,9 @@ public void stop(Runnable callback) {
158163
if (this.container != null) {
159164
this.container.stop(callback);
160165
}
166+
else {
167+
callback.run();
168+
}
161169
}
162170

163171
@Override
@@ -226,6 +234,7 @@ else if (this.logger.isWarnEnabled()) {
226234
throw new MessagingException("failed to handle incoming JMS Message", e);
227235
}
228236
}
237+
229238
}
230239

231240
}

spring-integration-jms/src/main/java/org/springframework/integration/jms/config/JmsChannelFactoryBean.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -548,6 +548,9 @@ public void stop(Runnable callback) {
548548
if (this.channel instanceof SubscribableJmsChannel) {
549549
((SubscribableJmsChannel) this.channel).stop(callback);
550550
}
551+
else {
552+
callback.run();
553+
}
551554
}
552555

553556
@Override

spring-integration-redis/src/main/java/org/springframework/integration/redis/channel/SubscribableRedisChannel.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -51,6 +51,7 @@
5151
* @author Oleg Zhurakousky
5252
* @author Gary Russell
5353
* @author Artem Bilan
54+
*
5455
* @since 2.0
5556
*/
5657
@SuppressWarnings("rawtypes")
@@ -105,7 +106,6 @@ public void setSerializer(RedisSerializer<?> serializer) {
105106
/**
106107
* Specify the maximum number of subscribers supported by the
107108
* channel's dispatcher.
108-
*
109109
* @param maxSubscribers The maximum number of subscribers allowed.
110110
*/
111111
public void setMaxSubscribers(int maxSubscribers) {
@@ -169,45 +169,37 @@ public void onInit() throws Exception {
169169

170170
@Override
171171
public boolean isAutoStartup() {
172-
return (this.container != null) && this.container.isAutoStartup();
172+
return this.container.isAutoStartup();
173173
}
174174

175175
@Override
176176
public int getPhase() {
177-
return (this.container != null) ? this.container.getPhase() : 0;
177+
return this.container.getPhase();
178178
}
179179

180180
@Override
181181
public boolean isRunning() {
182-
return (this.container != null) && this.container.isRunning();
182+
return this.container.isRunning();
183183
}
184184

185185
@Override
186186
public void start() {
187-
if (this.container != null) {
188-
this.container.start();
189-
}
187+
this.container.start();
190188
}
191189

192190
@Override
193191
public void stop() {
194-
if (this.container != null) {
195-
this.container.stop();
196-
}
192+
this.container.stop();
197193
}
198194

199195
@Override
200196
public void stop(Runnable callback) {
201-
if (this.container != null) {
202-
this.container.stop(callback);
203-
}
197+
this.container.stop(callback);
204198
}
205199

206200
@Override
207201
public void destroy() throws Exception {
208-
if (this.container != null) {
209-
this.container.destroy();
210-
}
202+
this.container.destroy();
211203
}
212204

213205
private class MessageListenerDelegate {
@@ -227,6 +219,7 @@ public void handleMessage(Object payload) {
227219
+ "' (" + SubscribableRedisChannel.this.getFullChannelName() + ").", e);
228220
}
229221
}
222+
230223
}
231224

232225
}

spring-integration-syslog/src/main/java/org/springframework/integration/syslog/config/SyslogReceivingChannelAdapterFactoryBean.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -32,7 +32,10 @@
3232

3333
/**
3434
* Factory bean to create syslog inbound adapters (UDP or TCP).
35+
*
3536
* @author Gary Russell
37+
* @author Artem Bilan
38+
*
3639
* @since 3.0
3740
*
3841
*/
@@ -161,6 +164,9 @@ public void stop(Runnable callback) {
161164
if (this.adapter != null) {
162165
this.adapter.stop(callback);
163166
}
167+
else {
168+
callback.run();
169+
}
164170
}
165171

166172
@Override

0 commit comments

Comments
 (0)