activemq学习笔记——spring整合(Point-To-Point点对点类型)
来源:互联网 发布:免费装修软件app 编辑:程序博客网 时间:2024/06/14 17:28
所需jar包:
①配置ConnectionFactory
connectionFactory是Spring用于创建到JMS服务器链接的
xml配置:
<!-- 连接工厂 --> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616" /> <property name="userName" value="admin"></property> <property name="password" value="admin"></property> </bean>②消息发送的目的地
点对点方式仅有一个订阅者会收到消息,消息一旦被处理就不会存在队列中
<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg value="myQueue"></constructor-arg> </bean>③消息模板
<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate"> <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 --> <constructor-arg ref="connectionFactory" /> </bean>
④生产者
<bean id="producer" class="com.activemq.mqq.ProducerServiceImpl"> <property name="jmsTemplate" ref="jmsQueueTemplate"></property> <property name="destination" ref="destination"></property> </bean>
package com.activemq.mqq;public interface ProducerService {public void send();}
package com.activemq.mqq;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.MapMessage;import javax.jms.Message;import javax.jms.Session;import org.springframework.jms.core.JmsTemplate;import org.springframework.jms.core.MessageCreator;/**生产者*/public class ProducerServiceImpl implements ProducerService{JmsTemplate jmsTemplate;Destination destination;@Overridepublic void send() {// TODO Auto-generated method stubMessageCreator mc = new MessageCreator() {@Overridepublic Message createMessage(Session session) throws JMSException {// TODO Auto-generated method stubMapMessage mm = session.createMapMessage();mm.setString("name", "张龙赵虎");return mm;}};jmsTemplate.send(destination, mc);}public JmsTemplate getJmsTemplate() {return jmsTemplate;}public void setJmsTemplate(JmsTemplate jmsTemplate) {this.jmsTemplate = jmsTemplate;}public Destination getDestination() {return destination;}public void setDestination(Destination destination) {this.destination = destination;}}⑤消息监听
在Spring整合JMS的应用中我们在定义消息监听器的时候一共可以定义三种类型的消息监听器,分别是MessageListener、SessionAwareMessageListener和MessageListenerAdapter。这里我们使用MessageListenerAdapter
xml配置:
<bean id="consumerListenerService" class="com.activemq.mqq.ConsumerListenerServiceImpl"> </bean>
package com.activemq.mqq;import java.util.HashMap;public interface ConsumerListenerService {public void receive(HashMap<String,Object> map);}
package com.activemq.mqq;import java.util.HashMap;public class ConsumerListenerServiceImpl implements ConsumerListenerService{@Overridepublic void receive(HashMap<String,Object> map) {// TODO Auto-generated method stubSystem.out.println(map.get("name"));}}
MessageListenerAdapter
<bean id="queueListener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter"><constructor-arg ref="consumerListenerService"></constructor-arg><property name="defaultListenerMethod" value="receive"></property></bean>
⑥Container消息监听容器
<bean id="queueListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory"></property><property name="destination" ref="destination"></property><property name="messageListener" ref="queueListener"></property></bean>全部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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jms="http://www.springframework.org/schema/jms" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd" > <!-- 连接工厂 --> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616" /> <property name="userName" value="admin"></property> <property name="password" value="admin"></property> </bean> <!-- 配置消息发送目的地方式 --> <!-- Queue队列:仅有一个订阅者会收到消息,消息一旦被处理就不会存在队列中 --> <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg value="myQueue"></constructor-arg> </bean> <!-- Spring JmsTemplate 的消息生产者 start--> <!-- 定义JmsTemplate的Queue类型 --> <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate"> <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 --> <constructor-arg ref="connectionFactory" /> <!-- 非pub/sub模型(发布/订阅),即队列模式 --> <property name="pubSubDomain" value="false" /> </bean> <bean id="producer" class="com.activemq.mqq.ProducerServiceImpl"> <property name="jmsTemplate" ref="jmsQueueTemplate"></property> <property name="destination" ref="destination"></property> </bean> <bean id="consumerListenerService" class="com.activemq.mqq.ConsumerListenerServiceImpl"> </bean> <bean id="queueListener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter"><constructor-arg ref="consumerListenerService"></constructor-arg><property name="defaultListenerMethod" value="receive"></property></bean><bean id="queueListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory"></property><property name="destination" ref="destination"></property><property name="messageListener" ref="queueListener"></property></bean></beans>
web.xml配置:
<context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:queue.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
将应用部署到tomcat上,运行测试代码:
ApplicationContext ac = new ClassPathXmlApplicationContext("queue.xml");ProducerService ps = ac.getBean(ProducerService.class);ps.send();
结果tomcat控制台打印出结果:
代码下载地址:http://download.csdn.net/detail/sunqingzhong44/9598001
发布订阅模式的配置与其类似,只不过消息监听为多个就不在一一介绍了
0 0
- activemq学习笔记——spring整合(Point-To-Point点对点类型)
- ActiveMQ: Point-to-Point (点对点)消息模式开发流程
- JMS(点对点point-to-point)
- ActiveMQ实战(二)--ActiveMQ的通信方式之P2P点对点通信(point-to-point)
- 点对点隧道协议PPTP(Point-to-Point Tunneling Protocol)
- Spring整合activeMq(一):点对点模式
- RFC1661_The Point-to-Point Protocol _点对点协议(PPP)
- activeMQ 例子(一) 简单的p2p(point to point)模式
- 使用Jmeter针对ActiveMQ JMS POINT TO POINT压力测试
- 使用Jmeter针对ActiveMQ JMS POINT TO POINT压力测试
- ActiveMQ学习总结(3)——spring整合ActiveMQ
- Point to Point Communication
- JMeter学习(二十九)使用Jmeter创建ActiveMQ JMS POINT TO POINT请求,环境搭建、请求创建、插件安装、监听服务器资源等
- 《Pro Spring》学习笔记之Spring+ActiveMQ实现Queue通信(点对点)
- Spring学习笔记 - Spring 整合 JMS - ActiveMQ
- Point to Point Protocol (PPP)
- 5.1. Point-to-Point Channel
- Point
- Cocoa消息,常用词
- 改变浏览器大小的时候 div浮动布局发生改变的问题。
- Luci页面配置
- git常用指令整理及说明(详细)
- 面向对象的"形"与"本"
- activemq学习笔记——spring整合(Point-To-Point点对点类型)
- LocalStorage,SessionStorage,Cookie
- C++ 产生guid
- SparseArray源码中的G点
- 谷歌浏览器在调试jquery的时候,防止跳入jquery文件
- 【性能测试】Linux性能监控命令——sar详解
- Python实现N皇后问题
- android 对图片进行裁剪的方法
- git 命令行