Please reprint from the source: http://eksliang.iteye.com/blog/2242743
In the previous article, ActiveMQ is integrated with Spring. In spring , we use ActiveMQ's own connection factory to create a connectionFactory. In this article, we will introduce using ActiveMQ's own namespace to declare connection factories, destinations, etc. in spring!
To use Active's own namespace factory, you need to import a jar package. The mave address is as follows:
<dependency> <groupId>org.apache.xbean</groupId> <artifactId>xbean-spring</artifactId> <version>3.7</version> </dependency>
At this point the Spring configuration is as follows:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:task="http://www.springframework.org/schema/task" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:amq="http://activemq.apache.org/schema/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.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-5.5.0.xsd"> <!-- Use activeMQ's own Spring configuration namespace to declare the connection address --> <amq:connectionFactory id="connectionFactory" brokerURL="tcp://localhost:61616"/> <!-- Declare queue address--> <amq:queue id="queue" physicalName="queue.destination"></amq:queue> <!-- Declaration subject address --> <amq:topic id="topic" physicalName="topic.destination"></amq:topic> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory"/> <property name="defaultDestination" ref="queue"></property> </bean> <!-- Assembly send message entity--> <bean id="queueSendService" class="com.gosun.jms.QueueSendService"> <property name="jmsTemplate" ref="jmsTemplate"/> </bean> <!-- Assemble the receiving message entity--> <bean id="queuesAcceptService" class="com.gosun.jms.QueuesAcceptService"> <property name="jmsTemplate" ref="jmsTemplate"/> </bean> </beans>
The test code is the same as the previous article
public static void main(String[] args) { ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext-activeMQ1.xml"); // send the message first QueueSendService qss = (QueueSendService) context.getBean("queueSendService"); User user = new User(); user.setUserName("ickes"); user.setUserPwd("123456"); user.setSal(12000F); qss.send(user); // receive message QueuesAcceptService qas = (QueuesAcceptService) context.getBean("queuesAcceptService"); qas.receive(); }
Use the namespace that ActiveMQ only brings to declare, you will find that the configuration will be much less, and the code will be more concise!