路由滑动弹簧集成(4.2)

时间:2016-03-23 01:52:23

标签: spring spring-integration spring-messaging

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

 @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而不是routeChannel2和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);
    }

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

1 个答案:

答案 0 :(得分:0)

感谢您对BTW的关注! :)

一切看起来都不错,但你错过了路由滑动的重点。

首先,您应该为邮件配置它。由于路由单是header,您应该使用HeaderEnricher将其添加到邮件的标题中。

路由确实是由requestMessage的下游和reply引起的,而不是HeaderEnricher。路由单不在HeaderEnricher之内。

虽然requestMessage HeaderEnricher可能会发生这种情况。

如果您想在@BridgeTo @Bean public MessageChannel xyzChannel() { return new DirectChannel(); } 之后完全参考路由单,请配置以下内容:

HeaderEnricher

注意:在RACCommand逻辑期间,没有一个标头可用。只在下游。而路由滑动就是其中之一。