Activemq消息类型
JMS规范中的消息类型包括TextMessage、MapMessage、ObjectMessage、BytesMessage、和StreamMessage等五种。ActiveMQ也有对应的实现,下面我们结合Spring JMS分别来看一下五种消息类型的收发代码。
1、TextMessage
/**
* 向指定Destination发送text消息
* @param destination
* @param message
*/
public void sendTxtMessage(Destination destination, final String message){
if(null == destination){
destination = jmsTemplate.getDefaultDestination();
}
jmsTemplate.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(message);
}
});
System.out.println("springJMS send text message...");
}

2、MapMessage

/**
* 向指定Destination发送map消息
* @param destination
* @param message
*/
public void sendMapMessage(Destination destination, final String message){
if(null == destination){
destination = jmsTemplate.getDefaultDestination();
}
jmsTemplate.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
MapMessage mapMessage = session.createMapMessage();
mapMessage.setString("msgId",message);
return mapMessage;
}
});
System.out.println("springJMS send map message...");
}

3、ObjectMessage

/**
* 向指定Destination发送序列化的对象
* @param destination
* @param object object 必须序列化
*/
public void sendObjectMessage(Destination destination, final Serializable object){
if(null == destination){
destination = jmsTemplate.getDefaultDestination();
}
jmsTemplate.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createObjectMessage(object);
}
});
System.out.println("springJMS send object message...");
}

4、BytesMessage

/**
* 向指定Destination发送字节消息
* @param destination
* @param bytes
*/
public void sendBytesMessage(Destination destination, final byte[] bytes){
if(null == destination){
destination = jmsTemplate.getDefaultDestination();
}
jmsTemplate.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
BytesMessage bytesMessage = session.createBytesMessage();
bytesMessage.writeBytes(bytes);
return bytesMessage; }
});
System.out.println("springJMS send bytes message...");
}

5、streamMessage

/**
* 向默认队列发送Stream消息
*/
public void sendStreamMessage(Destination destination) {
jmsTemplate.send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
StreamMessage message = session.createStreamMessage();
message.writeString("stream string");
message.writeInt(11111);
return message;
}
});
System.out.println("springJMS send Strem message...");
}

消息接收处理

/**
* 根据消息类型进行对应的处理
* @param destination 消息发送/接收共同的Destination
* @throws JMSException
*/
public void receive(Destination destination) throws JMSException {
Message message = jmsTemplate.receive(destination); // 如果是文本消息
if (message instanceof TextMessage) {
TextMessage tm = (TextMessage) message;
System.out.println("from" + destination.toString() + " get textMessage:\t" + tm.getText());
} // 如果是Map消息
if (message instanceof MapMessage) {
MapMessage mm = (MapMessage) message;
System.out.println("from" + destination.toString() + " get textMessage:\t" + mm.getString("msgId"));
} // 如果是Object消息
if (message instanceof ObjectMessage) {
ObjectMessage om = (ObjectMessage) message;
ExampleUser exampleUser = (ExampleUser) om.getObject();
System.out.println("from" + destination.toString() + " get ObjectMessage:\t"
+ ToStringBuilder.reflectionToString(exampleUser));
} // 如果是bytes消息
if (message instanceof BytesMessage) {
byte[] b = new byte[1024];
int len = -1;
BytesMessage bm = (BytesMessage) message;
while ((len = bm.readBytes(b)) != -1) {
System.out.println(new String(b, 0, len));
}
} // 如果是Stream消息
if (message instanceof StreamMessage) {
StreamMessage sm = (StreamMessage) message;
System.out.println(sm.readString());
System.out.println(sm.readInt());
}
}

Activemq 消息类型 (转)的更多相关文章

  1. Activemq消息类型

    Activemq消息类型JMS规范中的消息类型包括TextMessage.MapMessage.ObjectMessage.BytesMessage.和StreamMessage等五种.ActiveM ...

  2. activemq 消息类型

    //文本消息 TextMessage textMessage = session.createTextMessage("文本消息"); producer.send(textMess ...

  3. ActiveMQ之二--JMS消息类型

    1.前言 //发送文本消息 session.createTextMessage(msg); //接受文本消息 public void onMessage(Message msg) { TextMess ...

  4. 学习ActiveMQ(五):activemq的五种消息类型和三种监听器类型

    一.前面我们一直发送的是字符串类型,其实activemq一共支持五种消息类型: 1.String消息类型:发送者:消费者: 1.String消息类型:发送者:消费者: 1.String消息类型:发送者 ...

  5. ActiveMQ常见消息类型

    JMS由下面三部分组成:消息头.属性.消息体.其中消息体定义了五种消息体格式,也可以称为消息类型. JMS规范中的消息类型包括TextMessage.MapMessage.ObjectMessage. ...

  6. JAVA的设计模式之观察者模式----结合ActiveMQ消息队列说明

    1----------------------观察者模式------------------------------ 观察者模式:定义对象间一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的 ...

  7. JAVAEE——宜立方商城08:Zookeeper+SolrCloud集群搭建、搜索功能切换到集群版、Activemq消息队列搭建与使用

    1. 学习计划 1.solr集群搭建 2.使用solrj管理solr集群 3.把搜索功能切换到集群版 4.添加商品同步索引库. a) Activemq b) 发送消息 c) 接收消息 2. 什么是So ...

  8. Activemq消息确认机制 --转载

      转自:http://blog.csdn.net/czp11210/article/details/47022639 ActiveMQ消息传送机制以及ACK机制详解 AcitveMQ是作为一种消息存 ...

  9. ActiveMQ消息的延时和定时投递

    ActiveMQ对消息延时和定时投递做了很好的支持,其内部启动Scheduled来对该功能支持,也提供了一个封装的消息类型:org.apache.activemq.ScheduledMessage,只 ...

随机推荐

  1. Python学习笔记三:数据类型

    数据类型 整数int 32位机器,-2**31~2**31-1,即-2147483648~2147483647(4亿多) 64位机器,-2**63~2**63-1,非常大了. 长整型long 没有位数 ...

  2. print&input--Python

    1.print --> 打印到屏幕输出字符串 print("this is a dog!") -->代码 D:\Python\venv\Scripts\python.e ...

  3. Android零碎知识点

    1.android:foreground="?attr/selectableItemBackground"   ###设置水波纹效果 2.android:contentDescri ...

  4. BZOJ1012_Maxnumber_KEY

    题目传送门 这是一道单调栈的问题,单调栈维护所有数的最大值. 查询操作时只需要二分找答案即可,枚举栈内的数应该也不会超时. code: /******************************* ...

  5. VINS(六)边缘化

    通常的边缘化是将联合概率分布分解为边缘概率分布和条件概率分布的过程,这样可以将Sliding Window中较旧的状态边缘化出Sliding Window,同时保留其信息.并且保证了对应H海塞矩阵的稀 ...

  6. Ceres优化

    Ceres Solver是谷歌2010就开始用于解决优化问题的C++库,2014年开源.在Google地图,Tango项目,以及著名的SLAM系统OKVIS和Cartographer的优化模块中均使用 ...

  7. Updating Homebrew... 长时间不动解决方法

    确保你已安装Homebrew 依次输入下面的命令(注意:不要管重置部分的命令,这里原作者贴出来.我也贴出来是以防需要重置的时候有参考操作命令) 替换brew.git: cd "$(brew ...

  8. Spark性能优化--数据倾斜调优与shuffle调优

    一.数据倾斜发生的原理 原理:在进行shuffle的时候,必须将各个节点上相同的key拉取到某个节点上的一个task来进行处理,比如按照key进行聚合或join等操作.此时如果某个key对应的数据量特 ...

  9. Ubuntu目录与权限

    Ubuntu目录 / /bin /sbin /boot /etc /mnt /home d :directory - :file b :block  磁盘以块进行 l :link Ubuntu权限 U ...

  10. 180605-Linux下Crontab实现定时任务

    Linux下Crontab实现定时任务 基于Hexo搭建的个人博客,是一种静态博客页面,每次新增博文或者修改,都需要重新的编译并发布到Github,这样操作就有点蛋疼了,一个想法就自然而然的来了,能不 ...