- Spring Framework 3 (3.0.5.RELEASE)
- Apache ActiveMQ 5.5.0
- SLF4J 1.6.2
Prepare ActiveMQ Lib's Logger
To use SLF4J 1.6.2, we copy slf4j-api-1.6.2.jar and jcl-over-slf4j-1.6.2.jar to the directory [activemq_install_dir]/lib/, then delete old slf4j-api-1.5.11.jar and jcl-over-slf4j-1.5.11.jar.
Prepare ActiveMQ
Run [activemq_install_dir]/bin/activemq.bat. To check if ActiveMQ is running, open the URL http://localhost:8161/admin/ in the browser. You can see the welcome page of ActiveMQ.
Quick Start
- In Eclipse, new a Java Project.
- Add Main.java in demo package:
package demo; public class Main { public static void main(String[] args) { Demo.run(); } }
- Add Demo.java in demo package:
package demo; import javax.jms.JMSException; import javax.jms.TextMessage; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jms.core.JmsTemplate; import org.apache.activemq.command.ActiveMQDestination; public class Demo { public static void run() { ApplicationContext context = new ClassPathXmlApplicationContext("demo/beans.xml"); JmsTemplate template = (JmsTemplate) context.getBean("jmsTemplate"); ActiveMQDestination destination = (ActiveMQDestination) context.getBean("destination"); template.convertAndSend(destination, "Hi!"); Object message = template.receive(destination); if (message instanceof TextMessage) { try { System.out.println(((TextMessage) message).getText()); } catch (JMSException e) { System.out.println(e); } } } }
- Add beans.xml in demo package:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <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>
- Add external JARs:
- Spring Framework - all JARs (or asm, beans, context, context.support, core, expression, jms, transaction only)
- ActiveMQ - all JARs in [activemq_install_dir]/lib/ (or activemq-core, geronimo-j2ee-management_1.1_spec-1.0.1, geronimo-jms_1.1_spec-1.1.1, slf4j-api-1.6.2, jcl-over-slf4j-1.6.2 only)
- SLF4J - slf4j-simple-1.6.2 only
- Run: Right click Java Project > Run As > 2 Java Application > Select type: Main - demo.
We should see the greeting!120 [main] INFO org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@14d3343: startup date [Fri Oct 07 16:43:48 CST 2011]; root of context hierarchy 258 [main] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [demo/beans.xml] 879 [main] INFO org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@b61fd1: defining beans [connectionFactory,destination,jmsTemplate]; root of factory hierarchy Hi!
沒有留言:
張貼留言