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:
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.
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.
Even though JMS is inherently asynchronous, the JMS specification allows messages to be consumed in either of the following two ways:
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. 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:
The basic building blocks of any JMS application are as follows:
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.
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.
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.
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.
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.
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.
3.133.13.76