ActiveMQ与Spring集成(五)
在上篇文章中ActiveMQ与Spring集成在spring中我们使用ActiveMQ自带的连接工厂创建connectionFactory,在这篇文章将介绍在spring中使用ActiveMQ自己的命名空间来声明连接工厂,目的地等等!
使用Active自己的命名空间工厂,需要导入一个jar包,mave地址如下:
<dependency> <groupId>org.apache.xbean</groupId> <artifactId>xbean-spring</artifactId> <version>3.7</version> </dependency>
此时Spring配置如下:
<?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"> <!-- 使用activeMQ自己的Spring配置命名空间来声明 连接地址--> <amq:connectionFactory id="connectionFactory" brokerURL="tcp://localhost:61616"/> <!-- 声明队列地址 --> <amq:queue id="queue" physicalName="queue.destination"></amq:queue> <!-- 声明主题地址 --> <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> <!-- 装配发送消息实体 --> <bean id="queueSendService" class="com.gosun.jms.QueueSendService"> <property name="jmsTemplate" ref="jmsTemplate"/> </bean> <!-- 装配接收消息实体 --> <bean id="queuesAcceptService" class="com.gosun.jms.QueuesAcceptService"> <property name="jmsTemplate" ref="jmsTemplate"/> </bean> </beans>
测试代码跟上篇文章一样
public static void main(String[] args) { ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext-activeMQ1.xml"); //先发送消息 QueueSendService qss = (QueueSendService) context.getBean("queueSendService"); User user = new User(); user.setUserName("ickes"); user.setUserPwd("123456"); user.setSal(12000F); qss.send(user); //接收消息 QueuesAcceptService qas = (QueuesAcceptService) context.getBean("queuesAcceptService"); qas.receive(); }
使用ActiveMQ只带的命名空间进行声明,会发现配置会少很多,代码也更加简洁!