JMS with spring and activeMq

23 05 2008

JMS is one of most widely used API when async Communication is required. Example could be a simple publish subscribe systems to complex transports in B2B setups.

I have previously worked with JMS using weblogic 9.2.1 and also with IBM MQSeries. Just trying out JMS with activeMq. The motivation to use activeMq is that it is opensource and backed by apache. Spring provides JMS template makes our job easy (Be sure to read about the JMS template and its gotchas). Below are the steps and links to some resources I used. Hope this helps in getting a quick start with activeMq.

1. Download and exract the latest release of activeMq from http://activemq.apache.org/

2. Change directory to bin and run activemq.bat (Detailed instructions in http://activemq.apache.org/getting-started.html). We have just started a broker process. This process by default runs on port 61616.

3. Open another cmd prompt and run netstat -an|find “61616” to check if activeMq is running

4. We will use Spring JMS template to Connect to activeMq.

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd”&gt;

<bean id=”connectionFactory”
class=”org.apache.activemq.ActiveMQConnectionFactory”>
<property name=”brokerURL”>
<value>tcp://localhost:61616</value>
</property>
</bean>

<bean id=”destination” class=”org.apache.activemq.command.ActiveMQQueue”>
<constructor-arg value=”jmsExample” />
</bean>

<bean id=”jmsTemplate”
class=”org.springframework.jms.core.JmsTemplate”>
<property name=”connectionFactory” ref=”connectionFactory” />

</bean>

</beans>

5. Use spring Context to get the JMS template

import javax.jms.JMSException;
import javax.jms.TextMessage;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.jms.core.JmsTemplate;
import org.apache.activemq.command.ActiveMQDestination;

public class SpringInjectRef {

public static void main(String[] args) throws JMSException {

BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
“beans.xml”));

JmsTemplate template = (JmsTemplate) factory.getBean(“jmsTemplate”);
ActiveMQDestination destination = (ActiveMQDestination) factory.getBean(“destination”);

//sending a message
template.convertAndSend(destination, “Hi”);

//recieving a message
Object msg = template.receive(destination);
if (msg instanceof TextMessage) {
try {
System.out.println(((TextMessage) msg).getText());
} catch (JMSException e) {
System.out.println(e);
}
}

}
}

Note: if you do not want to start a seperate broker then change the Config file as shown below.

<bean id=”connectionFactory”
class=”org.apache.activemq.ActiveMQConnectionFactory”>
<property name=”brokerURL”>
<!–<value>tcp://localhost:61616</value>–>
<value>vm://localhost</value>
</property>
</bean>

This change will create a local Datastore and use it to store the messages. This is will help in cases where you need to start the broker as part of your application.

More about vm transport – http://activemq.apache.org/vm-transport-reference.html

I will add more to this post when time permits.

Advertisements

Actions

Information

7 responses

23 05 2008
Anonymous
26 08 2008
Rupinder

A good short tutorial. It gave me opportunity to understand tcp vs vm transports better.

25 07 2010
Binod Suman

Very simple and useful tutorial. Any one can understand in very short time.

Thanks,

Binod Suman
http://ayushsuman.blogspot.com

25 07 2010
harikrishnan83

Thanks for your comments. šŸ™‚

21 05 2011
swa27

when deployed following error appears….

Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘jmsTemplate’ defined in file [C:\Users\swarnajith\jbdsworkspace\SpringJMSexample\beans.xml]: Cannot resolve reference to bean ‘connectionFactory’ while setting bean property ‘connectionFactory’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘connectionFactory’ defined in file [C:\Users\swarnajith\jbdsworkspace\SpringJMSexample\beans.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.apache.activemq.ActiveMQConnectionFactory]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1305)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1067)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
at SpringInjectRef.main(SpringInjectRef.java:15)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘connectionFactory’ defined in file [C:\Users\swarnajith\jbdsworkspace\SpringJMSexample\beans.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.apache.activemq.ActiveMQConnectionFactory]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:946)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:890)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:479)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
… 10 more
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.apache.activemq.ActiveMQConnectionFactory]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:141)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:72)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:938)
… 18 more
Caused by: java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at org.apache.activemq.ActiveMQPrefetchPolicy.(ActiveMQPrefetchPolicy.java:38)
at org.apache.activemq.ActiveMQConnectionFactory.(ActiveMQConnectionFactory.java:88)
at org.apache.activemq.ActiveMQConnectionFactory.(ActiveMQConnectionFactory.java:131)
at org.apache.activemq.ActiveMQConnectionFactory.(ActiveMQConnectionFactory.java:127)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:126)
… 20 more
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
… 29 more

24 05 2011
harikrishnan83

Looks like you are missing sl4j jar in your classpath.

3 12 2012
Anupam

Thanks a lot for the short tuto… Just wondering if you can share some examples of JMS / SPring with Webspere..

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




%d bloggers like this: