简体   繁体   English

向http-inbound-gateway的回复通道发送消息

[英]Sending message to reply-channel of http-inbound-gateway

I have a below setup with spring integration. 我在下面安装了Spring集成。

<int-http:inbound-gateway id="restListener"
                          request-channel="restChannel"
                          reply-channel="restResponseChannel"
                          path="/service"
                          supported-methods="POST"
                          reply-timeout="5000"
</int-http:inbound-gateway>



<int:channel id="restChannel"/>
<int:channel id="restResponseChannel"/>

<int:service-activator input-channel="restChannel"
                       ref="restRequestHandler"
                       method="handleRestRequest"/>

I receive 2 different types of POST requests on gateway and both of them are passed to service-activator for processing. 我在网关上收到2种不同类型的POST请求,它们都传递给service-activator进行处理。 In one type of POST request my service-activator can immediately process it and reply back with a standard response. 在一种类型的POST请求中,我的service-activator可以立即对其进行处理并以标准响应进行回复。 However, other type of request will be routed through different channels and processed by different processors (depending on the content). 但是,其他类型的请求将通过不同的通道进行路由,并由不同的处理器处理(取决于内容)。 I want the output for processing both type of requests to be sent as response of REST call. 我希望将用于处理两种类型请求的输出作为REST调用的response发送。

For first type of request I can simply do this 对于第一种类型的请求,我可以简单地执行此操作

public void handleRestRequest(Message<JsonNode> postMessage) {
     if (type1) {
        // Do processing
        restResponseChannel.send(MessageBuilder
                .withPayload("{\"result\":\"success!\"}")
                .copyHeaders(postMessage.getHeaders())
                .build());
     } else {
        // send to another channel for further processing
     }

}

But for second type of request if I try to send my output to restResponseChannel (without headers) then I get below exception 但是对于第二种类型的请求,如果我尝试send我的输出sendrestResponseChannel (不带标题),那么我将得到以下异常

org.springframework.messaging.core.DestinationResolutionException: no output-channel or replyChannel header available org.springframework.messaging.core.DestinationResolutionException:没有可用的输出通道或replyChannel标头

I do not have access to original headers for second type of request. 我无权访问第二类请求的原始标题。 How can I send the output to reply-channel ? 如何将输出发送到reply-channel (I read somewhere that gateway creates an anonymous channel to connect to the reply-channel and not having those headers is causing the exception, however I don't understand the underlying mechanism completely to debug this myself) (我读过某个地方,网关创建了一个匿名通道来连接到reply-channel而没有这些标头会导致异常,但是我不完全了解自己调试该错误的底层机制)

You understand that absolutely correctly. 您完全正确地理解这一点。

If you don't carry request headers, there is no TemporaryReplyChannel header to correlate reply with response. 如果您不携带请求标头,则没有TemporaryReplyChannel标头可将回复与响应相关联。 See GenericMessagingTemplate.doSendAndReceive() for implementation: 有关实现,请参见GenericMessagingTemplate.doSendAndReceive()

TemporaryReplyChannel tempReplyChannel = new TemporaryReplyChannel(this.throwExceptionOnLateReply);
    requestMessage = MessageBuilder.fromMessage(requestMessage).setReplyChannel(tempReplyChannel)
            .setHeader(this.sendTimeoutHeader, null)
            .setHeader(this.receiveTimeoutHeader, null)
            .setErrorChannel(tempReplyChannel).build();

    try {
        doSend(channel, requestMessage, sendTimeout);
    }
    catch (RuntimeException ex) {
        tempReplyChannel.setSendFailed(true);
        throw ex;
    }

    Message<?> replyMessage = this.doReceive(tempReplyChannel, receiveTimeout);

So, if there is no that header, no possibility to accept reply. 因此,如果没有该标头,则不可能接受回复。 The behavior is the same like in many other messaging replyAddress patterns. 行为与许多其他消息传递的replyAddress模式相同。

That reply-channel="restResponseChannel" is just for convenience when you don't want to consult header for the next channel or when you would like to send a reply not only to the response but to somewhere else - publish-subscribe pattern. 当您不想查询下一个频道的标题时,或者想要不仅对响应而且对其他位置(发布-订阅模式)发送答复时,该reply-channel="restResponseChannel"只是为了方便。

You don't have choice unless carry headers anyway. 除非携带标题,否则您别无选择。 One case is to use Message directly, another is with the @Headers Map<String, Object> service method. 一种情况是直接使用Message ,另一种情况是使用@Headers Map<String, Object>服务方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM