activeMq多个监听配置:
集群或者zookeeper分布式集群分发处理只能到一个节点,即active的多个订阅端不会因此产生,除非一个系统配置了对个监听,监听一个发送通道(toptip)
在只有一个监听配置的时候,toptip就相当于queuen
配置多个监听客户端和配置一个一样只是clientId不同即可,当用的是下面的topic时有几个监听端就有会调用几次接收方法,(发送永远只发送一次)
当topic改为org.apache.activemq.command.ActiveMQQueue,(其他不需要动),不管有几个监听端,那个先消费就是谁,即只调用一次接受方法
对于分布式系统,一般采用topic的方式,监听对应需要的发送者,不会出现同一个子系统,对同一个发送者的同一个队列监听两次(否则造成监听多次重复调用)
一个浏览器同一个系统只能登录一个账户,否则session会覆盖掉
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jms="http://www.springframework.org/schema/jms" xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.5.0.xsd">
<bean id="topicSendConnectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
<!-- <property name="brokerURL" value="udp://localhost:8123" /> -->
<!-- UDP传输方式 -->
<property name="brokerURL" value="tcp://10.0.1.126:61616" />
<!-- TCP传输方式 -->
<property name="useAsyncSend" value="true" />
</bean>
<bean id="topicListenConnectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
<!-- <property name="brokerURL" value="udp://localhost:8123" /> -->
<!-- UDP传输方式需要在activemq上面做配置 -->
<property name="brokerURL" value="tcp://10.0.1.126:61616" />
<!-- TCP传输方式 -->
</bean>
<!-- 定义主题 -->
<bean id="myTopic" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="esteelChat-mq" />
</bean>
<bean id="messageConvertForSys" class="com.esteel.chat.mq.MessageConvertForSys" />
<!-- TOPIC send jms模板 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="topicSendConnectionFactory" />
<property name="defaultDestination" ref="myTopic" />
<property name="messageConverter" ref="messageConvertForSys" />
<!-- 发送模式 DeliveryMode.NON_PERSISTENT=1:非持久 ; DeliveryMode.PERSISTENT=2:持久 -->
<property name="deliveryMode" value="1" />
<property name="pubSubDomain" value="true" />
<!-- 开启订阅模式 -->
</bean>
<!-- 消息发送方 -->
<bean id="topicSender" class="com.esteel.chat.mq.MessageSender">
<property name="jmsTemplate" ref="jmsTemplate" />
</bean>
<!-- <bean id="springContextUtil" class="com.esteel.common.SpringContextUtil" /> -->
<!-- 消息接收方 -->
<bean id="topicReceiver" class="com.esteel.chat.mq.MessageReceiver" />
<!-- 主题消息监听容器,一经注册,自动监听 -->
<bean id="listenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="topicListenConnectionFactory" />
<property name="pubSubDomain" value="true" />
<!-- true 订阅模式 -->
<property name="destination" ref="myTopic" />
<!-- 目的地 myTopic -->
<property name="subscriptionDurable" value="true" />
<!-- -这里是设置接收客户端的ID,在持久化时,但这个客户端不在线时,消息就存在数据库里,直到被这个ID的客户端消费掉 -->
<property name="clientId" value="clientId_esteelChat_1" />///配置对个客户端的时候只要这个不同即可
<property name="messageListener" ref="topicReceiver" />
</bean>
<!-- 主题消息监听容器,一经注册,自动监听 -->
<bean id="listenerContainer2"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="topicListenConnectionFactory" />
<property name="pubSubDomain" value="true" />
<!-- true 订阅模式 -->
<property name="destination" ref="myTopic" />
<!-- 目的地 myTopic -->
<property name="subscriptionDurable" value="true" />
<!-- -这里是设置接收客户端的ID,在持久化时,但这个客户端不在线时,消息就存在数据库里,直到被这个ID的客户端消费掉 -->
<property name="clientId" value="clientId_esteelChat_2" />
<property name="messageListener" ref="topicReceiver" />
</bean>
<bean id="listenerContainer3"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="topicListenConnectionFactory" />
<property name="pubSubDomain" value="true" />
<!-- true 订阅模式 -->
<property name="destination" ref="myTopic" />
<!-- 目的地 myTopic -->
<property name="subscriptionDurable" value="true" />
<!-- -这里是设置接收客户端的ID,在持久化时,但这个客户端不在线时,消息就存在数据库里,直到被这个ID的客户端消费掉 -->
<property name="clientId" value="clientId_esteelChat_3" />
<property name="messageListener" ref="topicReceiver" />
</bean>
<!-- Servlet -->
<!-- <bean id="ControlServlet1" class="com.esteel.servlet.ControlServlet1">
<property name="topicSender" ref="topicSender" /> </bean> -->
</beans>