Messaging is a method of communication between software components where a Client can send messages to and receive messages from any other Client. Each client connects to a messaging Agent that provides facilities for creating, sending, receiving and reading messages.
- Loosely coupled, the destination is all that the sender and receiver have in common.
- Reliable, a message is delivered once and only once, though lower levels of reliability are available.
Java Message Service (JMS) is a Java API defines a common set of interfaces and associated semantics that allow applications written in the Java programming language to communicate with other messaging implementations.
- Application clients, Enterprise JavaBeans components and web components can send or synchronously receive a JMS Message.
- Can be set a message listener that allows JMS messages to be delivered to it asynchronously by being notified when a message is available.
- Message-driven beans enable asynchronous consumption of messages in the EJB container.
- Applications typically pools message-driven beans to implement concurrent processing of messages.
- Message send and receive can participate in Java Transaction API (JTA) transactions, which allow JMS operations and database accesses to take place within a single transaction.
- By accessing the JMS provider through a resource adapter in a Java EE Connector architecture
- Vendors can create JMS providers that can be plugged in to multiple application servers.
- Application servers can support multiple JMS providers.
The current release, defined in the JSR-343, is 2.0 and is part of Java EE 7.
JMS API Architecture
A JMS application is composed of the following parts
- JMS provider is a messaging system that implements the JMS interfaces and provides administrative and control features.
- JMS clients are the components that produce and consume messages.
- Messages are the objects that communicate information between the JMS clients.
- Administered objects are JMS objects configured for the use of clients.
- Connection factories
- Point to Point
- Publish / Subscriber
- Point to Point
- Publish / Subscriber
- Asynchronous, register a message listener with a consumer.
JMS API Programming Model
The basic building blocks of a JMS application are
- Administered objects, which are portable (ordinarily defined in a JNDI namespace and injected into JMS clients).
- Connection Factories, may be ConnectionFactory, QueueConnectionFactory or TopicConnectionFactory.
- Destinations, objects (queue or topic) a client uses to specify the target of messages it produces and the source of messages it consumes.
- The behavior of the application will depend on the kind of destination you use and not on the kind of connection factory you use.
- JMSContext objects, which combine a Connection and a Session in one object
- A Connection encapsulates a virtual connection with a JMS provider (could represent an open TCP/IP socket between a client and a provider service daemon).
- A Session is a single-threated context for producing and consuming messages
- Provides a transactional context with which to group a set of sends and receives into an atomic unit of work.
- Message producers (JMSProducer), is a lightweight object (so no need to store it) created by a JMSContext or a session and used for sending messages to a destination.
- Message consumers (JMSConsumer)
- Can be switched on and off (start / stop).
- Set a MessageListener for asynchronous messages.
- Use a Message Selector (a String that contains an expresion) for filtering the messages received.
- Messages. Can have
- Header (required): JMSMessageID, JMSCorrelationID, JMSType (TextMessage, MapMessage, BytesMessage, StreamMessage, ObjectMessage, Message), JMSDestination, JMSReplyTo, JMSDeliveryMode, JMSDeliveryTime, JMSExpiration, JMSPriority, JMSTimestamp, JMSRedelivered.
- Properties (optional), if additional values are needed for compatibility or for filtering (message selectors).
- Body (optional), each of the 6 possible Message Types has different Message Bodies.
- Queue browsers.
- JMSException is the root class for all checked exceptions in the JMS API, JMSRuntimeException is for all unchecked.
- MOM Messaging-Oriented Middleware.