Code, JavaEE

Active MQ – Producer / Consumer Spring

References:

Bruce Snyder's Blog
Benjamin Darfler's Blog

package principles;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
* MessageProducer
*
*/
public class MessageProducer {

private static final Logger LOG = LoggerFactory.getLogger(MessageProducer.class);
private JmsTemplate producerTemplate;
private ApplicationContext applicationContext;

public MessageProducer() {
applicationContext = new ClassPathXmlApplicationContext("/principles/activemqContext.xml");
producerTemplate = (JmsTemplate)applicationContext.getBean("producerTemplate");

}

public void sendMessage(final String json) throws JMSException {
producerTemplate.send(new MessageCreator() {

@Override
public Message createMessage(Session session) throws JMSException {
TextMessage message = session.createTextMessage(json);
return message;
}
});
}

public void setProducerTemplate(JmsTemplate producerTemplate) {
this.producerTemplate = producerTemplate;
}
}

 

package principles;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
* MessageConsumer
*
*/
public class MessageConsumer implements MessageListener {

private static final Logger LOG = LoggerFactory.getLogger(MessageConsumer.class);

@Override
public void onMessage(Message message) {
TextMessage messageTxt = (TextMessage)message;

try {
LOG.info("Consumed message - {}", messageTxt.getText());
} catch (JMSException e) {
LOG.error(e.getMessage(), e);
}
}
}

package principles;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.jms.JMSException;

import static org.junit.Assert.assertNotNull;

/**
* MessageTest
*/
public class MessageTest {

private static final Logger LOG = LoggerFactory.getLogger(MessageTest.class);
private ApplicationContext applicationContext;

@Test
public void testApplicationContext() {
assertNotNull("Application context was null.", applicationContext);
LOG.info("Application context was successfully loaded - {}", applicationContext);
}

@Test
public void testSendMessage() {
MessageProducer adaptMessageProducer = new MessageProducer();
String message = "\"This is a message.\"";
try {
adaptMessageProducer.sendMessage(message);
} catch (JMSException e) {
System.out.format("JMSException -> %s", e.toString());
}
}

@Before
public void setUp() {
applicationContext = new ClassPathXmlApplicationContext("classpath:/principles/activemqContext.xml");
}

@After
public void tearDown() {
applicationContext = null;
}
}

 

 

</pre>
<?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:context="http://www.springframework.org/schema/context"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:jms="http://www.springframework.org/schema/jms"
 xmlns:amq="http://activemq.apache.org/schema/core"
 xmlns:jee="http://www.springframework.org/schema/jee"
 xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.2.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">

<!-- enable annotations -->
 <context:annotation-config />

<context:component-scan base-package="com.davidkingenterprise" />

<bean id="applicationContextProvider"
 class="com.davidkingenterprise.config.ApplicationContextProvider" />

<!-- active mq connection factory -->
 <bean id="amqConnectionFactory"
 class="org.apache.activemq.ActiveMQConnectionFactory"
 p:brokerURL="tcp://localhost:61616"
 p:userName="admin"
 p:password="admin" />

<bean id="cachedConnectionFactory"
 class="org.springframework.jms.connection.CachingConnectionFactory"
 p:targetConnectionFactory-ref="amqConnectionFactory"
 p:sessionCacheSize="10" />

<!-- queue destination -->
 <amq:queue id="principlesRequestQueue" physicalName="Principles.Request"/>
 <amq:queue id="principlesResponseQueue" physicalName="Principles.Response"/>

<!-- jms template instance -->
 <bean id="producerTemplate"
 class="org.springframework.jms.core.JmsTemplate"
 p:connectionFactory-ref="cachedConnectionFactory"
 p:defaultDestination-ref="principlesRequestQueue" />

<!-- jms queue listener, response to requests -->
 <bean id="principlesListener" class="com.davidkingenterprise.messaging.TaskMessageListener" />

<!-- jms listener container -->
 <jms:listener-container
 container-type="default"
 connection-factory="cachedConnectionFactory"
 acknowledge="auto"
 concurrency="10-50">
 <jms:listener destination="Principles.Request"
 response-destination="Principles.Response"
 ref="principlesListener" />
 </jms:listener-container>
</beans>
<pre>



<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.2</version>
</dependency>

<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.2</version>
</dependency>

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.12</version>
</dependency>

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.12</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>

<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
<version>3.14</version>
</dependency>

<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.8.0</version>
</dependency>

<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.8.0</version>
</dependency>

<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.5</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.4.0</version>
</dependency>
</dependencies>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s