activemq发送和接收对象
来源:互联网 发布:淘宝宝贝被删除怎么办 编辑:程序博客网 时间:2024/06/05 14:51
1、网上的例子都是直接发送文本消息的, 我做了一个简单的变换,就是能够发送和接收对象。
首先,在这里确认一下版本,activemq版本是apache-activemq-5.4.1-bin.zip,activemq安装环境是win7,jdk是1.6版本。下载apache-activemq-5.4.1-bin.zip,到官网
http://activemq.apache.org/download.html,注意找对应的版本,然后下载解压到系统的任意一个目录。
如图,我这里是放在了我windows系统的e盘下面,然后双击activemq.bat文件,启动activemq,启动成功会出现下面这个界面:
请注意,如果activemq不能正常启动,出现这个问题的话:
java.io.EOFException: Chunk stream does not exist at page: 0
请参照这篇文章来解决
http://blog.csdn.net/technofantasy/article/details/6037295
2、将下载的activemq中的activemq-all-5.4.1.jar导入到工程中
3、发送类
package com.test.activemq;import java.io.Serializable;import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.DeliveryMode; import javax.jms.Destination; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory;import org.apache.activemq.command.ActiveMQObjectMessage;public class SenderMain {private static final int SEND_NUMBER = 5; public static void main(String[] args) {// TODO Auto-generated method stubConnectionFactory connectionFactory; // Connection :JMS 客户端到JMS // Provider 的连接 Connection connection = null; // Session: 一个发送或接收消息的线程 Session session; // Destination :消息的目的地;消息发送给谁. Destination destination; // MessageProducer:消息发送者 MessageProducer producer; // TextMessage message; // 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616"); try { // 构造从工厂得到连接对象 connection = connectionFactory.createConnection(); // 启动 connection.start(); // 获取操作连接 session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); // 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置 destination = session.createQueue("FirstQueue"); // 得到消息生成者【发送者】 producer = session.createProducer(destination); // 设置不持久化,此处学习,实际根据项目决定 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); // 构造消息,此处写死,项目就是参数,或者方法获取 sendMessage(session, producer); session.commit(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != connection) connection.close(); } catch (Throwable ignore) { } } }/* * 利用activemq发送java对象 * */public static void sendMessage(Session session, MessageProducer producer) throws Exception { Student s=new Student();s.setName("蒋辉文是一个非常不错的好人!");ActiveMQObjectMessage msg = (ActiveMQObjectMessage) session.createObjectMessage(); msg.setObject((Serializable) s); producer.send(msg); } }class Student implements Serializable{/** * */private static final long serialVersionUID = 1L;private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}}
4、接收类
package com.test.activemq;import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.MessageConsumer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.command.ActiveMQObjectMessage;public class ReceiveMain {public static void main(String[] args) {// TODO Auto-generated method stub// ConnectionFactory :连接工厂,JMS 用它创建连接 ConnectionFactory connectionFactory; // Connection :JMS 客户端到JMS Provider 的连接 Connection connection = null; // Session: 一个发送或接收消息的线程 Session session; // Destination :消息的目的地;消息发送给谁. Destination destination; // 消费者,消息接收者 MessageConsumer consumer; connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616"); try { // 构造从工厂得到连接对象 connection = connectionFactory.createConnection(); // 启动 connection.start(); // 获取操作连接 session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE); // 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置 destination = session.createQueue("FirstQueue"); consumer = session.createConsumer(destination); /* * 利用activemq接收java对象 * */ while (true) { // 设置接收者接收消息的时间,为了便于测试,这里谁定为100s ActiveMQObjectMessage msg = (ActiveMQObjectMessage) session.createObjectMessage(); msg = (ActiveMQObjectMessage) consumer.receive(100000); if(msg != null) { Student s=(Student) msg.getObject(); System.out.println("收到的消息对象:"+s.getName()); } } } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != connection) connection.close(); } catch (Throwable ignore) { } } } }
5、先运行发送类,再运行接收类,会打印出下面的消息
说明运行正常,先给消息中间件发消息,然后从消息中间件接收消息。
2 0
- activemq发送和接收对象
- ActiveMq发送接收对象
- ActiveMQ消息发送和接收
- ActiveMQ消息发送和接收
- ActiveMQ消息发送和接收
- ActiveMQ 消息的发送和接收
- JAVA ActiveMQ消息发送和接收
- ActiveMQ 发送消息和接收消息 Demo
- ActiveMQ 整合 Spring 发送和接收消息
- ActiveMQ(一):ActiveMQ入门案例 发送消息和接收消息
- ActiveMQ服务器之间传输对象,项目A发送对象到项目B接收发送对象《一》
- ActiveMQ服务器之间传输对象,项目A发送对象到项目B接收发送对象《二》
- jms+spring+activemq配置(发送和接收消息)
- jms+spring+activemq配置(发送和接收消息)
- JMS_使用ActiveMQ实现消息的发送和接收
- Activemq的topic发送和接收的例子
- Activemq的Queue发送和接收的例子
- activemq使用JMS发送消息和接收消息
- HTML5+NodeJs实现WebSocket即时通讯
- android 按钮点击1次,点击3次和点击6次触发不同的事件
- 004改进我们的小游戏
- 黑马程序员——多线程(二)
- UIActivityIndicatorView的属性和用法
- activemq发送和接收对象
- 如何查看java源码中的native方法源码
- Linux 进程组,作业,会话的理解
- 支持向量分类实用指南
- leetcode: Fraction to Recurring Decimal
- iOS应用内置付费In-App Purchases入门
- ext2 文件系统
- PhoneGap开发环境搭建
- 查看oracle数据库的sid