Spring integration 用于同步REST调用的Spring集成出站门方式

Spring integration 用于同步REST调用的Spring集成出站门方式,spring-integration,spring-integration-http,Spring Integration,Spring Integration Http,之前,我能够使用Spring集成开发一个小框架,开发人员可以在其中指定URL、HTTP方法和请求体,并调用任何外部RESTAPI 这是我的Spring集成的配置 <int:channel id='reply.channel'> <int:queue capacity='10' /> </int:channel> <int:channel id='request.channel'/> <int:channel id='outbound

之前,我能够使用Spring集成开发一个小框架,开发人员可以在其中指定URL、HTTP方法和请求体,并调用任何外部RESTAPI

这是我的Spring集成的配置

<int:channel id='reply.channel'>
    <int:queue capacity='10' />
</int:channel>
<int:channel id='request.channel'/>

<int:channel id='outbound.Channel'/>

<int:gateway id="outboundGateway"
    service-interface="com.bst.pm.PostGateway"
    default-request-channel="outbound.Channel">
</int:gateway>

<int:object-to-json-transformer input-channel="outbound.Channel" output-channel="request.channel"/>



<int-http:outbound-gateway id="outbound.gateway"
    request-channel="request.channel" url-expression="headers.bstUrl"
    http-method-expression="headers.bstHttpMethod" expected-response-type-expression="headers.bstExpectedResponseType"
    charset="UTF-8" reply-timeout="5000" reply-channel="reply.channel"
    mapped-request-headers="bst*, HTTP_REQUEST_HEADERS">

</int-http:outbound-gateway>

然后,开发人员可以使用上述基础结构调用外部RESTAPI调用,如下所示

@Autowired @Qualifier("reply.channel") PollableChannel receivedChannel;
@Autowired @Qualifier("request.channel") MessageChannel getRequestChannel;
@Autowired @Qualifier("outbound.Channel") MessageChannel httpOutboundGateway;

    Post post = new Post();
    post.setTitle("Spring INtegration Test");
    post.setBody("This is a sample request body to test Spring Integration HTTP Outbound gateway");
    post.setUserId(Long.valueOf(1));

    Message<?> message = MessageBuilder.withPayload(post)
                        .setHeader("bstUrl", "https://jsonplaceholder.typicode.com/posts")
                        .setHeader("bstHttpMethod", "POST")
                        .setHeader("bstExpectedResponseType", "com.bst.pages.crm.web.Post")
                         .build();

    httpOutboundGateway.send(message);
    Message<?> receivedMsg = receivedChannel.receive();

    Post post = (Post) receivedMsg.getPayload();
    System.out.println("############## ServerMsg ##############");
    System.out.println(o);
    System.out.println("############## Done! ##############");
@Autowired@Qualifier(“reply.channel”)PollableChannel-receivedChannel;
@Autowired@Qualifier(“request.channel”)MessageChannel getRequestChannel;
@Autowired@Qualifier(“outbound.Channel”)MessageChannel httpOutboundGateway;
Post Post=新Post();
post.setTitle(“Spring集成测试”);
setBody(“这是一个测试Spring集成HTTP出站网关的示例请求体”);
post.setUserId(Long.valueOf(1));
Message Message=MessageBuilder.withPayload(post)
.setHeader(“bstUrl”https://jsonplaceholder.typicode.com/posts")
.setHeader(“bsthttmpethod”、“POST”)
.setHeader(“bstExpectedResponseType”、“com.bst.pages.crm.web.Post”)
.build();
httpOutboundGateway.send(消息);
Message receivedMsg=receivedChannel.receive();
Post Post=(Post)receivedMsg.getPayload();
系统.输出.打印(“###################################”;
系统输出打印ln(o);
系统。输出。打印(“完成!”;
在这里,我希望通过这个集成基础设施使所有其余调用保持同步。但是,我使用了队列通道作为http:outbound网关的应答通道。因此,根据我的理解,回复可能会被错误的发送者接收,因为任何人都可以共用消息通道

我们如何确保正确的发件人始终收到正确的回复

谢谢,
Keth

你的回答是正确的。如果真的有一个全局回复通道和几个并发进程,那么最终可能会出现窃取工作的情况

要解决您的问题,您需要摆脱HTTP网关上的
回复频道
,只需依靠消息网关填充的
回复频道
头即可。然而,您的网关方法实际上应该作为请求-应答签名:它必须返回对象

请参阅参考手册中有关
replyChannel
标题的更多信息: