0

在 Spring 集成中,消息通道可以这样配置:

    <int:channel id="get_send_channel" />

    <int:channel id="get_receive_channel">
       <int:queue capacity='10' />
    </int:channel>

    <int-http:outbound-gateway id="get.outbound.gateway"
       request-channel="get_send_channel" 
       url="http://localhost:8080/greeting"
       http-method="GET" reply-channel="get_receive_channel"
       expected-response-type="java.lang.String">
    </int-http:outbound-gateway>

并像这样使用:

@SpringBootApplication
@ImportResource("http-outbound-gateway.xml")
public class HttpApplication {

@Autowired
@Qualifier("get_send_channel")
MessageChannel getSendChannel;

@Autowired
@Qualifier("get_receive_channel")
PollableChannel getReceiveChannel;

public static void main(String[] args) {
    SpringApplication.run(HttpApplication.class, args);
}

@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
    return args -> {
        Message<?> message = MessageBuilder.withPayload("").build();
        getSendChannel.send(message);
        System.out.println(getReceiveChannel.receive().getPayload());
    };
}

如何动态创建和注册 MessageChannel?

上面的代码来自这个例子

我现在已经试过了

return IntegrationFlows.from(MessageChannels.rendezvous("getSend1"))
            .handle(Http.outboundGateway("http://localhost:8080/greeting").httpMethod(HttpMethod.GET))
            .channel(MessageChannels.queue("getReceive1")).get();

使用默认轮询器,但有消息:

preReceive on channel 'getSend1'
postReceive on channel 'getSend1', message is null
Received no Message during the poll, returning 'false'

所以配置似乎不正确,消息不是从 URL 中获取的。

4

1 回答 1

0

它是这样工作的:

@Bean
public IntegrationFlow inbound() {
    return IntegrationFlows
            .from(this.integerMessageSource(), c -> c.poller(Pollers.fixedRate(2000)))
            .handle(Http.outboundGateway("http://localhost:8055/greeting")
                    .httpMethod(HttpMethod.GET).expectedResponseType(String.class))
            .channel(MessageChannels.queue("getReceive"))
            .handle(Http.outboundGateway("http://localhost:8055/greeting").httpMethod(HttpMethod.POST)
                    .expectedResponseType(String.class))
            .channel(MessageChannels.queue("postReceive"))
            .handle(Http.outboundGateway("http://localhost:8055/greeting-final").httpMethod(HttpMethod.POST)
                    .expectedResponseType(String.class))
            .channel(MessageChannels.queue("postReceiveFinal"))
            .get();
}

这确实会获取第一个 URL,然后将答案发布到第二个 URL,然后将答案发布到第三个 URL 并获得最终答案。

于 2018-09-27T07:35:55.320 回答