Spring integration Spring集成4异步请求/响应

Spring integration Spring集成4异步请求/响应,spring-integration,spring-jms,spring-dsl,Spring Integration,Spring Jms,Spring Dsl,我尝试使用Spring Integration v4的DSL API编写一个简单的消息流,如下所示: -> in.ch -> Processing -> JmsGatewayOut -> JMS_OUT_QUEUE Gateway <- out.ch <- Processing <- JmsGatewayIn <- JMS_IN_QUEUE 我的集成流程配置如下: @MessagingGateway public in

我尝试使用Spring Integration v4的DSL API编写一个简单的消息流,如下所示:

       -> in.ch -> Processing -> JmsGatewayOut -> JMS_OUT_QUEUE
Gateway
       <- out.ch <- Processing <- JmsGatewayIn <- JMS_IN_QUEUE
我的集成流程配置如下:

@MessagingGateway
public interface WsGateway {

    @Gateway(requestChannel = "in.ch", replyChannel = "out.ch", 
        replyTimeout = 45000)
    AResponse process(ARequest request);
}
@Configuration
@EnableIntegration
@IntegrationComponentScan
@ComponentScan
public class IntegrationConfig {

    @Bean(name = "in.ch")
    public DirectChannel inCh() {
        return new DirectChannel();
    }

    @Bean(name = "out.ch")
    public DirectChannel outCh() {
        return new DirectChannel();
    }   

    @Autowired
    private MQQueueConnectionFactory mqConnectionFactory;

    @Bean
    public IntegrationFlow requestFlow() {

        return IntegrationFlows.from("in.ch")
                .handle("processor", "processARequest")
                .handle(Jms.outboundGateway(mqConnectionFactory)
                        .requestDestination("JMS_OUT_QUEUE")
                        .correlationKey("JMSCorrelationID")
                .get();
    }

    @Bean
    public IntegrationFlow responseFlow() {

        return IntegrationFlows.from(Jms.inboundGateway(mqConnectionFactory)
                .destination("JMS_IN_QUEUE"))
                .handle("processor", "processAResponse")
                .channel("out.ch")
                .get();
    }
}
谢谢你在这方面的帮助,
PM.

首先,您的配置不正确:

  • 由于您是从
    WsGateway#process
    启动流程的,因此您确实应该在那里等待回复。 网关的请求/应答功能基于
    临时应答通道
    ,该通道作为不可序列化的值放置在
    标题

  • 只要您等待并依赖该网关,实际上没有理由提供
    replyChannel
    ,如果您不打算对回复执行一些发布-订阅逻辑的话

  • 当您向JMS队列发送消息时,您应该了解使用者部分可能是一个单独的远程应用程序。最后一个可能对您的
    out.ch
    一无所知

  • JMS请求/应答功能实际上是基于
    JMSCorrelationID
    ,但这还不够。这里还有一个
    ReplyTo
    JMS头。因此,如果你打算从消费者那里发送回复,你应该真正依赖
    JmsGatewayIn
    的东西

  • 因此,我将您的代码更改为:

    @MessagingGateway
    public interface WsGateway {
    
        @Gateway(requestChannel = "in.ch", replyTimeout = 45000)
        AResponse process(ARequest request);
    }
    
    @Configuration
    @EnableIntegration
    @IntegrationComponentScan
    @ComponentScan
    public class IntegrationConfig {
    
        @Bean(name = "in.ch")
        public DirectChannel inCh() {
            return new DirectChannel();
        }
    
        @Autowired
        private MQQueueConnectionFactory mqConnectionFactory;
    
        @Bean
        public IntegrationFlow requestFlow() {
            return IntegrationFlows.from("in.ch")
                    .handle("processor", "processARequest")
                    .handle(Jms.outboundGateway(mqConnectionFactory)
                            .requestDestination("JMS_OUT_QUEUE")
                            .replyDestination("JMS_IN_QUEUE"))
                    .handle("processor", "processAResponse")
                    .get();
        }
    
    }
    
    如果适合您,请告诉我,或者尝试解释为什么您在一个
    单向
    情况下使用
    双向
    网关。也许
    Jms.outboundAdapter()
    Jms.inboundAdapter()
    更适合您

    更新

    如何使用Java DSL中的

    .enrichHeaders(e -> e.headerChannelsToString())
    

    您好,您的解决方案是正确的,谢谢。我第一次尝试用Jms-In/Out适配器替换Jms-In/Out网关,但在配置这些适配器时也遇到了问题。然后,我在您的解决方案中只尝试了带有replyDestination的Jms网关,效果很好。我没有删除最初的@Gateway的replyChannel,正如你提到的,它是不需要的。谢谢再次你好,我发现自己需要回到这个帖子。。。我有一个需求,我认为我确实需要使用单独的JMS入站/出站适配器来允许异步响应。为了做到这一点,我再次在WsGateway上设置了replyChannel=“out.ch”,将Jms.outboundGateway切换为Jms.outboundAdapter和Jms.inboundAdapter,并在发送前在消息上设置了JMSCorrelationID头,但是,当响应返回并放入out.ch时,WsGateway不会拾取响应消息?我的SI流实际上比这里显示的要复杂一些……在发送请求之前,尝试将
    一起使用
    标题通道如何在新的DSL表单中设置字符串?是否只是将其设置为具有空值的标题名?是的,您是对的。因此,您必须进行以下变通: