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
This commit is contained in:
Artem Bilan
2019-06-21 16:50:38 -04:00
committed by Gary Russell
parent a75f0808fd
commit bda5221720
13 changed files with 79 additions and 152 deletions

View File

@@ -246,11 +246,6 @@ public class ConsumerEndpointFactoryBean
}
}
@Override
public boolean isSingleton() {
return true;
}
@Override
public AbstractEndpoint getObject() {
if (!this.initialized) {
@@ -302,8 +297,9 @@ public class ConsumerEndpointFactoryBean
}
private void eventDrivenConsumer(MessageChannel channel) {
Assert.isNull(this.pollerMetadata, "A poller should not be specified for endpoint '" + this.beanName
+ "', since '" + channel + "' is a SubscribableChannel (not pollable).");
Assert.isNull(this.pollerMetadata,
() -> "A poller should not be specified for endpoint '" + this.beanName
+ "', since '" + channel + "' is a SubscribableChannel (not pollable).");
this.endpoint = new EventDrivenConsumer((SubscribableChannel) channel, this.handler);
if (logger.isWarnEnabled()
&& Boolean.FALSE.equals(this.autoStartup)
@@ -316,8 +312,9 @@ public class ConsumerEndpointFactoryBean
PollingConsumer pollingConsumer = new PollingConsumer((PollableChannel) channel, this.handler);
if (this.pollerMetadata == null) {
this.pollerMetadata = PollerMetadata.getDefaultPollerMetadata(this.beanFactory);
Assert.notNull(this.pollerMetadata, "No poller has been defined for endpoint '" + this.beanName
+ "', and no default poller is available within the context.");
Assert.notNull(this.pollerMetadata,
() -> "No poller has been defined for endpoint '" + this.beanName
+ "', and no default poller is available within the context.");
}
pollingConsumer.setTaskExecutor(this.pollerMetadata.getTaskExecutor());
pollingConsumer.setTrigger(this.pollerMetadata.getTrigger());

View File

@@ -157,11 +157,6 @@ public class SourcePollingChannelAdapterFactoryBean implements FactoryBean<Sourc
return SourcePollingChannelAdapter.class;
}
@Override
public boolean isSingleton() {
return true;
}
private void initializeAdapter() {
synchronized (this.initializationMonitor) {
if (this.initialized) {

View File

@@ -127,17 +127,22 @@ public class StandardIntegrationFlow implements IntegrationFlow, SmartLifecycle
@Override
public void stop(Runnable callback) {
AggregatingCallback aggregatingCallback = new AggregatingCallback(this.lifecycles.size(), callback);
ListIterator<SmartLifecycle> iterator = this.lifecycles.listIterator(this.lifecycles.size());
while (iterator.hasPrevious()) {
SmartLifecycle lifecycle = iterator.previous();
if (lifecycle.isRunning()) {
lifecycle.stop(aggregatingCallback);
}
else {
aggregatingCallback.run();
if (this.lifecycles.size() > 0) {
AggregatingCallback aggregatingCallback = new AggregatingCallback(this.lifecycles.size(), callback);
ListIterator<SmartLifecycle> iterator = this.lifecycles.listIterator(this.lifecycles.size());
while (iterator.hasPrevious()) {
SmartLifecycle lifecycle = iterator.previous();
if (lifecycle.isRunning()) {
lifecycle.stop(aggregatingCallback);
}
else {
aggregatingCallback.run();
}
}
}
else {
callback.run();
}
this.running = false;
}

View File

@@ -52,7 +52,7 @@ import org.springframework.util.StringUtils;
@IntegrationManagedResource
public class MessageHistoryConfigurer implements SmartLifecycle, BeanFactoryAware, DestructionAwareBeanPostProcessor {
private final Log logger = LogFactory.getLog(this.getClass());
private static final Log logger = LogFactory.getLog(MessageHistoryConfigurer.class);
private final Set<TrackableComponent> currentlyTrackedComponents = ConcurrentHashMap.newKeySet();
@@ -230,12 +230,6 @@ public class MessageHistoryConfigurer implements SmartLifecycle, BeanFactoryAwar
}
}
@Override
public void stop(Runnable callback) {
this.stop();
callback.run();
}
private static Collection<TrackableComponent> getTrackableComponents(ListableBeanFactory beanFactory) {
return BeanFactoryUtils.beansOfTypeIncludingAncestors(beanFactory, TrackableComponent.class).values();
}

View File

@@ -39,20 +39,20 @@ public class MessageGroupStoreReaper implements Runnable, DisposableBean, Initia
private static Log logger = LogFactory.getLog(MessageGroupStoreReaper.class);
private final ReentrantLock lifecycleLock = new ReentrantLock();
private MessageGroupStore messageGroupStore;
private boolean expireOnDestroy = false;
private long timeout = -1;
private int phase = 0;
private boolean autoStartup = true;
private volatile boolean running;
private final ReentrantLock lifecycleLock = new ReentrantLock();
private volatile int phase = 0;
private volatile boolean autoStartup = true;
public MessageGroupStoreReaper(MessageGroupStore messageGroupStore) {
this.messageGroupStore = messageGroupStore;
}
@@ -63,7 +63,6 @@ public class MessageGroupStoreReaper implements Runnable, DisposableBean, Initia
/**
* Flag to indicate that the stores should be expired when this component is destroyed (i.e. usually when its
* enclosing {@link org.springframework.context.ApplicationContext} is closed).
*
* @param expireOnDestroy the flag value to set
*/
public void setExpireOnDestroy(boolean expireOnDestroy) {
@@ -73,7 +72,6 @@ public class MessageGroupStoreReaper implements Runnable, DisposableBean, Initia
/**
* Timeout in milliseconds (default -1). If negative then no groups ever time out. If greater than zero then all
* groups older than that value are expired when this component is {@link #run()}.
*
* @param timeout the timeout to set
*/
public void setTimeout(long timeout) {
@@ -82,7 +80,6 @@ public class MessageGroupStoreReaper implements Runnable, DisposableBean, Initia
/**
* A message group store to expire according the other configurations.
*
* @param messageGroupStore the {@link MessageGroupStore} to set
*/
public void setMessageGroupStore(MessageGroupStore messageGroupStore) {
@@ -187,16 +184,4 @@ public class MessageGroupStoreReaper implements Runnable, DisposableBean, Initia
this.autoStartup = autoStartup;
}
@Override
public void stop(Runnable callback) {
this.lifecycleLock.lock();
try {
this.stop();
callback.run();
}
finally {
this.lifecycleLock.unlock();
}
}
}

View File

@@ -308,12 +308,6 @@ public class LockRegistryLeaderInitiator implements SmartLifecycle, DisposableBe
}
}
@Override
public void stop(Runnable runnable) {
stop();
runnable.run();
}
/**
* Stop the registration of the {@link #candidate} for leader election. If the
* candidate is currently leader, its leadership will be revoked.