Please reprint from the source: http://eksliang.iteye.com/blog/2242642
In the introduction to JMS and ActiveMQ (1) , the composition of the JMS message is introduced, which consists of three parts, head (header), properties (attribute), body (package body), in which the body stores the content to be sent to the receiving application. . Each message interface is specific to the content types it supports. JMS provides their own message types for different types of content, but all messages derive from the Message interface.
- StreamMessage A message whose body contains a stream of Java primitive values. It is filled and read sequentially.
- MapMessage A message whose body contains a set of key-value pairs. The order of entries is not defined.
- TextMessage A message (for example, an XML message) that contains Java strings in the body.
- ObjectMessage A message whose body contains serialized Java objects.
- BytesMessage A message whose body contains a continuous stream of bytes.
The following is an example of the five types of messages:
1. Serialize the object for the test of ObjectMessage. The code is as follows:
package com.gosun.activemq.advanced; import java.io.Serializable; public class User implements Serializable{ private static final long serialVersionUID = 1454L; private String userName; private String userPwd; private Integer sex; private Float sal; get() set().....! @Override public String toString() { return "User [userName=" + userName + ", userPwd=" + userPwd + ", sex=" + sex + ", sal=" + sal + "]"; } }
2. The producer who sends the message
package com.gosun.activemq.advanced; import javax.jms.BytesMessage; import javax.jms.Connection; import javax.jms.DeliveryMode; import javax.jms.Destination; import javax.jms.MapMessage; import javax.jms.MessageProducer; import javax.jms.ObjectMessage; import javax.jms.Session; import javax.jms.StreamMessage; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.command.ActiveMQQueue; /** * The message queue model sends messages to activeMQ * @author Ickes */ public class QueueSend { public static void main(String[] args) throws Exception { ActiveMQConnectionFactory connectionfactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); Connection connection = connectionfactory.createConnection(); connection.start(); Session session =connection.createSession(false,Session.AUTO_ACKNOWLEDGE); Destination destination = new ActiveMQQueue("gosun"); MessageProducer producer = session.createProducer(destination); //settings are not persistent producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); // send -- text message TextMessage msgText = session.createTextMessage(); msgText.setText("I am a text message"); //Send --Map message MapMessage msgMap = session.createMapMessage(); msgMap.setString("name","ickes"); msgMap.setInt("sal",12000); //send -- serialize message ObjectMessage msgObj = session.createObjectMessage(); User user = new User(); user.setUserName("ickes"); user.setSex(1); user.setUserPwd("1234"); user.setSal(120000f); msgObj.setObject(user); // send -- message flow StreamMessage smsg = session.createStreamMessage(); smsg.writeString("I am StreamMessage"); // send --byte message String byStrs = "I am a byte message"; BytesMessage bmsg = session.createBytesMessage (); bmsg.writeBytes(byStrs.getBytes()); //Step 6: The producer sends a message to the JMS queue producer.send(msgText); producer.send(msgMap); producer.send(msgObj); producer.send(smsg); producer.send(bmsg); //Step 7: Close the connection session.close(); connection.close(); } }
3. Consumers who receive messages
package com.gosun.activemq.advanced; import javax.jms.BytesMessage; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.MapMessage; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.ObjectMessage; import javax.jms.Session; import javax.jms.StreamMessage; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; /** * Message consumer, manual receiving example * @author Ickes */ public class QueuesAccept { public static void main(String[] args) throws Exception { ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); Connection conn = connectionFactory.createConnection(); Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); conn.start(); // message destination Destination dest = session.createQueue("gosun"); // message consumer MessageConsumer consumer = session.createConsumer(dest); //Receive messages, the timeout is 10 seconds, first manually accept JMS messages, you can use monitoring here Message msg = consumer.receive(10*1000); if(msg instanceof TextMessage){//Receive text message System.out.println(((TextMessage) msg).getText()); }else if(msg instanceof MapMessage){//Receive map message System.out.println(((MapMessage) msg).getString("name")); System.out.println(((MapMessage) msg).getString("sal")); }else if (msg instanceof ObjectMessage) {//Receive serial number message System.out.println(((ObjectMessage) msg).getObject()); }else if(msg instanceof StreamMessage){ //Receive stream message System.out.println(((StreamMessage)msg).readString()); }else if(msg instanceof BytesMessage){ //Receive byte message byte[] bs = new byte[1024]; BytesMessage message = (BytesMessage) msg; while(message.readBytes(bs) !=- 1){ System.out.println(new String(bs)); } } //close the channel consumer.close(); session.close(); conn.close(); } }