Jun 22, 2016

ActiveMQ with Spring Integration inbound-gateway

In this example we want to read one message froom one queue (request-queue) put some header on it and send uppercase response to reply queue (response-queue). Following is Integration diagram.

Create two queues from ActiveMQ web console.
Following is configuration file

<?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/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd">
<!-- Loan Request comes from MQ message (temporary direct message) -->
<int:channel id="xmerChannel" />
<int:channel id="svcActChannel" />
<int-jms:inbound-gateway request-channel="xmerChannel" connection-factory="connectionFactory" request-destination="requestQueue" default-reply-destination="responseQueue" acknowledge="transacted"/>
<int:header-enricher input-channel="xmerChannel"
output-channel="svcActChannel">
<int:header name="lang" value="en" />
</int:header-enricher>
<int:service-activator id="printSvcActivator" ref="msgPrintBean"
method="printOutput" input-channel="svcActChannel"/>
<bean id="msgPrintBean" class="com.techrefresh.spring.integration.bean.UtilBean" />
<bean id="requestQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="request-queue" />
</bean>
<bean id="responseQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="response-queue" />
</bean>
<bean id="connectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616" />
</bean>
</property>
<property name="sessionCacheSize" value="10" />
</bean>
<int:poller fixed-rate="1000" id="pollermain"></int:poller>
</beans>


Folloiwng is util bean.

package com.techrefresh.spring.integration.bean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UtilBean {
private static final Logger logger = LoggerFactory.getLogger(UtilBean.class);
public String printOutput(String msg){
logger.info("Simple message {}",msg);
return msg.toUpperCase();
}
}
view raw UtilBean.java hosted with ❤ by GitHub

Now send message from activemq web console.


Following is what you receive on response channel.