ActiveMQ(四):Topic方式使用MessageListener监听的方式接收消息
来源:互联网 发布:网络推广职位描述 编辑:程序博客网 时间:2024/04/28 07:01
发布/订阅模式
消息发布者:
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-autowire="byName"> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616" /> <property name="userName" value="admin" /> <property name="password" value="admin" /> </bean> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory" /> </bean> <bean id="destination" class="org.apache.activemq.command.ActiveMQTopic"> <!-- 设置消息队列的名字 --> <constructor-arg index="0" value="STATUS" /> </bean> </beans>
发送消息:
package ActiveMQ;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.Session;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import org.springframework.jms.core.JmsTemplate;import org.springframework.jms.core.MessageCreator;public class HelloSender { /** * @param args * jmsTemplate和destination都是在spring配置文件中进行配制的 * Sender只使用了配置文件中的jmsFactory,jmsTemplate,还有destination这三个属性 */ public static void main(String[] args) { ApplicationContext applicationContext = new FileSystemXmlApplicationContext("conf/applicationContext-jms.xml"); JmsTemplate template = (JmsTemplate) applicationContext.getBean("jmsTemplate"); Destination destination = (Destination) applicationContext.getBean("destination"); template.send(destination, new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage("发送消息:Hello ActiveMQ Text Message2!"); } }); System.out.println("成功发送了一条JMS消息"); }}
消息接收者:
xml配置:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:jaxws="http://cxf.apache.org/jaxws" 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.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd"> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616" /> <property name="userName" value="admin" /> <property name="password" value="admin" /> </bean> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory" /> </bean> <bean id="mqDestination" class="org.apache.activemq.command.ActiveMQTopic"> <!-- 设置消息队列的名字 --> <constructor-arg index="0" value="STATUS" /> </bean> <bean id="messageListener" class="activeMQ.ConsumerMessageListener"></bean> <bean id="listenerContainer" class="org.springframework.jms.listener.SimpleMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="mqDestination" /> <property name="messageListener" ref="messageListener" /> </bean> </beans>
消息监听者:
package activeMQ;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageListener;import javax.jms.ObjectMessage;import javax.jms.TextMessage;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import org.springframework.jms.core.JmsTemplate;//监听代码 public class ConsumerMessageListener implements MessageListener{ @Override public void onMessage(Message message) { // TODO Auto-generated method stub System.out.println("topic收到的消息:"+message); TextMessage textmessage = (TextMessage)message; try { System.out.println("message:"+textmessage.getText()); } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
消费者代码:
package activeMQ;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;//这是消费者代码,这里你可以创建多个XML文件,模拟多个消费者public class JmsTopicReceiver { public static void main(String[] args){ //加载消费者监听 ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/applicationContext.xml"); //写个死循环 while(true){} }}
测试:
先启动消息监听者JmsTopicReceiver:
再启动消息发送端:
在监控端即可监控到发送的消息:
0 0
- ActiveMQ(四):Topic方式使用MessageListener监听的方式接收消息
- ActiveMQ 消息监听 MessageListener 的使用
- ActiveMQ(三):使用Topic方式发送消息
- ActiveMQ 消息接收方式
- Activemq 使用Stomp方式接收、过滤 topic 数据
- ActiveMQ消息处理机制-Topic方式
- 消息队列ActiveMQ(一)——Queue方式和Topic方式
- javase 用来接收activemq消息的两种方式
- ActiveMQ——2.消息的接收方式以及选择器
- 学习淘淘商城第五十八课(ActiveMQ发送topic消息和接收topic消息)
- ActiveMQ的消息存储方式
- ActiveMQ的消息存储方式
- JMS(二):MessageListener,实战Queue ,实战Topic,消息头
- ActiveMQ(三):消息监听器 MessageListener
- ActiveMQ 发布订阅(topic)模式_发送消息和接收消息
- 消息队列技术终结者(四)—消息消费者以何种方式接收消息
- 以SpringMVC框架为中心疯狂扩展-06、MessageListener实时监听ActiveMQ中的消息
- 【消息队列MQ】ActiveMq的通信方式
- easy=ui 树形结构案例
- 认识自己——恐惧的奴隶4:爸爸
- js刷新页面方法大全
- [安卓]手机管家(二十) 缓存清理
- Redis安装
- ActiveMQ(四):Topic方式使用MessageListener监听的方式接收消息
- 下载各种软件的地址 VMWARE
- TCP/IP参考模型的详解
- 黑马程序员——Objective-C语言知识整理——封装、继承与多态
- 根据英语谐音记单词
- WPF的WebBrowser屏蔽弹出脚本错误窗口
- Could not find any version that matches com.android.support:appcompat-v7:+
- 测试
- chmod命令