Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days

The working model of a JMS messaging system, shown in Figure 15.3, consists of these parts:

  • A client that creates the message

  • A destination, also known as the queue or topic

  • Messages to be passed between the sender and receiver

  • Destinations (that is, queues and topics) and connection factories, also known as administered objects, which are configured by the administrator for use by the client

  • The JMS provider, which provides the JMS features, including a JNDI namespace and administrative tools

Figure 15.3. JMS API architecture.

A message, as defined earlier, has header fields, properties, and a body. Figure 15.4 illustrates the structure of a message. It has these components:

  • Message header A message contains a set of standard name/value pairs, which describe the message. Some of the header values are added by default into the message, whereas some are set by the JMS provider. All the values are available to the message consumers. The message headers contain information for routing, identification, expiration, and so on. Examples of the fields that can be set are the correlation ID containing the message ID, the delivery mode (used to indicate whether messaging is persistent or nonpersistent), the destination (queue or topic) to which the message and any reply should be delivered, the time-out period for the message, the message type, and many others. Once the message is received, its values can be reset or changed before being sent again by the application. Get and set methods for each of the possible header fields are defined in the JMS API and are implemented by the JMS provider.

  • Message properties These optional, application-specific Java name-value pairs are added by the message producer. These property names' syntax must conform to the specifications for a message selector in the javax.jms.Message. The JMS provider can filter messages, depending on the values present in the properties field.

  • Message body This part of the message contains the actual message contents. The contents can be represented as bytes, as text, or in many other ways. These types are described in detail a little later in the discussion of the JMS API.

Figure 15.4. Structure of a message object.

JMS Interfaces

The important interfaces of the JMS API implemented by the JMS provider are found in the javax.jms package.

Point-to-point messaging uses the following interfaces:

  • QueueConnection A queue connection is an active connection object to a queue provided by a JMS provider. This object is used by the sender and receiver to connect to the JMS provider's destination.

  • QueueConnectionFactory This interface is used to create a collection of QueueConnection objects to connect to a queue of a JMS provider.

  • QueueSender This interface is used by the sender application to send messages to a queue. The most commonly used method is the send() method. The parameters of this method are the Queue object (optional), the message (required), the delivery mode (optional), the priority (optional), and the lifetime of the queue (optional). The values that are optional, if not specified when the method is called, are given the default queue values, which are specified when the queue is created in the JMS provider. The return type for this method is void.

  • QueueReceiver This interface is used to receive messages from a queue by the receiver application. The commonly used methods of this interface are the receive() and receiveNoWait() methods, which are inherited from the MessageConsumer interface. The parameter of the receive method is the time-out (optional, type long; the default value is the value with which the queue is configured). The message is received only within the specified time-out interval. No parameters are required for the receiveNoWait method, because it does not have a waiting period at all.

  • QueueSession A QueueConnection object is created from the QueueSession object. This object provides a method to create the QueueReceiver and QueueSender objects and a few others. The most commonly used methods are the createQueue(String queuename_to_be_created) method, which is used to create a queue and returns a queue; createReceiver(Queue_object_with_which_to_be_associated), which is used to create a queue receiver and returns a QueueReceiver object; and createSender(Queue_object_with_which_to_be_associated), which is used to create a queue sender and returns a QueueSender object.

For publisher-subscriber messaging, the following topic counterparts of the queue come into the picture:

  • TopicConnection A topic connection is an active connection object to a topic provided by a JMS provider. This object is used by the sender and receiver to connect to the JMS provider's destination.

  • TopicConnectionFactory This interface is used to create a collection of TopicConnection objects to connect to a topic of a JMS provider.

  • TopicSender This interface is used by the sender application to send messages to a topic. The most commonly used method is the send() method. The parameters of this method are the Topic object (optional), the message (required), the delivery mode (optional), the priority (optional), and the lifetime of the topic (optional). The values that are optional, if not specified when the method is called, are given the default topic values, specified when the topic is created in the JMS provider. The return type for this method is void.

  • TopicReceiver This interface is used to receive messages from a topic by the receiver application. The commonly used methods of this interface are the receive() and receiveNoWait() methods, which are inherited from the MessageConsumer interface. The parameter of the receive method is the time-out (optional, type long; the default value is the value with which the topic is configured). The message is received only within the specified time-out interval. No parameters are required for the receiveNoWait method, because it does not have a waiting period at all.

  • TopicSession A TopicConnection object is created from the TopicSession object. This object provides a method to create the TopicReceiver and TopicSender objects and a few others. The most commonly used methods are createTopic(String topic_name_to_be_created) to create a topic, createReceiver(Topic_object_with_which_to_be_associated) to create a topic receiver, and createSender(Topic_object_with_which_to_be_associated) to create a topic sender.

The following interfaces are used for message creation:

  • MessageListener In an asynchronous messaging system, this interface is used to receive messages. A commonly used method in this interface is the onMessage() method, which takes as a parameter a Message object and is an event called by the JMS provider when it finds a message delivered on the destination (the topic or the queue).

  • Message This interface is the main interface for the whole JMS system. This is a lightweight object that is passed between applications using the JMS application provider. It mainly contains methods to get and set header properties. The message-body-specific methods are defined in the type-specific Message interfaces that extend this interface.

  • TextMessage This interface extends the Message interface. The methods to get and set text messages are defined in this interface. The most important message-body-related methods are setText(String textmessage) and getText, which returns a String.

  • ObjectMessage This interface extends the Message interface. The methods to get and set object messages are defined in this interface. Keep in mind that the objects that are set in this interface should be serializable. The most important message-body-related methods are setObject(java.io.Serializable messageObject) and getObject, which returns a java.io.Serializable Object.

  • BytesMessage MapMessage, and StreamMessage Other message types supported, which have APIs similar to that specified for TextMessage and ObjectMessage.

Категории