Chapter 7. Developing Applications with JBoss Messaging Service

The medium is the message. Marshall McLuhan

Messaging is a method of communication between software components or applications. The Java Message Service (JMS) is a Java API designed by Sun that allows applications to create, send, receive, and read messages (refer to http://java.sun.com/products/jms/docs.html).

Messaging differs from other standard protocols, such as Remote Method Invocation (RMI) and Hypertext Transfer Protocol (HTTP), in two ways. Firstly, the conversation is mediated by a messaging server, so it's not a two-way conversation between peers. Secondly, the sender and receiver need to know only the message format and the destination to be used. This is in contrast to tightly coupled technologies such as RMI, which requires an application to know a remote application's methods.

In this chapter, we will cover:

  • A brief introduction to message-oriented systems
  • The new JBoss Messaging system that replaces the earlier JBossMQ
  • Setting up some proof-of-concept programming examples

Short introduction to JMS

JMS defines a vendor-neutral (but Java-specific) set of programming interfaces for interacting with asynchronous messaging systems. Messaging enables distributed communication, which is loosely coupled. A component sends a message to a destination, which in turn is retrieved by the recipient with the mediation of the JMS server. In JMS, there are two types of destinations, topics and queues, and they have different semantics.

In the point-to-point model, messages are sent from producers to consumers through queues. A given queue may have multiple receivers, but only one receiver may consume each message. The first receiver to fetch the message will get it, while everyone else will not.

Short introduction to JMS

On the other hand, a message sent to a topic may be received by multiple parties. Messages published on a specific topic are sent to all message consumers who have registered (subscribed) to receive messages on that topic. A subscription can be durable or non-durable. A non-durable subscriber can receive only those messages that are published while it is active. Also, non-durable subscription does not guarantee the delivery of the message or may deliver the same message more than once. On the other hand, a durable subscription guarantees that the consumer receives the message only once.

Short introduction to JMS

Even though JMS is inherently asynchronous, the JMS specification allows messages to be consumed in either of the following two ways:

  • Synchronous: A subscriber or a receiver explicitly fetches the message from the destination by calling the receive() method of any MessageConsumer instance. The receive() method can block until a message arrives, or can time-out if a message does not arrive within a specified time limit.
  • Asynchronous: With the asynchronous mode, the client must implement the javax.jms.MessageListener interface and overwrite the method onMessage(). Whenever a message arrives at the destination, the JMS provider delivers the message by calling the listener's onMessage() method that acts on the contents of the message.

A JMS message consists of a header, properties, and a body. The message headers provide a fixed set of metadata fields describing the message, using information such as where the message is going and when it was received. The properties are a set of key-value pairs used for application-specific purposes, usually to help filter messages quickly when they have been received. Finally, the body contains the data that is being sent in the message.

The JMS API supports two delivery modes for messages. If the JMS provider fails, these modes specify whether the messages are lost. The two modes are as described:

  • PERSISTENT: This is the default mode. It instructs the JMS provider to take extra care, to ensure that a message is not lost in transit in case of a JMS provider failure. A message sent with this delivery mode is logged to stable storage when it is sent.
  • NON_PERSISTENT: This delivery mode does not require the JMS provider to store the message or guarantee that it is not lost if the provider fails.

The building blocks of JMS

The basic building blocks of any JMS application are as follows:

  • Administered objects (connection factories and destinations):

    A ConnectionFactory object encapsulates a set of connection configuration parameters that have been defined by an administrator. A client uses it to create a connection with a JMS provider. A ConnectionFactory object hides provider-specific details from JMS clients and abstracts administrative information into objects in the Java programming language.

    A destination is the component that a client uses to specify the target of messages it produces and the source of messages it consumes. In the point-to-point (PTP) messaging domain, destinations are called queues, whereas in the publish/subscribe messaging domain, destinations are called topics.

  • Connections:

    A connection encapsulates a virtual connection with a JMS provider. A connection could represent an open TCP/IP socket between a client and a service provider. A connection is used to create one or more sessions.

  • Sessions:

    A session is a single-threaded context for producing and consuming messages. Sessions are used to create message producers, message consumers, and messages. Sessions serialize the execution of message listeners and provide a transactional context to group a set of sent and received messages into an atomic unit of work.

  • Message producers:

    A message producer is an object created by a session and is used for sending messages to a destination. The PTP form of a message producer implements the QueueSender interface. The publish/subscribe form implements the TopicPublisher interface.

  • Message consumers:

    A message consumer is an object created by a session and is used for receiving messages sent to a destination. A message consumer allows a JMS client to register interest in a destination with a JMS provider. The JMS provider manages the delivery of messages from a destination to the registered consumers of the destination. The PTP form of message consumer implements the QueueReceiver interface, whereas the publish/subscribe form implements the TopicSubscriber interface.

  • Messages:

    The Message is an object that contains the data being transferred between JMS clients. In developer terms, it is defined as an interface that defines the message header and the acknowledge method used for all messages.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.13.76