INT-3402 ChannelNames lazy-resolution Refactoring

JIRA: https://jira.spring.io/browse/INT-3402

After applying introduction of `AbstractMessageProducingHandler` there was need some simply refactoring to follow with DRY
This commit is contained in:
Artem Bilan
2014-08-12 13:01:07 +03:00
committed by Gary Russell
parent 8b5c1c5053
commit f0ab3b03bd
3 changed files with 40 additions and 52 deletions

View File

@@ -131,7 +131,7 @@ public abstract class AbstractCorrelatingMessageHandler extends AbstractMessageP
this.correlationStrategy = correlationStrategy == null ?
new HeaderAttributeCorrelationStrategy(IntegrationMessageHeaderAccessor.CORRELATION_ID) : correlationStrategy;
this.releaseStrategy = releaseStrategy == null ? new SequenceSizeReleaseStrategy() : releaseStrategy;
this.messagingTemplate.setSendTimeout(DEFAULT_SEND_TIMEOUT);
setSendTimeout(DEFAULT_SEND_TIMEOUT);
sequenceAware = this.releaseStrategy instanceof SequenceSizeReleaseStrategy;
}
@@ -193,15 +193,10 @@ public abstract class AbstractCorrelatingMessageHandler extends AbstractMessageP
@Override
protected void onInit() throws Exception {
super.onInit();
Assert.state(!(this.discardChannelName != null && this.discardChannel != null),
"'discardChannelName' and 'discardChannel' are mutually exclusive.");
BeanFactory beanFactory = this.getBeanFactory();
if (beanFactory != null) {
this.messagingTemplate.setBeanFactory(beanFactory);
Assert.state(!(this.discardChannelName != null && this.discardChannel != null),
"'discardChannelName' and 'discardChannel' are mutually exclusive.");
Assert.state(!(getOutputChannelName() != null && getOutputChannel() != null),
"'outputChannelName' and 'outputChannel' are mutually exclusive.");
if (this.outputProcessor instanceof BeanFactoryAware) {
((BeanFactoryAware) this.outputProcessor).setBeanFactory(beanFactory);
}
@@ -559,8 +554,7 @@ public abstract class AbstractCorrelatingMessageHandler extends AbstractMessageP
if (sendPartialResultOnExpiry) {
if (logger.isDebugEnabled()) {
logger.debug("Prematurely releasing partially complete group with key ["
+ correlationKey + "] to: "
+ (getOutputChannelName() != null ? getOutputChannelName() : getOutputChannel()));
+ correlationKey + "] to: " + getOutputChannel());
}
completeGroup(correlationKey, group);
}
@@ -615,20 +609,6 @@ public abstract class AbstractCorrelatingMessageHandler extends AbstractMessageP
if (message != null) {
replyChannelHeader = message.getHeaders().getReplyChannel();
}
if (getOutputChannelName() != null) {
synchronized (this) {
if (getOutputChannelName() != null) {
try {
setOutputChannel(getBeanFactory().getBean(getOutputChannelName(), MessageChannel.class));
setOutputChannelName(null);
}
catch (BeansException e) {
throw new DestinationResolutionException("Failed to look up MessageChannel with name '"
+ getOutputChannelName() + "' in the BeanFactory.");
}
}
}
}
Object replyChannel = getOutputChannel();
if (replyChannel == null) {

View File

@@ -16,14 +16,18 @@
package org.springframework.integration.handler;
import org.springframework.beans.BeansException;
import org.springframework.integration.core.MessageProducer;
import org.springframework.integration.core.MessagingTemplate;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.core.DestinationResolutionException;
import org.springframework.util.Assert;
/**
* The base {@link AbstractMessageHandler} implementation for the {@link MessageProducer}.
*
* @author David Liu
* @author Artem Bilan
* since 4.1
*/
public abstract class AbstractMessageProducingHandler extends AbstractMessageHandler
@@ -49,15 +53,37 @@ public abstract class AbstractMessageProducingHandler extends AbstractMessageHan
}
public void setOutputChannelName(String outputChannelName) {
Assert.hasText(outputChannelName, "'outputChannelName' must not be empty");
this.outputChannelName = outputChannelName;
}
@Override
protected void onInit() throws Exception {
super.onInit();
Assert.state(!(this.outputChannelName != null && this.outputChannel != null),
"'outputChannelName' and 'outputChannel' are mutually exclusive.");
if (getBeanFactory() != null) {
this.messagingTemplate.setBeanFactory(getBeanFactory());
}
}
public MessageChannel getOutputChannel() {
if (this.outputChannelName != null) {
synchronized (this) {
if (this.outputChannelName != null) {
try {
Assert.state(getBeanFactory() != null, "A bean factory is required to resolve the outputChannel at runtime.");
this.outputChannel = getBeanFactory().getBean(this.outputChannelName, MessageChannel.class);
this.outputChannelName = null;
}
catch (BeansException e) {
throw new DestinationResolutionException("Failed to look up MessageChannel with name '"
+ this.outputChannelName + "' in the BeanFactory.");
}
}
}
}
return outputChannel;
}
public String getOutputChannelName() {
return outputChannelName;
}
}

View File

@@ -21,7 +21,6 @@ import java.util.List;
import org.aopalliance.aop.Advice;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.integration.support.AbstractIntegrationMessageBuilder;
import org.springframework.messaging.Message;
@@ -90,12 +89,8 @@ public abstract class AbstractReplyProducingMessageHandler extends AbstractMessa
@Override
protected final void onInit() {
Assert.state(!(getOutputChannelName() != null && getOutputChannel() != null),
"'outputChannelName' and 'outputChannel' are mutually exclusive.");
if (this.getBeanFactory() != null) {
this.messagingTemplate.setBeanFactory(getBeanFactory());
}
protected final void onInit() throws Exception {
super.onInit();
if (!CollectionUtils.isEmpty(this.adviceChain)) {
ProxyFactory proxyFactory = new ProxyFactory(new AdvisedRequestHandler());
for (Advice advice : this.adviceChain) {
@@ -181,27 +176,14 @@ public abstract class AbstractReplyProducingMessageHandler extends AbstractMessa
* @param replyMessage the reply Message to send
* @param replyChannelHeaderValue the 'replyChannel' header value from the original request
*/
private void sendReplyMessage(Message<?> replyMessage, final Object replyChannelHeaderValue) {
private void sendReplyMessage(Message<?> replyMessage, Object replyChannelHeaderValue) {
if (logger.isDebugEnabled()) {
logger.debug("handler '" + this + "' sending reply Message: " + replyMessage);
}
if (getOutputChannelName() != null) {
synchronized (this) {
if (getOutputChannelName() != null) {
try {
setOutputChannel(this.getBeanFactory().getBean(getOutputChannelName(), MessageChannel.class));
setOutputChannelName(null);
}
catch (BeansException e) {
throw new DestinationResolutionException("Failed to look up MessageChannel with name '"
+ getOutputChannelName() + "' in the BeanFactory.");
}
}
}
}
if (getOutputChannel() != null) {
this.sendMessage(replyMessage, getOutputChannel());
MessageChannel outputChannel = getOutputChannel();
if (outputChannel != null) {
this.sendMessage(replyMessage, outputChannel);
}
else if (replyChannelHeaderValue != null) {
this.sendMessage(replyMessage, replyChannelHeaderValue);