Different types of messages available in JMS API are TextMessage, BytesMessage, StreamMessage, ObjectMessage and MapMessage. As already known, JMS API offers specialised service of transmitting messages between brokers and nodes in a single direction asynchronously. The message bodies are available in javax.jms.Message of JMS API. However, actual classes of the messages are not available as part of API and they are defined by the corresponding providers.
Rest of the article explains in detail about different types of messages available in JMS API:
1. TextMessage
TextMessage is used to store string values of any specific length. Here is a code snippet to demonstrate TextMessage:
1 2 3 4 5 6 7 8 |
//Create a TextMessage TextMessage sampleTextMsg = session.createTextMessage(); //Store values within TextMessage sampleTextMsg.setText(“sample content”); // Retrieve values from TextMessage String storedText = sampleTextMsg.getText(); |
2. BytesMessage
BytesMessage is a basic type of message that can store sequence of bytes that are uninterpreted. BytesMessage can literally store anything. Here is an associated code snippet for BytesMessage:
1 2 3 4 5 6 7 8 9 10 |
//Create BytesMessage BytesMessage sampleBytesMsg = session.createBytesMessage(); //Storing an array of numbers in BytesMessage byte[] byteArray = new byte[]{66,68,70}; sampleBytesMsg.writeBytes(byteArray); // Fetch the stored numbers by doing required casting byte[] msgContent = new byte[3]; sampleBytesMsg.readBytes(msgContent); |
3. StreamMessage
StreamMessage is a type of message that can wrap a stream of primitive values that are of unlimited length. Given below is a code snippet demonstrating StreamMessage:
1 2 3 4 5 6 7 8 9 10 11 12 |
//Create a StreamMessage StreamMessage sampleStreamMsg = session.createStreamMessage(); //Store values within StreamMessage sampleStreamMsg.writeBoolean(false); sampleStreamMsg.writeBoolean(true); sampleStreamMsg.writeBoolean(false); // Retrieve values from StreamMessage System.out.println(sampleStreamMsg.readBoolean()); System.out.println(sampleStreamMsg.readBoolean()); System.out.println(sampleStreamMsg.readBoolean()); |
Output will be false, true, false.
4. ObjectMessage
Using ObjectMessage, an object can be wrapped as a message and transmitted across, but the object must be serializable. Here is a code snippet example of ObjectMessage:
1 2 3 4 5 6 7 8 9 10 11 |
//Create an ObjectMessage ObjectMessage sampleObjMsg = session.createObjectMessage(); //Create a ValueObject and initialize it with values ValueObject vObj = new ValueObject(‘sampleField’,54); //Store the ValueObject within ObjectMessage sampleObjMsg.setObject(vObj)); //Retrieve the stored ValueObject from the ObjectMessage vObj = (ValueObject) sampleObjMsg.getObject(); |
5. MapMessage
MapMessage is a type of message that is defined with a key value pair. Map is also referred as hash or dictionary. String objects are stored as Keys and String objects or any primitives can be stored as values. Since values can include primitives, it should be taken care that the value cannot be null. Here is a code snippet demonstrating MapMessage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//Create a MapMessage MapMessage sampleMapMsg = session.createMapMessage(); //Set Key Value Pair with type String sampleMapMsg.setString('SampleKey', 'SampleValue'); // Retrieve String value using Key sampleMapMsg.getString('SampleKey'); //Set Key Value Pair without specifying type sampleMapMsg.setObject('SampleKey1', 'SampleValue1'); // Retrieve Object value using Key sampleMapMsg.getObject(‘'SampleKey1'); |