Make IOS.setTaskScheduler() as public (#2725)

* Make IOS.setTaskScheduler() as public

* Make `IntegrationObjectSupport.setTaskScheduler()` as `public` and
remove all the overrides for visibility
* Fix Sonar smells for all the affected classes

* * Fix `throws Exception` for affected classes to avoid compilation errors

* * Fix "merely rethrow" smell
* Revert `throws Exception` for `AbstractEndpoint.destroy()`

* * Catch a couple exceptions from `destroy()`
This commit is contained in:
Artem Bilan
2019-01-30 13:59:37 -05:00
committed by Gary Russell
parent 7980afef9b
commit 020ea76d59
10 changed files with 158 additions and 151 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,8 +28,6 @@ import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.locks.Lock;
import org.aopalliance.aop.Advice;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.BeanFactory;
@@ -57,7 +55,6 @@ import org.springframework.integration.util.UUIDConverter;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageDeliveryException;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
@@ -96,8 +93,6 @@ import org.springframework.util.CollectionUtils;
public abstract class AbstractCorrelatingMessageHandler extends AbstractMessageProducingHandler
implements DiscardingMessageHandler, DisposableBean, ApplicationEventPublisherAware, Lifecycle {
protected final Log logger = LogFactory.getLog(getClass());
private final Comparator<Message<?>> sequenceNumberComparator = new MessageSequenceComparator();
private final Map<UUID, ScheduledFuture<?>> expireGroupScheduledFutures = new ConcurrentHashMap<>();
@@ -286,11 +281,6 @@ public abstract class AbstractCorrelatingMessageHandler extends AbstractMessageP
this.popSequence = popSequence;
}
@Override
public void setTaskScheduler(TaskScheduler taskScheduler) {
super.setTaskScheduler(taskScheduler);
}
protected boolean isReleaseLockBeforeSend() {
return this.releaseLockBeforeSend;
}
@@ -444,7 +434,7 @@ public abstract class AbstractCorrelatingMessageHandler extends AbstractMessageP
}
@Override
protected void handleMessageInternal(Message<?> message) throws Exception {
protected void handleMessageInternal(Message<?> message) throws InterruptedException {
Object correlationKey = this.correlationStrategy.getCorrelationKey(message);
Assert.state(correlationKey != null,
"Null correlation not allowed. Maybe the CorrelationStrategy is failing?");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2018 the original author or authors.
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,7 +30,6 @@ import org.springframework.integration.context.IntegrationObjectSupport;
import org.springframework.integration.support.channel.HeaderChannelRegistry;
import org.springframework.lang.Nullable;
import org.springframework.messaging.MessageChannel;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.util.Assert;
/**
@@ -53,11 +52,11 @@ public class DefaultHeaderChannelRegistry extends IntegrationObjectSupport
private static final int DEFAULT_REAPER_DELAY = 60000;
protected static final AtomicLong id = new AtomicLong();
protected static final AtomicLong id = new AtomicLong(); // NOSONAR
protected final Map<String, MessageChannelWrapper> channels = new ConcurrentHashMap<>();
protected final Map<String, MessageChannelWrapper> channels = new ConcurrentHashMap<>(); // NOSONAR
protected final String uuid = UUID.randomUUID().toString() + ":";
protected final String uuid = UUID.randomUUID().toString() + ":"; // NOSONAR
private boolean removeOnGet;
@@ -108,11 +107,6 @@ public class DefaultHeaderChannelRegistry extends IntegrationObjectSupport
this.removeOnGet = removeOnGet;
}
@Override
public void setTaskScheduler(TaskScheduler taskScheduler) {
super.setTaskScheduler(taskScheduler);
}
@Override
public final int size() {
return this.channels.size();
@@ -157,16 +151,18 @@ public class DefaultHeaderChannelRegistry extends IntegrationObjectSupport
}
@Override
@Nullable
public Object channelToChannelName(@Nullable Object channel) {
return channelToChannelName(channel, this.reaperDelay);
}
@Override
@Nullable
public Object channelToChannelName(@Nullable Object channel, long timeToLive) {
if (!this.running && !this.explicitlyStopped && this.getTaskScheduler() != null) {
start();
}
if (channel != null && channel instanceof MessageChannel) {
if (channel instanceof MessageChannel) {
String name = this.uuid + id.incrementAndGet();
this.channels.put(name, new MessageChannelWrapper((MessageChannel) channel,
System.currentTimeMillis() + timeToLive));
@@ -181,6 +177,7 @@ public class DefaultHeaderChannelRegistry extends IntegrationObjectSupport
}
@Override
@Nullable
public MessageChannel channelNameToChannel(@Nullable String name) {
if (name != null) {
MessageChannelWrapper messageChannelWrapper;

View File

@@ -205,6 +205,20 @@ public abstract class IntegrationObjectSupport implements BeanNameAware, NamedCo
return this.beanFactory;
}
/**
* Configure a {@link TaskScheduler} for those components which logic relies
* on the scheduled tasks.
* If not provided, falls back to the global {@code taskScheduler} bean
* in the application context, provided by the Spring Integration infrastructure.
* @param taskScheduler the {@link TaskScheduler} to use.
* @since 5.1.3
* @see #getTaskScheduler()
*/
public void setTaskScheduler(TaskScheduler taskScheduler) {
Assert.notNull(taskScheduler, "taskScheduler must not be null");
this.taskScheduler = taskScheduler;
}
protected TaskScheduler getTaskScheduler() {
if (this.taskScheduler == null && this.beanFactory != null) {
this.taskScheduler = IntegrationContextUtils.getTaskScheduler(this.beanFactory);
@@ -219,11 +233,6 @@ public abstract class IntegrationObjectSupport implements BeanNameAware, NamedCo
return this.channelResolver;
}
protected void setTaskScheduler(TaskScheduler taskScheduler) {
Assert.notNull(taskScheduler, "taskScheduler must not be null");
this.taskScheduler = taskScheduler;
}
public ConversionService getConversionService() {
if (this.conversionService == null && this.beanFactory != null) {
this.conversionService = IntegrationUtils.getConversionService(this.beanFactory);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,7 +26,6 @@ import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.integration.context.IntegrationObjectSupport;
import org.springframework.integration.context.IntegrationProperties;
import org.springframework.integration.support.SmartLifecycleRoleController;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.util.PatternMatchUtils;
import org.springframework.util.StringUtils;
@@ -56,9 +55,9 @@ public abstract class AbstractEndpoint extends IntegrationObjectSupport
private volatile boolean running;
protected final ReentrantLock lifecycleLock = new ReentrantLock();
protected final ReentrantLock lifecycleLock = new ReentrantLock(); // NOSONAR
protected final Condition lifecycleCondition = this.lifecycleLock.newCondition();
protected final Condition lifecycleCondition = this.lifecycleLock.newCondition(); // NOSONAR
private String role;
@@ -89,11 +88,6 @@ public abstract class AbstractEndpoint extends IntegrationObjectSupport
return this.role;
}
@Override
public void setTaskScheduler(TaskScheduler taskScheduler) {
super.setTaskScheduler(taskScheduler);
}
@Override
protected void onInit() {
super.onInit();
@@ -125,7 +119,7 @@ public abstract class AbstractEndpoint extends IntegrationObjectSupport
}
@Override
public void destroy() throws Exception {
public void destroy() throws Exception { // NOSONAR TODO: remove throws in 5.2
if (this.roleController != null) {
this.roleController.removeLifecycle(this);
}

View File

@@ -48,6 +48,7 @@ import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessageHandlingException;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.core.DestinationResolver;
import org.springframework.messaging.support.ErrorMessage;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.util.Assert;
@@ -142,7 +143,7 @@ public class DelayHandler extends AbstractReplyProducingMessageHandler implement
*/
public DelayHandler(String messageGroupId, TaskScheduler taskScheduler) {
this(messageGroupId);
this.setTaskScheduler(taskScheduler);
setTaskScheduler(taskScheduler);
}
/**
@@ -272,11 +273,9 @@ public class DelayHandler extends AbstractReplyProducingMessageHandler implement
if (this.delayedMessageErrorChannel != null) {
return this.delayedMessageErrorChannel;
}
if (this.delayedMessageErrorChannelName != null) {
if (getChannelResolver() != null) {
this.delayedMessageErrorChannel = getChannelResolver()
.resolveDestination(this.delayedMessageErrorChannelName);
}
DestinationResolver<MessageChannel> channelResolver = getChannelResolver();
if (this.delayedMessageErrorChannelName != null && channelResolver != null) {
this.delayedMessageErrorChannel = channelResolver.resolveDestination(this.delayedMessageErrorChannelName);
}
return this.delayedMessageErrorChannel;
}
@@ -516,11 +515,9 @@ public class DelayHandler extends AbstractReplyProducingMessageHandler implement
}
handleMessageInternal(message);
}
else {
if (logger.isDebugEnabled()) {
logger.debug("No message in the Message Store to release: " + message +
". Likely another instance has already released it.");
}
else if (logger.isDebugEnabled()) {
logger.debug("No message in the Message Store to release: " + message +
". Likely another instance has already released it.");
}
}