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
|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.
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.
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
Properties props = new Properties();//Set all the properties to create the context
Context ctx = new InitialContext(props);//Get the context
ConnectionFactory connectionFactory = (ConnectionFactory)ctx.lookup(“connFactory”);
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
Connection conn = connectionFactory.createConnection();
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: 13993