你的位置:首页 > 软件开发 > Java > Spring下ActiveMQ实战

Spring下ActiveMQ实战

发布时间:2015-11-06 00:01:49
MessageQueue是分布式的系统里经常要用到的组件,一般来说,当需要把消息跨网段、跨集群的分发出去,就可以用这个。一些典型的示例就是:    1、集群A中的消息需要发送给多个机器共享;    2、集群A中消息需要主动推送,但彼此的网络不是互通的(如集群A只有过HA才能被外界 ...

Spring下ActiveMQ实战

    MessageQueue是分布式的系统里经常要用到的组件,一般来说,当需要把消息跨网段、跨集群的分发出去,就可以用这个。一些典型的示例就是:

    1、集群A中的消息需要发送给多个机器共享;

    2、集群A中消息需要主动推送,但彼此的网络不是互通的(如集群A只有过HA才能被外界访问);

    Spring下ActiveMQ实战

    当然上面的几个点,除了用MQ还有其它实现方式,但是MQ无疑是非常适合用来做这些事的。众多MQ中,ActiveMQ是比较有名气也很稳定的,它发送消息的成本非常廉价,支持Queue与Topic两种消息机制。本文主要就是讲如何Spring环境下配置此MQ:

 

1、场景假设

    现有机器两台Server、Worker需要进行异步通信,另有一台ActiveMQ机器,关于MQ的配置信息存放在Zookeeper中,Zookeeper的节点有:

      - /mq/activemq/ip:mq的机器ip

      -/mq/activemq/port:这是mq的机器端口

 

2、Server的Spring

    Server主要的工作就是接受Worker消息,并发送消息给Worker。主要是定义了连接MQ的连接池接受Worker消息的队列worker,发送消息给Worker的队列server:

 1 <??> 2 <beans ="http://www.springframework.org/schema/beans" ="http://www.w3.org/2001/ ="http://www.springframework.org/schema/jms" ="http://www.springframework.org/schema/p" xsi:schemaLocation=" 3     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 4     http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.1.xsd"> 5  6   <!-- ActiveMQ连接池 --> 7   <bean id="conFactory" class="org.apache.activemq.pool.PooledConnectionFactory"> 8     <property name="connectionFactory"> 9       <bean class="org.apache.activemq.ActiveMQConnectionFactory">10         <property name="brokerURL">11           <bean class="lekko.mq.util.MQPropertiesFactory" factory-method="getUrl" />12         </property>13         <property name="closeTimeout" value="60000" />14         <!-- <property name="userName" value="admin" /> -->15         <!-- <property name="password" value="admin" /> -->16         <!-- <property name="optimizeAcknowledge" value="true" /> -->17         <property name="optimizedAckScheduledAckInterval" value="10000" />18       </bean>19     </property>20   </bean>21 22 23   <!-- Worker任务消息 -->24   <bean id="taskWorkerTopic" class="org.apache.activemq.command.ActiveMQTopic">25     <constructor-arg value="worker_topic" />26   </bean>27   <!-- 任务监听容器 -->28   <bean id="taskWorkerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">29     <property name="connectionFactory" ref="conFactory" />30     <property name="destination" ref="taskWorkerTopic" />31     <property name="messageListener">32       <bean class="lekko.mq.task.TaskWorkerListener" />33     </property>34     <property name="pubSubDomain" value="true" />35   </bean>36 37 38   <!-- Server任务消息 -->39   <bean id="taskServerTopic" class="org.apache.activemq.command.ActiveMQTopic">40     <constructor-arg value="server_topic" />41   </bean>  42   <!-- 任务消息发送模板 -->43   <bean id="taskServerTemplate" class="org.springframework.jms.core.JmsTemplate" p:connectionFactory-ref="conFactory" p:defaultDestination-ref="taskServerTopic" />44 45 </beans>

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:Spring下ActiveMQ实战

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录