• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

StreamMessage

Different types of messages available in JMS API

April 21, 2017 By j2eereference Leave a Comment

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:

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:

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:

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:

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');

 

Related Posts

  • Differences between Point to Point Messaging Model and Publish Subscribe Messaging Model
  • Point to Point Messaging Model Architecture
  • Publish Subscribe Messaging Model Architecture
  • How JMS is different from RPC?
  • Advantages of Java Message Service (JMS)
  • Java Message Service and JMS Programming Model
  • What is JavaServer Pages (JSP)?
  • Java EE or J2EE Architecture

Filed Under: J2EE Tagged With: BytesMessage, different types of messages, javax.jms.Message, JMS API, MapMessage, message types, ObjectMessage, StreamMessage, TextMessage

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • What is parallel Stream
  • reduce method of the Stream class
  • Difference between the findFirst() and findAny() method
  • intern() method of String class
  • SOLID – Five principles of object-oriented software design
  • Java Coding Best Practices
  • How to use lambda expression effectively
  • Enhanced pseudo-Random Number Generators in java17
  • How to use Foreign-Memory Access API
  • Pattern Matching for instanceof
  • Text Blocks – Feature added in Java17
  • Record – The new feature added in java 17
  • What is Sealed Class
  • Features added in Java 17
  • Java Buzzwords

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.