ActiveMQ學習筆記(四)——經過ActiveMQ收發消息http://my.oschina.net/xiaoxishan/blog/380446 和ActiveMQ學習筆記(五)——使用Spring JMS收發消息http://my.oschina.net/xiaoxishan/blog/381209 中,發送和接受的消息類型都是TextMessage,即文本消息(以下面的代碼所示)。顯然消息類型只有文本類型是不能知足要求的。html
//發送文本消息 session.createTextMessage(msg); //接受文本消息 public void onMessage(Message msg) { TextMessage message = (TextMessage) msg; …… }
根據Message接口的方法,能夠獲取消息類型java
String msgType = getJMSType()
根據 JSR 914: JavaTM Message Service (JMS) API ,JMS規範中的消息類型包括TextMessage、BytesMessage、MapMessage、StreamMessage和ObjectMessage 等五種。ActiveMQ也有對應的實現,下面咱們結合Spring JMS分別來看一下五種消息類型的收發代碼。session
/** * 向默認隊列發送text消息 */ public void sendMessage(final String msg) { String destination = jmsTemplate.getDefaultDestination().toString(); System.out.println("ProducerService向隊列" + destination + "發送了消息:\t" + msg); jmsTemplate.send(new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage(msg); } }); } /** * 向默認隊列發送map消息 */ public void sendMapMessage() { jmsTemplate.send(new MessageCreator() { public Message createMessage(Session session) throws JMSException { MapMessage message = session.createMapMessage(); message.setString("name", "小西山"); return message; } }); } /** * 向默認隊列發送Object消息 */ public void sendObjectMessage() { jmsTemplate.send(new MessageCreator() { public Message createMessage(Session session) throws JMSException { Staff staff = new Staff(1, "搬磚工"); // Staff必須實現序列化 ObjectMessage message = session.createObjectMessage(staff); return message; } }); } /** * 向默認隊列發送Bytes消息 */ public void sendBytesMessage() { jmsTemplate.send(new MessageCreator() { public Message createMessage(Session session) throws JMSException { String str = "BytesMessage 字節消息"; BytesMessage message = session.createBytesMessage(); message.writeBytes(str.getBytes()); return message; } }); } /** * 向默認隊列發送Stream消息 */ public void sendStreamMessage() { jmsTemplate.send(new MessageCreator() { public Message createMessage(Session session) throws JMSException { String str = "StreamMessage 流消息"; StreamMessage message = session.createStreamMessage(); message.writeString(str); message.writeInt(521); return message; } }); }
/** * 接受消息 */ public void receive(Destination destination) throws JMSException { Message message = jmsTemplate.receive(destination); // 若是是文本消息 if (message instanceof TextMessage) { TextMessage tm = (TextMessage) message; System.out.println("ConsumerService從隊列" + destination.toString() + "收到了消息:\t" + tm.getText()); } // 若是是Map消息 if (message instanceof MapMessage) { MapMessage mm = (MapMessage) message; System.out.println("ConsumerService從隊列" + destination.toString() + "收到了消息:\t" + mm.getString("name")); } // 若是是Object消息 if (message instanceof ObjectMessage) { ObjectMessage om = (ObjectMessage) message; Staff staff = (Staff) om.getObject(); System.out.println("ConsumerService從隊列" + destination.toString() + "收到了消息:\t" + staff); } // 若是是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()); } }