Spring Integration:用网关回复消息

问题描述 投票:0回答:1

我有这个 Spring Integration 代码,它接收 SOAP 消息然后回复它。

这是配置:

  @Bean
  public MessageChannel wsGatewayInboundChannel() {
    return MessageChannels.direct(GATEWAY_INBOUND_CHANNEL_NAME).get();
  }

  @Bean
  public MessageChannel wsGatewayOutboundChannel() {
    return MessageChannels.direct(GATEWAY_OUTBOUND_CHANNEL_NAME).get();
  }

  @Bean
  public IntegrationFlow soapMessageFlow(SimpleWebServiceInboundGateway webServiceInboundGateway) {
    return IntegrationFlows.from(webServiceInboundGateway)
            .log(LoggingHandler.Level.INFO)
            .channel(SOAP_MESSAGE_SERVICE_CHANNEL_NAME).get();
  }

  @Bean
  public SimpleWebServiceInboundGateway webServiceInboundGateway() {
    SimpleWebServiceInboundGateway simpleWebServiceInboundGateway = new SimpleWebServiceInboundGateway();
    simpleWebServiceInboundGateway.setRequestChannel(wsGatewayInboundChannel());
    simpleWebServiceInboundGateway.setReplyChannel(wsGatewayOutboundChannel());
    simpleWebServiceInboundGateway.setExtractPayload(false);
    simpleWebServiceInboundGateway.setLoggingEnabled(true);
    return simpleWebServiceInboundGateway;
  }

这是处理消息的服务:

@Service
public class SoapMessageService {

  @ServiceActivator(inputChannel = SOAP_MESSAGE_SERVICE_CHANNEL_NAME, outputChannel = GATEWAY_OUTBOUND_CHANNEL_NAME)
  public SoapMessage receive(SoapMessage request) {
    //...
    return reply;
  }
}

这很好用。

我不知道回复频道在幕后是如何运作的,但我真的想确保回复不会混淆。例如,如果我收到:

Request_1 from A, which reply is Reply_1
Request_2 from B, which reply is Reply_2

我希望将 Reply_1 传递给 A,将 Reply_2 传递给 B,而不是将 Reply_1 传递给 B 而将 Reply_2 传递给 A。

回复通道能否保证之前描述的行为?

提前致谢。

spring-integration spring-integration-dsl spring-integration-http spring-integration-ws
1个回答
0
投票

他们确实有保证。 Spring Integration 中的网关实现完全基于

Return Address
EI 模式:https://www.enterpriseintegrationpatterns.com/patterns/messaging/ReturnAddress.html.

每个网关请求都设置有一个

replyChannel
头到
TemporaryReplyChannel
。当下游没有配置
outputChannel
时,框架会查询该标头以传递当前消息。

在大多数情况下,我们真的不需要在入站网关上配置

setReplyChannel
。只是因为我们可以简单地依赖
replyChannel
header.

在文档中查看更多信息:https://docs.spring.io/spring-integration/docs/current/reference/html/messaging-endpoints.html#gateway-default-reply-channel

还有这个 GH 问题:https://github.com/spring-projects/spring-integration/issues/3985

看看你在任何地方摆脱那个

GATEWAY_OUTBOUND_CHANNEL_NAME
是否有意义。

© www.soinside.com 2019 - 2024. All rights reserved.