What is the difference between Gateway and Service Activator?
Asked Answered
O

3

24

What is the difference between Gateway and Service Activator as Message Endpoints (in terms of Enterprise Integration Patterns)?

Oriana answered 29/4, 2012 at 13:49 Comment(0)
B
13

http://eaipatterns.com/

Typically, a service activator is used to invoke a local service, in such a manner that the service doesn't know it's being invoked from a messaging system.

A gateway s typically an entry or exit point for the messaging system.

Brenneman answered 29/4, 2012 at 17:42 Comment(9)
i had already checked the EAI site but, read it several times, and was not clear on the two concepts. also read about them in a book, but it was still not clear enough (in my head). they are both endpoints (one wraps a spring bean, as you mentioned, decoupling the Service from the messaging system), while the other exposes a service interface. what i found out so far is that one can generate a reply (service activator) while the other can manage a reply (gateway), as in wait for it or time it out. is that correct? is that the main difference or is it more to that?Oriana
Let's talk in terms of Spring Integration's implementation of the patterns. A <service-activator/> receives a message and invokes a method, usually with a method parameter that is the payload of the message; if the method returns void there is nothing more to do when the method returns. If it returns some object, it becomes the new message payload and the message is sent to the output-channel. In Spring Integration, there are two types of gateway. The messaging gateway is an implementation that allows a java application to invoke a messaging flow; the parameter usually becomes the payload.Brenneman
You just define an interface and Spring Integration builds the implementation to deal with the parameter as a message. The second type of gateway are for interfacing to other systems - there are inbound and outbound gateways. Inbound gateways are used to expose a messaging system using some technology (JMS, HTTP etc). An outbound gateway is used to send and receive from a system that exposes some service over some technology (JMS, HTTP etc). There are lots of these types of gateway (twitter, TCP,FTP... and many more)Brenneman
ok, so one is pretty much the reverse of the other in terms of design (one wraps a Service while the other builds it - for Messaging Gateway, at least)Oriana
Yes; in addition, there is an inbound-channel adapter, which polls a mehod for new messages; and an outbound-channel-adapter that invokes a method with a void return (similar to a service-activator with a void, but an outbound-adapter has to return void.Brenneman
@GaryRussell Thx for nice explanation. I had problems with understanding of gateways too.Pouter
@GaryRussell So one of the biggest distinctions between the two is internal (service activator) vs external (gateway)?Acutance
Sort of; however, aside from interacting with external systems, there's an additional messaging gateway (<int:gateway/> when using xml or @MessagingGateway for Java configuration) where the gateway is an entry point from a plain old java object into the messaging flow, where the calling code doesn't necessarily "know" it is interacting with messaging.Brenneman
For more clarity, in terms of spring-integration, we have int-http:inbound-gateway as well as service-activator and as far as I understood both are the type of endpoints only. So, design wise why do they need to co-exist.Joslyn
B
8

The service activator calls a method on an object where the application developer provides the implementation. Spring Integration takes care of calling the method with messages from the input channel and shunting the results off to some output channel. The application-provided code can do some arbitrary work.

For the gateway the application developer provides only an interface, its implementation is provided by Spring.

An appendix to the Spring Integration documentation includes a Cafe example where Barista is a service called through a service activator, and Cafe is a gateway.

The application's main method looks up a Cafe object from the Spring application context and calls placeOrder, on it, passing an Order in as an argument:

public static void main(String[] args) {
      AbstractApplicationContext context = null;
      if (args.length > 0) {
          context = new FileSystemXmlApplicationContext(args);
      }
      else {
          context = new ClassPathXmlApplicationContext(
          "cafeDemo.xml", CafeDemo.class);
      }
      Cafe cafe = (Cafe) context.getBean("cafe");
      for (int i = 1; i <= 100; i++) {
          Order order = new Order(i);
          order.addItem(DrinkType.LATTE, 2, false);
          order.addItem(DrinkType.MOCHA, 3, true);
          cafe.placeOrder(order);
      }
  }

The Cafe is an interface that the application does not provide an implementation for. Spring generates an implementation that sends the Orders passed into it down the input channel called "orders".

Further down the pipeline, there are two service-activators that have a reference to the Barista. The Barista is a POJO that has code for creating a Drink like this:

  public Drink prepareHotDrink(OrderItem orderItem) {
      try {
          Thread.sleep(this.hotDrinkDelay);
          System.out.println(Thread.currentThread().getName()
                  + " prepared hot drink #" + hotDrinkCounter.incrementAndGet()
                  + " for order #" + orderItem.getOrder().getNumber() 
                  + ": " + orderItem);
          return new Drink(orderItem.getOrder().getNumber(), 
                  orderItem.getDrinkType(),
                  orderItem.isIced(), orderItem.getShots());
      }
      catch (InterruptedException e) {
          Thread.currentThread().interrupt();
          return null;
      }
  }

The Barista receives drink orders from the service-activator's input channel and has a method called on it that returns a Drink, which gets sent down the service-activator's output channel, "preparedDrinks".

Barghest answered 25/3, 2014 at 21:24 Comment(0)
L
1

For me the gateway is used for making an abstraction and provide a normalised API for one or more back-end services. E.g You have 5 providers which are using different ways to interface with you (SOAP, REST, XML/http, whatever), but your client want only one way to get the data (let says json/REST). The gateway will convert the json request form your client and convert them to the right backend with its own protocol, after it will convert the backend response to json to give the response to your client.

The service activator acts more as a trigger on an incoming message. Let say your activator poll a database for incoming message and then when the condition meet the "activation" it calls the underlying service.

Info for gateway here.

Info for Activator here.

Leyla answered 11/7, 2013 at 7:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.