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());
}
}

最新文章

  1. 工行ATM转账——事务操作
  2. Struts2 简介
  3. xml/map转换器,递归设计思路
  4. 更改Android Studio的主题背景
  5. sql篇 select from where group by having order by
  6. Sprint第三个冲刺(第七天)
  7. Super Object Toolkit (支持排序)
  8. python之递归
  9. JSP(二)
  10. Zabbix监控Linux主机设置
  11. ThinkPhp学习09
  12. [转]Ubuntu系统下常用的新建、删除、拷贝文件命令
  13. Tomcat与Nginx、Apache结合的相关实践
  14. 【LeetCode】187. Repeated DNA Sequences
  15. Mysql 用法
  16. COMPUTE子句和Group By
  17. java使用google开源工具实现图片压缩【转】
  18. ECharts图形库
  19. nginx 长连接keeplive
  20. Sqoop 学习之路

热门文章

  1. js分片上传大文件,前端代码
  2. [Real World Haskell翻译]第27章 网络通信和系统日志 Sockets and Syslog
  3. CC3200-LAUNCHXL驱动不能正常识别的问题
  4. libevent学习八(evbuffer)
  5. 关于Python的多重排序
  6. mysql面试常见题目3
  7. word record 01
  8. C if 判断 else 否则
  9. ThreadLocal 线程的私有内存
  10. 【第三章】MySQL数据库的字段约束:数据完整性、主键、外键、非空、默认值、自增、唯一性