Introduction to Spring Integration Framework

As we are very much aware of the Spring Framework and JMS. In this article, we will get introduced to an enterprise integration framework known as Spring Integration. Spring Integration is an open source enterprise integration framework that enhances the capability of what Spring could have done alone. Built on Spring’s IoC, Spring Integration abstracts message sources and destinations, integrates the messages, routes and manipulates them, while integrating various components of the application environment.

The Message object that is used by Spring Integration to communicate, consists of the payload and header data. Payload contains the actual data, while header contains other metadata like id, timestamp, etc. Below diagrams illustrates the different components involved in a Spring Integration communication –

Spring Integration Components


Note that if we have a Queue Listener configured, DefaultMessageListener needs to be used in instead of the Inbound Gateway. Accordingly, we will be having an Inbound Channel Adaptor (if we need to poll for Message on the queue) or Message Driven Channel Adaptor (if we have a Queue Listener configured) in place.

There are two endpoints in a Message communication, one is the Producer Endpoint and other is the Consumer Endpoint. The endpoints mark the ends of a particular Channel, such that the Producer Endpoint produces the Message and drops it to an inbound channel. On the other end of the channel is the Consumer Endpoint, which is ready to consume the Message on the same channel. The Producer Endpoint is the inbound-channel-adapter, whereas Consumer Endpoint is the service-activator component. Service Activator (Consumer Endpoint) polls for Messages on the channel, and once it finds any, it activates the configured service method. The service method processes the Message accordingly and passes on to the Outbound Channel Adapter, which uses the jmsTemplate to finally drop the Message to the destination queue.

So let’s create a sample Spring Integration application –

With a custom java interface as inbound gateway

  1. Create a new Maven project.
  2. Add the dependencies into the pom.xml file –
  3. Create the Student entity class, Student.java, as –
  4. Now let’s configure the spring integration components in a context file (name it as Test-context.xml) –

    Message channels are something that encapsulate the actual data and decouple message producer from the consumer.

    Gateway
    is basically an entry/exit point for the messaging system. So if you have a messaging server in place, like ActiveMQ or TIBCO, the DefaultMessageListener acts as the inbound gateway, being the entry point to our messaging system.

    Service Activator
    is used to invoke a local service when the message arrives to the configured channel, in such a manner that the service doesn’t know it’s being invoked from a messaging system.

    An Adapter receives messages from an external messaging system (JMS, SFTP, etc.) and “adapts” it to the messaging system (as a Spring Integration Message<T>). The inbound JMS adapter takes in the incoming external messages and “adapts” it to the Spring Integration Message<T> type. And vice-versa: it takes a Spring Integration Message<T> and “adapts” it to the formats required by an external system.

    So, once a message comes in, via an Inbound Adapter, it flows from one component to another via Channels. Ultimately, it might be appropriate to write that message out somewhere. We can write the message using an Outbound Adapter.

  5. Now let’s create the gateway interface we have specified in the context, as –

  6. Create a receiver class, which will finally receive the incoming message from the channel after being routed by the adapter.

  7. This is it. Finally we need a client to make a call to the gateway interface method. Let’s do that using Spring Test Framework as –

  8. Add your application to the server and start the server.
  9. Navigate to the Test.java file we created in point 7 above and run it as JUnit test. Below is what we see logged in the IDE console –
    Message received - Spring Integration Framework

With Spring DefaultMessageListener as inbound gateway

  1. Create a new Maven project.
  2. Make sure the ActiveMQ server is up and running.
  3. The first and foremost thing is to make the entries into the pom.xml file. So it is the same as we have for the previous scenario.
  4. Let’s write the context file (jmsContext.xml) and configure the Spring Integration components as –

  5. Let’s load up the context on server start. To do that, make an appropriate entry into the web.xml file as –

  6. Create the listener class, which will be finally receiving the message, as –

  7. Time to run our application. So this should work as –
    1. Send a custom message to the testQueue
      Send a message to the queue - Spring Integration Framework
    2. The listener gets invoked, and it’s onMessage method gets executed, logging the incoming message to the IDE console as –
      Message logged to console - Spring Integration Framework
    3. The message is passed on the outbound channel and via outbound adapter to the destination sampleQueue, as can be seen below –
      Message arrives to the destination queue - Spring Integration Framework

Useful Links:

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.