Skip to content

Commit bda5221

Browse files
artembilangaryrussell
authored andcommitted
Fix SmartLifecycle.stop(Runnable) usage (#2973)
* Fix `SmartLifecycle.stop(Runnable)` usage We always have to call `callback` in the `SmartLifecycle.stop(Runnable)` implementation independently of component state * Fix `StandardIntegrationFlow.stop(Runnable)` for a logic when we don't have any `this.lifecycles` * Remove those `stop(Runnable)` which are fully equivalent of the `default` on in the `SmartLifecycle` * Some other simple polishing for the affected classes, e.g. `isSingleton()` is `default` with `true` in the `InitializingBean` **Cherry-pick to 5.1.x** * * Fix checkstyle violation
1 parent a75f080 commit bda5221

File tree

13 files changed

+78
-151
lines changed

13 files changed

+78
-151
lines changed

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,6 @@ private void adviceChain() {
246246
}
247247
}
248248

249-
@Override
250-
public boolean isSingleton() {
251-
return true;
252-
}
253-
254249
@Override
255250
public AbstractEndpoint getObject() {
256251
if (!this.initialized) {
@@ -302,8 +297,9 @@ else if (channel instanceof PollableChannel) {
302297
}
303298

304299
private void eventDrivenConsumer(MessageChannel channel) {
305-
Assert.isNull(this.pollerMetadata, "A poller should not be specified for endpoint '" + this.beanName
306-
+ "', since '" + channel + "' is a SubscribableChannel (not pollable).");
300+
Assert.isNull(this.pollerMetadata,
301+
() -> "A poller should not be specified for endpoint '" + this.beanName
302+
+ "', since '" + channel + "' is a SubscribableChannel (not pollable).");
307303
this.endpoint = new EventDrivenConsumer((SubscribableChannel) channel, this.handler);
308304
if (logger.isWarnEnabled()
309305
&& Boolean.FALSE.equals(this.autoStartup)
@@ -316,8 +312,9 @@ private void pollingConsumer(MessageChannel channel) {
316312
PollingConsumer pollingConsumer = new PollingConsumer((PollableChannel) channel, this.handler);
317313
if (this.pollerMetadata == null) {
318314
this.pollerMetadata = PollerMetadata.getDefaultPollerMetadata(this.beanFactory);
319-
Assert.notNull(this.pollerMetadata, "No poller has been defined for endpoint '" + this.beanName
320-
+ "', and no default poller is available within the context.");
315+
Assert.notNull(this.pollerMetadata,
316+
() -> "No poller has been defined for endpoint '" + this.beanName
317+
+ "', and no default poller is available within the context.");
321318
}
322319
pollingConsumer.setTaskExecutor(this.pollerMetadata.getTaskExecutor());
323320
pollingConsumer.setTrigger(this.pollerMetadata.getTrigger());

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,6 @@ public Class<?> getObjectType() {
157157
return SourcePollingChannelAdapter.class;
158158
}
159159

160-
@Override
161-
public boolean isSingleton() {
162-
return true;
163-
}
164-
165160
private void initializeAdapter() {
166161
synchronized (this.initializationMonitor) {
167162
if (this.initialized) {

spring-integration-core/src/main/java/org/springframework/integration/dsl/StandardIntegrationFlow.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,22 @@ public void start() {
127127

128128
@Override
129129
public void stop(Runnable callback) {
130-
AggregatingCallback aggregatingCallback = new AggregatingCallback(this.lifecycles.size(), callback);
131-
ListIterator<SmartLifecycle> iterator = this.lifecycles.listIterator(this.lifecycles.size());
132-
while (iterator.hasPrevious()) {
133-
SmartLifecycle lifecycle = iterator.previous();
134-
if (lifecycle.isRunning()) {
135-
lifecycle.stop(aggregatingCallback);
136-
}
137-
else {
138-
aggregatingCallback.run();
130+
if (this.lifecycles.size() > 0) {
131+
AggregatingCallback aggregatingCallback = new AggregatingCallback(this.lifecycles.size(), callback);
132+
ListIterator<SmartLifecycle> iterator = this.lifecycles.listIterator(this.lifecycles.size());
133+
while (iterator.hasPrevious()) {
134+
SmartLifecycle lifecycle = iterator.previous();
135+
if (lifecycle.isRunning()) {
136+
lifecycle.stop(aggregatingCallback);
137+
}
138+
else {
139+
aggregatingCallback.run();
140+
}
139141
}
140142
}
143+
else {
144+
callback.run();
145+
}
141146
this.running = false;
142147
}
143148

spring-integration-core/src/main/java/org/springframework/integration/history/MessageHistoryConfigurer.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
@IntegrationManagedResource
5353
public class MessageHistoryConfigurer implements SmartLifecycle, BeanFactoryAware, DestructionAwareBeanPostProcessor {
5454

55-
private final Log logger = LogFactory.getLog(this.getClass());
55+
private static final Log logger = LogFactory.getLog(MessageHistoryConfigurer.class);
5656

5757
private final Set<TrackableComponent> currentlyTrackedComponents = ConcurrentHashMap.newKeySet();
5858

@@ -230,12 +230,6 @@ public void stop() {
230230
}
231231
}
232232

233-
@Override
234-
public void stop(Runnable callback) {
235-
this.stop();
236-
callback.run();
237-
}
238-
239233
private static Collection<TrackableComponent> getTrackableComponents(ListableBeanFactory beanFactory) {
240234
return BeanFactoryUtils.beansOfTypeIncludingAncestors(beanFactory, TrackableComponent.class).values();
241235
}

spring-integration-core/src/main/java/org/springframework/integration/store/MessageGroupStoreReaper.java

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,19 @@ public class MessageGroupStoreReaper implements Runnable, DisposableBean, Initia
3939

4040
private static Log logger = LogFactory.getLog(MessageGroupStoreReaper.class);
4141

42+
private final ReentrantLock lifecycleLock = new ReentrantLock();
43+
4244
private MessageGroupStore messageGroupStore;
4345

4446
private boolean expireOnDestroy = false;
4547

4648
private long timeout = -1;
4749

48-
private volatile boolean running;
49-
50-
private final ReentrantLock lifecycleLock = new ReentrantLock();
50+
private int phase = 0;
5151

52-
private volatile int phase = 0;
52+
private boolean autoStartup = true;
5353

54-
private volatile boolean autoStartup = true;
54+
private volatile boolean running;
5555

5656
public MessageGroupStoreReaper(MessageGroupStore messageGroupStore) {
5757
this.messageGroupStore = messageGroupStore;
@@ -63,7 +63,6 @@ public MessageGroupStoreReaper() {
6363
/**
6464
* Flag to indicate that the stores should be expired when this component is destroyed (i.e. usually when its
6565
* enclosing {@link org.springframework.context.ApplicationContext} is closed).
66-
*
6766
* @param expireOnDestroy the flag value to set
6867
*/
6968
public void setExpireOnDestroy(boolean expireOnDestroy) {
@@ -73,7 +72,6 @@ public void setExpireOnDestroy(boolean expireOnDestroy) {
7372
/**
7473
* Timeout in milliseconds (default -1). If negative then no groups ever time out. If greater than zero then all
7574
* groups older than that value are expired when this component is {@link #run()}.
76-
*
7775
* @param timeout the timeout to set
7876
*/
7977
public void setTimeout(long timeout) {
@@ -82,7 +80,6 @@ public void setTimeout(long timeout) {
8280

8381
/**
8482
* A message group store to expire according the other configurations.
85-
*
8683
* @param messageGroupStore the {@link MessageGroupStore} to set
8784
*/
8885
public void setMessageGroupStore(MessageGroupStore messageGroupStore) {
@@ -187,16 +184,4 @@ public void setAutoStartup(boolean autoStartup) {
187184
this.autoStartup = autoStartup;
188185
}
189186

190-
@Override
191-
public void stop(Runnable callback) {
192-
this.lifecycleLock.lock();
193-
try {
194-
this.stop();
195-
callback.run();
196-
}
197-
finally {
198-
this.lifecycleLock.unlock();
199-
}
200-
}
201-
202187
}

spring-integration-core/src/main/java/org/springframework/integration/support/leader/LockRegistryLeaderInitiator.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,6 @@ public void destroy() {
308308
}
309309
}
310310

311-
@Override
312-
public void stop(Runnable runnable) {
313-
stop();
314-
runnable.run();
315-
}
316-
317311
/**
318312
* Stop the registration of the {@link #candidate} for leader election. If the
319313
* candidate is currently leader, its leadership will be revoked.

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

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
* @author Oleg Zhurakousky
5252
* @author Gary Russell
5353
* @author Artem Bilan
54+
*
5455
* @since 2.0
5556
*/
5657
public class JmsChannelFactoryBean extends AbstractFactoryBean<AbstractJmsChannel>
@@ -64,83 +65,83 @@ public class JmsChannelFactoryBean extends AbstractFactoryBean<AbstractJmsChanne
6465

6566
private final JmsTemplate jmsTemplate = new DynamicJmsTemplate();
6667

67-
private volatile AbstractMessageListenerContainer listenerContainer;
68+
private AbstractMessageListenerContainer listenerContainer;
6869

69-
private volatile Class<? extends AbstractMessageListenerContainer> containerType;
70+
private Class<? extends AbstractMessageListenerContainer> containerType;
7071

71-
private volatile boolean acceptMessagesWhileStopping;
72+
private boolean acceptMessagesWhileStopping;
7273

73-
private volatile boolean autoStartup = true;
74+
private boolean autoStartup = true;
7475

75-
private volatile String cacheLevelName;
76+
private String cacheLevelName;
7677

77-
private volatile Integer cacheLevel;
78+
private Integer cacheLevel;
7879

79-
private volatile String clientId;
80+
private String clientId;
8081

81-
private volatile String concurrency;
82+
private String concurrency;
8283

83-
private volatile Integer concurrentConsumers;
84+
private Integer concurrentConsumers;
8485

85-
private volatile ConnectionFactory connectionFactory;
86+
private ConnectionFactory connectionFactory;
8687

87-
private volatile Destination destination;
88+
private Destination destination;
8889

89-
private volatile String destinationName;
90+
private String destinationName;
9091

91-
private volatile DestinationResolver destinationResolver;
92+
private DestinationResolver destinationResolver;
9293

93-
private volatile String durableSubscriptionName;
94+
private String durableSubscriptionName;
9495

95-
private volatile ErrorHandler errorHandler;
96+
private ErrorHandler errorHandler;
9697

97-
private volatile ExceptionListener exceptionListener;
98+
private ExceptionListener exceptionListener;
9899

99-
private volatile Boolean exposeListenerSession;
100+
private Boolean exposeListenerSession;
100101

101-
private volatile Integer idleTaskExecutionLimit;
102+
private Integer idleTaskExecutionLimit;
102103

103-
private volatile Integer maxConcurrentConsumers;
104+
private Integer maxConcurrentConsumers;
104105

105-
private volatile Integer maxMessagesPerTask;
106+
private Integer maxMessagesPerTask;
106107

107-
private volatile String messageSelector;
108+
private String messageSelector;
108109

109-
private volatile Integer phase;
110+
private Integer phase;
110111

111-
private volatile Boolean pubSubDomain;
112+
private Boolean pubSubDomain;
112113

113-
private volatile boolean pubSubNoLocal;
114+
private boolean pubSubNoLocal;
114115

115-
private volatile Long receiveTimeout;
116+
private Long receiveTimeout;
116117

117-
private volatile Long recoveryInterval;
118+
private Long recoveryInterval;
118119

119-
private volatile String beanName;
120+
private String beanName;
120121

121-
private volatile boolean subscriptionShared;
122+
private boolean subscriptionShared;
122123

123124
/**
124125
* This value differs from the container implementations' default (which is AUTO_ACKNOWLEDGE)
125126
*/
126-
private volatile int sessionAcknowledgeMode = Session.SESSION_TRANSACTED;
127+
private int sessionAcknowledgeMode = Session.SESSION_TRANSACTED;
127128

128129
/**
129130
* This value differs from the container implementations' default (which is false).
130131
*/
131-
private volatile boolean sessionTransacted = true;
132+
private boolean sessionTransacted = true;
132133

133-
private volatile boolean subscriptionDurable;
134+
private boolean subscriptionDurable;
134135

135-
private volatile Executor taskExecutor;
136+
private Executor taskExecutor;
136137

137-
private volatile PlatformTransactionManager transactionManager;
138+
private PlatformTransactionManager transactionManager;
138139

139-
private volatile String transactionName;
140+
private String transactionName;
140141

141-
private volatile Integer transactionTimeout;
142+
private Integer transactionTimeout;
142143

143-
private volatile int maxSubscribers = Integer.MAX_VALUE;
144+
private int maxSubscribers = Integer.MAX_VALUE;
144145

145146

146147
public JmsChannelFactoryBean() {
@@ -204,8 +205,7 @@ public void setAutoStartup(boolean autoStartup) {
204205

205206
public void setCacheLevelName(String cacheLevelName) {
206207
Assert.isTrue(this.messageDriven, "'cacheLevelName' is allowed only in case of 'messageDriven = true'");
207-
Assert.state(this.cacheLevel == null,
208-
"'cacheLevelName' and 'cacheLevel' are mutually exclusive");
208+
Assert.state(this.cacheLevel == null, "'cacheLevelName' and 'cacheLevel' are mutually exclusive");
209209
this.cacheLevelName = cacheLevelName;
210210
}
211211

@@ -376,7 +376,8 @@ protected AbstractJmsChannel createInstance() {
376376
this.initializeJmsTemplate();
377377
if (this.messageDriven) {
378378
this.listenerContainer = createContainer();
379-
SubscribableJmsChannel subscribableJmsChannel = new SubscribableJmsChannel(this.listenerContainer, this.jmsTemplate);
379+
SubscribableJmsChannel subscribableJmsChannel =
380+
new SubscribableJmsChannel(this.listenerContainer, this.jmsTemplate);
380381
subscribableJmsChannel.setMaxSubscribers(this.maxSubscribers);
381382
this.channel = subscribableJmsChannel;
382383
}
@@ -519,9 +520,7 @@ public void stop(Runnable callback) {
519520

520521
@Override
521522
protected void destroyInstance(AbstractJmsChannel instance) {
522-
if (instance instanceof SubscribableJmsChannel) {
523-
((SubscribableJmsChannel) this.channel).destroy();
524-
}
523+
instance.destroy();
525524
}
526525

527526
}

spring-integration-stomp/src/main/java/org/springframework/integration/stomp/AbstractStompSessionManager.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -306,16 +306,6 @@ public void start() {
306306
}
307307
}
308308

309-
@Override
310-
public void stop(Runnable callback) {
311-
synchronized (this.lifecycleMonitor) {
312-
stop();
313-
if (callback != null) {
314-
callback.run();
315-
}
316-
}
317-
}
318-
319309
@Override
320310
public void stop() {
321311
synchronized (this.lifecycleMonitor) {

spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/config/XmppConnectionFactoryBean.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,6 @@ public void stop() {
207207
}
208208
}
209209

210-
@Override
211-
public void stop(Runnable callback) {
212-
stop();
213-
callback.run();
214-
}
215-
216210
@Override
217211
public boolean isRunning() {
218212
return this.running;

spring-integration-zookeeper/src/main/java/org/springframework/integration/zookeeper/config/CuratorFrameworkFactoryBean.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,6 @@ public void stop() {
129129
}
130130
}
131131

132-
@Override
133-
public void stop(Runnable runnable) {
134-
stop();
135-
runnable.run();
136-
}
137-
138132
@Override
139133
public CuratorFramework getObject() {
140134
return this.client;
@@ -145,9 +139,4 @@ public Class<?> getObjectType() {
145139
return CuratorFramework.class;
146140
}
147141

148-
@Override
149-
public boolean isSingleton() {
150-
return true;
151-
}
152-
153142
}

0 commit comments

Comments
 (0)