Spring Book – Chapter 17 – Messaging with Spring

Standard Message Properties

Message can optionally have additional standard properties which can be useful for client to filter messages out. They provide additional information about the data, such as which process created it and the time it was created and so forth. These message properties are optional and it is not mandatory for the JMS provider to support these properties.

The various standard message properties of the Message interface can be summarized as shown in Table 18-1 below.

Table 18-1. Standard JMS optional message property details

Header Field Type Description
JMSXUserID String Identify the user sending the message.
JMSXApplID String Identify the application sending the message
JMSXDeliveryCount int Number of times delivery of messages attempted.
JMSXGroupID String Identify the message group to which this message is associated.
JMSXGroupSeq int Sequence number of this message within the message group.
JMSXProducerTXID String Identify the transaction within which this message was produced.
JMSConsumerTXID String Identify the transaction within which this message was consumed.
JMSXRcvTimestamp long The time when JMS delivered the message to the consumer.
JMSXState int Not much used at this point in time.
JMSX_<vendor_name> Reserved for provider specific properties if any.

Message Body (Payload)

The actual content of the message is called as message body or payload. There are five forms of message and they have appropriate interfaces defined which intern extends from the root java.jms.Message interface. All these interfaces reside in javax.jms package. The various messages can be identified by these interfaces as follows:

  • javax.jms.TextMessage – a message whose body contains a Java string. Example for which is an XML message.
  • javax.jms.BytesMessage – a message whose body containing a stream of uninterrupted bytes.
  • javax.jms.StreamMessage – a message which contains a stream of Java primitive values that are filled and read sequentially using standard stream operations supported in Java.
  • javax.jms.MapMessage – a message whose body contains a set of name/value pairs. The names are of type string and the values are Java primitives.
  • javax.jms.ObjectMessage – a message whose body contains serialized Java objects.

Each JMS provider makes sure that they have appropriate implementation classes for these interfaces so as to support these messages.

Destination

The Java object javax.jms.Destination encapsulates the destination for the messages. It can also be represented as the target of messages that the client produces and the source of messages that the client consumes. It is an administered object that is retrieved from JNDI. There are two implementations of the Destination interface as given below:

  • Queue – used for point-to-point messaging.
  • Topic – used for publish/subscribe type messaging.

In “Message System Types” section we have covered both these models in detail with of figures to make you understand these fully in all aspects.

ConnectionFactory

In a typical enterprise application ConnectionFactory is an administered object and is bound to JNDI. It contains createConnection() method which is used to obtain a Connection object. Obtaining a ConnectionFactory from JNDI is shown in Listing 18-1 below.

Listing 18-1. Getting the ConnectionFactory object

Connection

Connection object is an encapsulation over the active connection to the JMS provider. It is obtained from the ConnectionFactory as shown in Listing 18-2 below.

Listing 18-2. Getting Connection from ConnectionFactory object

Some of the important methods in the Connection object is as summarized below:

  • createSession(boolean, int) – method which returns a Session object. The “boolean” parameter indicates whether the Session is transacted or not and the “int” indicates the acknowledgment mode.
  • start() – method which activates the delivery of messages from the provider.
  • stop() – method which temporarily stops delivery of messages. However, the delivery can be restarted with the start() method.
  • close() – method which is responsible for closing the connection to the provider and releases all resources held on its behalf.

Several purposes which connection serves can be summarized as below:

  • Encapsulation of open connection with the JMS provider. It typically represents an open TCP/IP socket between a client and the service provider software.
  • Its creation is where client authentication takes place.
  • It can specify a unique client identifier.
  • It provides a ConnectionMetaData object which provides information describing the Connection object.
  • It supports an optional ExceptionListener object which allows a client to be notified of a problem asynchronously.

Note: We will see “acknowledgement mode” in detail in later sections of this chapter.

Page Visitors: 16775

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

1 thought on “Spring Book – Chapter 17 – Messaging with Spring

  1. First of all I would like to say fantastic blog! I had a quick question which I’d like to ask if you don’t mind. I was interested to know how you center yourself and clear your head before writing. I have had difficulty clearing my mind in getting my ideas out there. I do enjoy writing but it just seems like the first 10 to 15 minutes are usually wasted just trying to figure out how to begin. Any recommendations or tips? Thank you!|

Leave a Reply

Your email address will not be published. Required fields are marked *