软件编程
位置:首页>> 软件编程>> java编程>> ActiveMQ结合Spring收发消息的示例代码

ActiveMQ结合Spring收发消息的示例代码

作者:zy_lebron  发布时间:2023-11-24 06:01:12 

标签:ActiveMQ,Spring,收发消息

ActiveMQ 结合 Spring 收发消息

直接使用 ActiveMQ 的方式需要重复写很多代码,且不利于管理,Spring 提供了一种更加简便的方式————Spring JMS ,通过它可以更加方便地使用 ActiveMQ。

Maven 依赖

结合Spring使用ActiveMQ的依赖如下:


<!-- Spring JMS -->
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-jms</artifactId>
 <version>${spring.version}</version>
</dependency>
<!-- xbean 如<amq:connectionFactory /> -->
<dependency>
 <groupId>org.apache.xbean</groupId>
 <artifactId>xbean-spring</artifactId>
 <version>3.16</version>
</dependency>
<!-- ActiiveMQ -->
<dependency>
 <groupId>org.apache.activemq</groupId>
 <artifactId>activemq-core</artifactId>
 <version>5.7.0</version>
</dependency>
<dependency>
 <groupId>org.apache.activemq</groupId>
 <artifactId>activemq-pool</artifactId>
 <version>5.7.0</version>
</dependency>

ActiveMQ.xml 文件


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:amq="http://activemq.apache.org/schema/core"
   xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

http://activemq.apache.org/schema/core

http://activemq.apache.org/schema/core/activemq-core-5.12.1.xsd">

<!-- ActiveMQ 连接工厂 -->
 <amq:connectionFactory id="amqConnectionFactory"
             brokerURL="tcp://localhost:61616"
             userName="admin"
             password="admin" />
 <!-- 提高效率,配置JMS连接工厂 -->
 <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
   <constructor-arg ref="amqConnectionFactory" />
   <property name="sessionCacheSize" value="100" />
 </bean>
 <!-- 定义消息队列(Queue)-->
 <!-- <bean id="QueueDestination" class="org.apache.activemq.command.ActiveMQQueue">
   &lt;!– 设置消息队列的名字 –&gt;
   <constructor-arg value="Queue-zy"/>
 </bean>-->
 <!--定义主题(Topic)-->
 <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
   <constructor-arg value="Topic-zy"/>
 </bean>
 <!-- 配置JMS模板(Queue),Spring提供的JMS工具类,利用它发送、接收消息。 -->
 <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
   <property name="connectionFactory" ref="connectionFactory" />
   <property name="defaultDestination" ref="topicDestination" />
   <property name="receiveTimeout" value="10000" />
   <!-- true是topic,false是queue,默认是false -->
   <property name="pubSubDomain" value="true" />
 </bean>
 <!-- 配置消息队列监听者(Queue or Topic) -->
 <bean id="messageListener" class="com.service.TopicMessageListener" />
 <!-- 显示注入消息监听容器,配置连接工厂,监听的目标是QueueDestination, * 是上面定义的 * -->
 <bean id="ListenerContainer"
    class="org.springframework.jms.listener.DefaultMessageListenerContainer">
   <property name="connectionFactory" ref="connectionFactory" />
   <property name="destination" ref="topicDestination" />
   <property name="messageListener" ref="messageListener" />
 </bean>
</beans>

配置 connectionFactory

connectionFactory 是 Spring 用于创建到 JMS 服务器链接的,Spring 提供了多种 connectionFactory。


<!-- ActiveMQ 连接工厂 -->
<amq:connectionFactory id="amqConnectionFactory"
           brokerURL="tcp://localhost:61616"
           userName="admin"
           password="admin" />
<!-- 提高效率,配置JMS连接工厂 -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
 <constructor-arg ref="amqConnectionFactory" />
 <property name="sessionCacheSize" value="100" />
</bean>

配置Queue


<bean id="QueueDestination" class="org.apache.activemq.command.ActiveMQQueue">
   <!-- 设置消息队列的名字 -->
   <constructor-arg value="Queue-zy"/>
</bean>

配置Topic


<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
   <constructor-arg value="Topic-zy"/>
</bean>

配置JMS消息模板——jmsTemplate


<!-- 配置JMS模板,Spring提供的JMS工具类,利用它发送、接收消息-->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
 <property name="connectionFactory" ref="connectionFactory" />
 <property name="defaultDestination" ref="QueueDestination" />
 <!--<property name="defaultDestination" ref="topicDestination" />-->
 <property name="receiveTimeout" value="10000" />
 <property name="pubSubDomain" value="false" /><!-- true是topic,false是queue,默认是false -->
</bean>

最后,在 applicationContext.xml 中引入配置好的 ActiveMQ.xml


<import resource="ActiveMQ.xml" />

来源:http://www.importnew.com/30159.html

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com