Example of Spring Integration using Service Activator and JMS inbound channel adapter Endpoints

0

In this article, I will show you an example of spring integration’s service-activator endpoint example. Spring Integration is an event-driven messaging architecture.

The best way to know about a software is through examples.

With this example, I am going to start my new series on spring integration . By the end of this article, you will come to know about channels and endpoints ‘Service Activator’ and ‘JMS inbound channel adapter’.

So let’s begin with our first example

Service Activator and JMS inbound channel adapter Endpoints

In this example, we will see how a message sent to a jms destination can trigger a method defined in a bean. This will be an intra-application example as the message flow will be between two components of an application. Before we start with the example, I will first brief you about endpoint and channels.

Endpoints are the components in the application that consume a message from a channel or produce a message and send it to a channel, thus endpoints are always at the end of channels. In other words, a message can leave the channel successfully only by being consumed by an endpoint, and a message can enter the channel only by being produced by an endpoint.

Now the obvious question is what is channel. Channel is like an address box. The sender knows at which address the message should be sent to, likewise, the receiver knows from which address box it needs to receive the message from. Thus a channel connects two endpoints. One of the endpoint is the producer of the message and the other endpoint is the consumer.

In our example, the producer endpoint is the JMS based inbound Channel Adapters jms:inbound-channel-adapter. It keeps polling the configured destination queue. Once a message is available, it will post it to the outbound channel.

Our second endpoint is on the other side of channel ready to consume the message. The second endpoint is Service Activator endpoint. It keeps polling the inbound channel, once a message is received, its job is to invoke the configured bean’s method. The message received is passed in as the parameter value to the method invoked.

 

service-activator example

service-activator example

 

Configure service-activator and inbound-channel-adapter

We will now configure our channel and endpoints in applicationContext.xml.
We need to declare channels and configure endpoints for jms:inbound-channel-adapter and int:service-activator.
Element int:channel is used for declaring channel. W need to assign an id to it so that it can be referred by name while defining the endpoints. There many types of channels, child element int:queue will create the channel as a QueueChannel.
The JMS inbound channel adaptor endpoint is defined by element jms:inbound-channel-adapter. We need to specify the destination-name as this is the queue from which it will poll the messages. The messages polled will be sent to the outbound channel specified using attribute channel. Attribute connection-factory will refer to the connection factory bean for connecting to the JMS messaging server. The child element int:poller will configure the polling details.

The ConnectionFactory is based on broker URL vm://localhost?broker.persistent=false as the components are in the same application and we only need an embedded broker.

Element int:service-activator will poll the messages from the inbound channel and invoke the configured bean’s method for each message. The inbound channel’s name is specified in attribute input-channel, the bean ID is mentioned in attribute ref and the method to be invoked is mentioned in attribute method.

applicationContext.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"
	xmlns:int-jms="http://www.springframework.org/schema/integration/jms"
	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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd">

	<int:channel id="processEmpChannel">
		<int:queue/>
	</int:channel>

	<int-jms:inbound-channel-adapter
		channel="processEmpChannel" connection-factory="connectionFactory"
		destination-name="empQueue">
		<int:poller fixed-delay="500" />
	</int-jms:inbound-channel-adapter>

	<bean id="connectionFactory"
		class="org.springframework.jms.connection.CachingConnectionFactory">
		<property name="targetConnectionFactory">
			<bean class="org.apache.activemq.ActiveMQConnectionFactory">
				<property name="brokerURL" value="vm://localhost?broker.persistent=false" />
			</bean>
		</property>
		<property name="sessionCacheSize" value="10" />
		<property name="cacheProducers" value="false" />
	</bean>
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory" ref="connectionFactory" />
	</bean>

	<bean id="springIntExample"
		class="com.javarticles.spring.integration.jms.SpringIntegrationJmsExample">
		<property name="jmsTemplate" ref="jmsTemplate" />
	</bean>

	<int:service-activator input-channel="processEmpChannel" ref="springIntExample" method="processEmployee">
		<int:poller fixed-delay="500" />
	</int:service-activator>
</beans>

In our example’s main method, we first create ApplicationContext, get SpringIntegrationJmsExample bean. Next, we call method sendEmployee to create an Employee bean and post it to the destination empQueue. Once posted, the JMS inbound channel adapter will poll the message and post it to the outbound channel processEmpChannel.

The service adapter polling the channel processEmpChannel will poll the message posted and invoke method SpringIntegrationJmsExample.processEmployee.

SpringIntegrationJmsExample:

package com.javarticles.spring.integration.jms;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.messaging.PollableChannel;

public class SpringIntegrationJmsExample {
    private JmsTemplate jmsTemplate;
    
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        SpringIntegrationJmsExample springIntExample = (SpringIntegrationJmsExample) context.getBean("springIntExample");
        springIntExample.sendEmployee();
    }
    
    public void sendEmployee() {
        Employee emp = new Employee(1, "Joe", 37);
        System.out.println("Queue employee " + emp + " for processing");
        getJmsTemplate().convertAndSend("empQueue", emp);
    }
    
    public JmsTemplate getJmsTemplate() {
        return jmsTemplate;
    }

    public void setJmsTemplate(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }

    public void processEmployee(Employee emp) {
        System.out.println("Employee: " + emp + " processed");
    }
}

Output:

Queue employee Employee: [Joe, ID: 1, AGE 37] for processing
Employee: Employee: [Joe, ID: 1, AGE 37] processed

Download Source Code

In this article, I introduced you to JMS inbound channel adapter and Service Activator adapter. You can download the source code here: springintegrationjms.zip

Share.

Leave A Reply