How to create dynamically Spring Integration MessageChannels

1.8k views Asked by At

In the Spring integration the message channel can be configured like this:

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

and used like this:

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

How the MessageChannel can be created and registered dynamically?

Above code is from this example

I have now tried this

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

with default poller, but there is the message:

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

So the configuration seems to be incorrect and the message is not fetch from the URL.

1

There are 1 answers

0
Mike On BEST ANSWER

It is working like this:

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

This does get for the first URL, then posts the answer the second URL and then posts the answer to third URL and gets the final answer.