Fix some ChannelName Late Resolution

Polishing
This commit is contained in:
Artem Bilan
2014-09-17 15:21:16 +03:00
parent 87745d43a1
commit db05476acf
4 changed files with 49 additions and 18 deletions

View File

@@ -23,6 +23,7 @@ import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.integration.core.MessageSelector;
import org.springframework.integration.dsl.support.MessageChannelReference;
import org.springframework.integration.filter.ExpressionEvaluatingSelector;
import org.springframework.integration.router.RecipientListRouter;
import org.springframework.messaging.MessageChannel;
@@ -69,21 +70,45 @@ class DslRecipientListRouter extends RecipientListRouter {
List<Recipient> recipients = new ArrayList<Recipient>(this.selectorRecipientMap.size());
for (Map.Entry<String, MessageSelector> entry : selectorRecipientMap.entrySet()) {
recipients.add(new Recipient(this.resolveChannelName(entry.getKey()), entry.getValue()));
recipients.add(new DslRecipient(new MessageChannelReference(entry.getKey()), entry.getValue()));
}
this.setRecipients(recipients);
super.onInit();
}
private MessageChannel resolveChannelName(String channelName) {
try {
return this.getBeanFactory().getBean(channelName, MessageChannel.class);
class DslRecipient extends Recipient {
private volatile MessageChannel channel;
DslRecipient(MessageChannelReference channel, MessageSelector selector) {
super(channel, selector);
}
catch (BeansException e) {
throw new DestinationResolutionException("Failed to look up MessageChannel with name '"
+ channelName + "' in the BeanFactory.");
@Override
public MessageChannel getChannel() {
if (this.channel == null) {
synchronized (this) {
if (this.channel == null) {
this.channel = resolveChannelName((MessageChannelReference) super.getChannel());
}
}
}
return this.channel;
}
private MessageChannel resolveChannelName(MessageChannelReference channelReference) {
String channelName = channelReference.getName();
try {
return DslRecipientListRouter.this.getBeanFactory().getBean(channelName, MessageChannel.class);
}
catch (BeansException e) {
throw new DestinationResolutionException("Failed to look up MessageChannel with name '"
+ channelName + "' in the BeanFactory.");
}
}
}
}

View File

@@ -79,7 +79,18 @@ class GatewayMessageHandler extends AbstractReplyProducingMessageHandler {
}
@Override
protected void doInit() {
protected Object handleRequestMessage(Message<?> requestMessage) {
if (this.exchanger == null) {
synchronized (this) {
if (this.exchanger == null) {
initialize();
}
}
}
return this.exchanger.exchange(requestMessage);
}
private void initialize() {
BeanFactory beanFactory = getBeanFactory();
if (StringUtils.hasText(this.requestChannel)) {
@@ -108,9 +119,4 @@ class GatewayMessageHandler extends AbstractReplyProducingMessageHandler {
}
}
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
return exchanger.exchange(requestMessage);
}
}