ActiveMQ(二):使用队列Queue方式发送消息
来源:互联网 发布:淘宝有没有分期付款的 编辑:程序博客网 时间:2024/04/29 02:56
代码示例:队列queue方式发送消息
配置activeMQ服务信息与实现springjms的对应接口
<?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.ActiveMQQueue"> <!-- 设置消息队列的名字 --> <constructor-arg index="0" value="STATUS" /> </bean> <bean id="MessageSenderBean" class="activeMQ.MessageSender"> <constructor-arg name="template"> <ref bean="jmsTemplate" /> </constructor-arg> <constructor-arg name="destination"> <ref bean="mqDestination" /> </constructor-arg> </bean> <bean id="MessageReceiverBean" class="activeMQ.MessageReceiver"> <constructor-arg name="template"> <ref bean="jmsTemplate" /> </constructor-arg> <constructor-arg name="destination"> <ref bean="mqDestination" /> </constructor-arg> </bean> </beans>
消息生产者
package activeMQ;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.Session;import javax.ws.rs.Path;import javax.ws.rs.Produces;import org.springframework.jms.core.MessageCreator;public class MymessageCreator implements MessageCreator{ public int n = 0; private static String str1 = "这个是第"; private static String str2 = "个测试消息"; private String str = ""; @Override public Message createMessage(Session session) throws JMSException{ System.out.println("MyMessageCreator n=" +n); if(n == 9){ return session.createTextMessage("end"); } str = str1 + n +str2; return session.createTextMessage(str); }}
发送消息方
package activeMQ;import java.util.ArrayList;import java.util.List;import javax.jms.Destination;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import org.springframework.jms.core.JmsTemplate;public class MessageSender extends Thread{ private static JmsTemplate jmsTemplate; private static Destination destination; public MessageSender(JmsTemplate template,Destination destination){ this.jmsTemplate = template; this.destination = destination; } public static void main(String args[]) throws Exception{ ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/applicationContext.xml"); MessageSender myMessageSender = (MessageSender)ctx.getBean("MessageSenderBean"); for(int i = 1; i < 100; i++){ System.out.println("发送 i=" + i); MymessageCreator myMessageCreator = new MymessageCreator(); myMessageCreator.n = i; jmsTemplate.send(destination,myMessageCreator); sleep(1000); } }}
消息发送方在发送消息时会自动调用MymessageCreator实现的MessageCreator类中的createMessage()方法来创建会话session中发送的消息。
消息接收方
package activeMQ;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.TextMessage;import javax.sound.midi.SysexMessage;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import org.springframework.jms.core.JmsTemplate;public class MessageReceiver { private static JmsTemplate jmsTemplate; private static Destination destination; public MessageReceiver(JmsTemplate template,Destination destination){ this.jmsTemplate = template; this.destination = destination; } public static void main(String args[]) throws Exception{ ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/applicationContext.xml"); MessageReceiver myMessageReceiver = (MessageReceiver)ctx.getBean("MessageReceiverBean"); TextMessage msg = null; boolean isContinue = true; while(isContinue){ msg = (TextMessage)jmsTemplate.receive(destination); System.out.println("收到消息:" + msg.getText()); if(msg.getText().equals("end")){ isContinue = false; System.out.println("收到退出消息,程序要退出!"); } } System.out.println("程序退出了!"); }}
测试:
运行发送方MessageSender和接受方MessageReceiver(顺序随便,因为采用对列Queue的形式,发送的消息都会存在队列value=”STATUS”中,消息接收方启动之后就会去取存在队列中的消息,即使接收方启动晚,或者发送发关闭了,接收方都会正常接收完所有数据。)
0 0
- ActiveMQ(二):使用队列Queue方式发送消息
- ActiveMQ (二) 使用Queue或者Topic发送/接受消息
- ActiveMQ发送queue消息
- ActiveMQ 使用Queue或者Topic发送/接受消息
- spring-jms(activemq实现)使用queue发送消息简单例子
- 消息队列ActiveMQ(一)——Queue方式和Topic方式
- activeMQ消息队列消息的发送
- ActiveMQ(三):使用Topic方式发送消息
- ActiveMQ消息处理机制-Queue方式
- ActiveMQ消息处理机制-Queue方式
- ActiveMQ实现消息队列发送邮件
- 使用ActiveMQ 发送/接收消息
- 使用JMS发送消息--ActiveMQ
- MQ系列3 使用Spring发送,消费topic和queue消息 activeMQ
- spring整合activemq发送消息[queue类型]实例
- spring整合activemq发送消息[queue类型]实例
- 淘淘商城系列——ActiveMQ发送queue消息
- RabbitMQ(python实现)学习之二:Producer发送消息至多个消息队列queue(广播消息)
- 【思维突破】——撑过最难的阶段,接下来就是海阔天空
- 小记
- 【Codeforces69E】Subsegments
- 离散数学编程作业
- chrome 开发者工具不完全指南(二、进阶篇)
- ActiveMQ(二):使用队列Queue方式发送消息
- 深度学习(二)theano环境搭建
- 物联网系统的搭建(八)
- Hibernate入门教程
- Message Flood
- 冒泡算法
- >/dev/null 2>&1 与 2>&1 >/dev/null 的区别及使用技巧
- android光传感器数据的获取及储存
- echop JS Library