Spring integration Spring集成4异步请求/响应
我尝试使用Spring Integration v4的DSL API编写一个简单的消息流,如下所示: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
-> 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
,如果您不打算对回复执行一些发布-订阅逻辑的话out.ch
一无所知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表单中设置字符串?是否只是将其设置为具有空值的标题名?是的,您是对的。因此,您必须进行以下变通: