Omar Kooheji
Omar Kooheji

Reputation: 55760

Are channels or gateways what I'm looking for?

I'm just getting to grips with Spring Integration and was wondering whether my interpretation of this is correct.

As I understand it, Channels are mono directional and Gateways are bi-directional.

Is it the case that the bidirectionality of gateways is request response? Or does a Gateway allow for full duplex communication?

Basically I've got to write an app where two components have to communicate, but rather than a simple request/response pattern of communication it's more of a request followed by multiple responses, each of which have to be processed in turn and forwarded on to a third system.

Can I do this with a Gateway or should I be setting up a channel for requests and a channel for responses?

If this doesn't make sense I'm happy to elaborate.

Upvotes: 0

Views: 71

Answers (1)

Gary Russell
Gary Russell

Reputation: 174484

Yes, your understanding is correct and yes, gateways are request/response.

If you want async comms between components (e.g. more than one reply for a request) then, yes you need a pair of channels.

You can, however avoid exposing the messaging infrastructure to your components by using a "void" gateway and an inbound channel adapter. Where the gateway method returns void (no response) and the outbound-channel adapter invokes your pojo "responses" method.

EDIT:

This example sends an array into a Spring Integration flow via a gateway and gets the individual elements back as a series of "responses"...

public class Foo {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("foo-context.xml", Foo.class);
        context.getBean(Sender.class).send(new String[] {"foo", "bar", "baz"});
        context.close();
    }

    public interface Sender {

        void send (String[] foo);
    }

    public static class Bar {

        public void receive(String reply) {
            System.out.println(reply);
        }

    }

}

With foo-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:int="http://www.springframework.org/schema/integration"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-4.0.xsd">

    <int:gateway service-interface="foo.Foo$Sender"
        default-request-channel="split" />

    <int:channel id="split" />

    <int:splitter input-channel="split" output-channel="replies" />

    <int:channel id="replies" />

    <int:outbound-channel-adapter channel="replies" method="receive">
        <bean class="foo.Foo$Bar"/>
    </int:outbound-channel-adapter>

</beans>

Upvotes: 1

Related Questions