javaspringintegrationspring-integrationintegration-patterns

custom spring integration outbound gateway implementation


How to specify service implementation for outbound gateway in Spring Integration? If outbound gateways are for bidirectional communication and generally used to provide integration with external systems and service activators are for local service calls then how to implement outbound gateway for custom transport/external system? Gateway namespace allows setting service interface for inbound gateway, but what about service implementation for outbound gateway?

<int:gateway id="cafeService"
         service-interface="org.cafeteria.Cafe"
         default-request-channel="requestChannel"
         default-reply-channel="replyChannel"/>

Solution

  • The easiest mechanism is to implement your service interface as a POJO and invoke it from a

    <service-activator input-channel="..." output-channel="..." 
          ref="myPojo" method="process" />
    

    where

    public class MyPoJo {
    
        public Bar process(Foo foo) {
    
            ...
    
        }
    
    }
    

    If you want to do it more formally you can extend AbstractReplyProducingMessageHandler. And wrap it in a consumer endpoint but the POJO route is preferred by most because then you would have no framework dependencies.

    EDIT:

    If you use Spring Tool Suite (based on eclipse), there is a starter project to help you build a full-blown gateway using a template; it includes starter classes for the namespace parsers and everything (New | Spring | Spring Project | Integration ...). The template is hosted here. They are a little out of date (needs an SI update for example, and we don't use docbook any more - on master at least - we use asciidoc), but it should take you a long way. You can also look at the standard parsers for help with the namespace.

    If you think your gateway might have wide appeal, consider contributing it to the extensions.

    EDIT2:

    From your comment...

    as far as I understood the outbound gateways are used to provide integration with external systems and service activators are for local service calls

    That is generally the case, but there is nothing to stop you from invoking an external service via a <service-activator/> - it's a matter of personal preference and there is no real reason to create a formal adapter unless you wish to publish it for use within your organization and/or contribute it to the community in a more formal fashion.