如何动态创建 Spring Integration MessageChannels

How to create dynamically Spring Integration MessageChannels

在 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?

以上代码来自this example

我已经试过了

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 中提取的。

它是这样工作的:

@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 的答案并获得最终答案。