如何动态创建Spring Integration MessageChannels

时间:2018-09-25 12:11:34

标签: spring spring-integration spring-integration-dsl message-channel

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

PATH

并以此方式使用:

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

如何动态创建和注册MessageChannel?

以上代码来自this example

我现在已经尝试过了

@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());
    };
}

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

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

因此配置似乎不正确,并且无法从URL提取消息。

1 个答案:

答案 0 :(得分: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,并得到最终答案。