ActiveMQ is a stand-alone JMS messaging service written in 100% Java. Spring, Tomcat and ActiveMQ offer a light weight alternative to a full blown application server. Here are the steps I took to configure this stack on my Thinkpad.
Here are the basic prerequisites:
- Download latest version of Tomcat (5.x or 6.x). Update the tomcat-users.xml file and add the “
manager” rolename, and add this tole to the “tomcat” username. This change is required so that you can view the manager application:http://localhost:8080/manager/html - Download the latest version of ActiveMQ (4.1.1).
- You’ll also need Maven to build ActiveMQ. Read the build instructions. Install the Eclipse plug-in for Maven using the Eclipse’s Software Update mechanism:
http://m2eclipse.codehaus.org/update/ - Import the ActiveMQ projects in Eclipse and fire off a maven build. You can deploy the activemq-web-console servlet in Tomcat and then visit the ActiveMQ sample web monitor:
http://localhost:8080/activemq-web-console-4.1.1/
The web console also starts the ActiveMQ system, you can then send it messages and browse the messages using this web application. - Download latest version of Spring (2.0.3) and install the Spring Eclipse plug-in from
http://springide.org/updatesite/
To write a sender and receiver you’ll need to include the following libraries:
- activemq-core-4.1.1.jar
- backport-util-concurrent-2.1.jar
- commons-logging-1.1.jar
- geronimo-j2ee-management_1.0_spec-1.0.jar
- geronimo-jms_1.1_spec-1.0.jar
- spring.jar
Here are the spring configuration files that you’ll need:
File applicationContext-common.xml:
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- configure connection factory -->
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://localhost:61616</value>
</property>
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref local="connectionFactory"/>
</property>
</bean>
</beans>
File applicationContext-helloWorld.xml:
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<!-- Set the Queue Name -->
<constructor-arg index="0">
<value>FirstQueue</value>
</constructor-arg>
</bean>
</beans>
Sender
import javax.jms.Destination;
import javax.jms.Session;
import javax.jms.Message;
import javax.jms.JMSException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
public class Sender {
public static void main(String[] args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext(
new String[] { "./conf/applicationContext-common.xml",
"./conf/applicationContext-helloWorld.xml"
});
JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");
Destination destination = (Destination)ctx.getBean("destination");
template.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("Hello World");
}
});
}
}
Receiver
import javax.jms.Destination;
import javax.jms.Message;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
public class Receiver {
public static void main(String[] args) {
//C:seedprojects2ndspringconfapplicationContext-common.xml
ApplicationContext ctx = new FileSystemXmlApplicationContext(
new String[] { "/conf/applicationContext-common.xml",
"/conf/applicationContext-helloWorld.xml"
});
JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");
Destination destination = (Destination)ctx.getBean("destination");
System.out.println("Will wait " + template.getReceiveTimeout() + " sec. for message");
Message msg = template.receive(destination);
System.out.println("Recieved Message: " + msg);
}
}
Advertisement
June 24, 2007 at 4:55 am |
In the Spring configuration, it is best to use the Failover Transport. “Clients should be using the Failover Transport to connect to the available brokers. e.g. using a URL something like the following
failover:(tcp://broker1:61616,tcp://broker2:61616,tcp://broker3:61616)
Only the master broker starts up its transport connectors and so the clients can only connect to the master.”