1

我正在尝试使用 Spring 集成来实现路由滑动(EI 模式)。我所做的配置是

 @Bean
    @Transformer(inputChannel = "routingServiceChannel")
    public HeaderEnricher headerEnricher() {
        return new HeaderEnricher(Collections.singletonMap(IntegrationMessageHeaderAccessor.ROUTING_SLIP,
                new RoutingSlipHeaderValueMessageProcessor("routingChannel2",                   
                    "routingChannel1")));
    }

所以当它到达routingServiceChannel时,它会去routingChannel2,然后是routingChannel1。但在我的情况下,它总是会抛出一个异常,即它没有回复通道。当我设置输出通道时,比如

@Transformer(inputChannel = "routingServiceChannel", outputChannel=""xyzChannel)

然后路由发生在 xyzChannel 上,而不是去 routingChannel2 和 routingChannel1。

当我在 spring 集成核心中调试代码时,我偶然发现了这段代码,在

类 AbstractReplyProducingMessageHandler

protected final void handleMessageInternal(Message<?> message) {
        Object result;
        if (this.advisedRequestHandler == null) {
            result = handleRequestMessage(message);
        } else {
            result = doInvokeAdvisedRequestHandler(message);
        }
        if (result != null) {
            sendOutputs(result, message);
        } else if (this.requiresReply) {
            throw new ReplyRequiredException(message, "No reply produced by handler '" + getComponentName()
                    + "', and its 'requiresReply' property is set to true.");
        } else if (logger.isDebugEnabled()) {
            logger.debug("handler '" + this + "' produced no reply for request Message: " + message);
        }
    }

在处理消息方法中,他们获取 routingSlip 映射并将其分配给结果。和

protected void produceOutput(Object reply, Message<?> requestMessage) {
        MessageHeaders requestHeaders = requestMessage.getHeaders();

        Object replyChannel = null;
        if (getOutputChannel() == null) {
            Map<?, ?> routingSlipHeader = requestHeaders.get(IntegrationMessageHeaderAccessor.ROUTING_SLIP, Map.class);
            if (routingSlipHeader != null) {
                Assert.isTrue(routingSlipHeader.size() == 1, "The RoutingSlip header value must be a SingletonMap");
                Object key = routingSlipHeader.keySet().iterator().next();
                Object value = routingSlipHeader.values().iterator().next();
                Assert.isInstanceOf(List.class, key, "The RoutingSlip key must be List");
                Assert.isInstanceOf(Integer.class, value, "The RoutingSlip value must be Integer");
                List<?> routingSlip = (List<?>) key;
                AtomicInteger routingSlipIndex = new AtomicInteger((Integer) value);
                replyChannel = getOutputChannelFromRoutingSlip(reply, requestMessage, routingSlip, routingSlipIndex);
                if (replyChannel != null) {
                    // TODO Migrate to the SF MessageBuilder
                    AbstractIntegrationMessageBuilder<?> builder = null;
                    if (reply instanceof Message) {
                        builder = this.getMessageBuilderFactory().fromMessage((Message<?>) reply);
                    } else if (reply instanceof AbstractIntegrationMessageBuilder) {
                        builder = (AbstractIntegrationMessageBuilder<?>) reply;
                    } else {
                        builder = this.getMessageBuilderFactory().withPayload(reply);
                    }
                    builder.setHeader(IntegrationMessageHeaderAccessor.ROUTING_SLIP,
                            Collections.singletonMap(routingSlip, routingSlipIndex.get()));
                    reply = builder;
                }
            }

            if (replyChannel == null) {
                replyChannel = requestHeaders.getReplyChannel();
            }
        }

        Message<?> replyMessage = createOutputMessage(reply, requestHeaders);
        sendOutput(replyMessage, replyChannel);
    }

他们没有从回复中获取 routingSlip 配置,而是尝试从 requestMessage 中获取。我在这里错过了什么吗?我需要设置任何其他配置吗?

4

1 回答 1

1

感谢您对主题的关注,顺便说一句!:)

一切看起来都不错,但是您错过了路由单的要点。

首先,您应该为消息配置它。而且由于 Routing Slip 是一个header,您应该使用HeaderEnricher它来将其添加到消息的标题中。

路由实际上是在下游引起的,并且完全针对requestMessage,而不是reply。传送单已用完HeaderEnricher

当然,虽然它可能发生在HeaderEnricher'srequestMessage上。

如果您想在 之后查看 Routing Slip HeaderEnricher,您应该配置如下内容:

@BridgeTo
@Bean
public MessageChannel xyzChannel() {
    return new DirectChannel();
}

注意:在逻辑过程中没有一个新的标题可用。HeaderEnricher只在下游。而Routing Slip就是其中之一。

于 2016-03-23T02:39:12.577 回答