Java Messaging Service JMS specification and rationale

Source: Internet
Author: User
Tags message queue

First, Introduction

JMS, the Java Message Service Application interface, is an API for message-oriented middleware (MOM) in the Java platform for sending messages between two applications, or distributed systems, for asynchronous communication. The Java Messaging Service is a platform-agnostic API, and the vast majority of MOM providers support JMS.

JMS allows application components to create, send, receive, and read messages based on the Java EE platform. It makes the distributed communication less coupled, the message service more reliable and asynchronous.

Second, the Common terminology introduction

When it comes to JMS, we usually talk about some terms, which are explained as follows:

    1. Message middleware (JMS Provider): Refers to the provision of the JMS protocol of the third-party components, such as ACTIVEMQ is a message middleware, in addition to the more well-known KFA, Rabbit MQ and so on.
    2. Message patterns: Divided into point-to-point, and publish/subscribe (pub/sub), the corresponding data structures are queues (queue) and subject (TOPIC)
    3. Message: The carrier of the communication content, its structure mainly divides into the message header, the attribute and the message body, and divides into several kinds according to the storage structure difference, later will refer in detail.
    4. Message producer: The party that generated the message, in peer-mode, refers to the message sender (sender), which in p/s mode refers to the message publisher (publisher)
    5. Message consumer: The party receiving the message, corresponding to the two modes are the message receiver (receiver) and the message Subscriber (subscriber)

Three, the basic concept of JMS and the explanation of the principle

1. Basic Concepts
JMS is a Java messaging service that allows asynchronous message transmission between JMS clients through the JMS service.

2. Architecture

JMS consists of the following elements.

1.JMS Provider
An implementation of a JMS interface that connects to a message-oriented middleware. The provider can be a JMS implementation of the Java platform, or it can be an adapter for message-oriented middleware on a non-Java platform.

2.JMS Customer
A Java-based application or object that produces or consumes messages.

3.JMS producer
The JMS client that created and sent the message.

4.JMS Consumer
The JMS client that receives the message.

5.JMS message
An object that includes data that can be passed between JMS clients

6.JMS queues
An area that accommodates messages that are sent for waiting to be read. The queue implies that these messages will be sent in order. Once a message is read, the message is removed from the queue.

7.JMS Themes
A mechanism that supports sending messages to multiple subscribers.


3. JMS message model (that is, point-to-point and publish-subscribe models)

1, point-to-point (peer)

2, Publish/subscribe (pub/sub)


4, Peer
  1, Peer mode diagram effect
      
  2. The concepts involved

2.1. Message Queuing (queue)
2.2. Provider (Sender)
2.3. Consumer (Receiver)
2.4. Each message is sent to a specific queue where the recipient obtains the message from the queue. The queue retains messages until they are consumed or timed out.

  3, the characteristics of peer-to

3.1, each message only one consumer (Consumer) (that is, once consumed, the message is no longer in the message queue)
3.2. There is no dependency between the provider and the consumer in time, that is, when the provider sends the message, regardless of whether the consumer is running, it does not affect the message being sent to the queue
3.3, each message will only be sent to a consumer. There may be multiple consumers listening in a queue, but messages in each queue can only be consumed by one consumer in the queue.
3.4, the message exists in order. A queue transmits messages to the consumer in the order in which they are placed in the queue by the message server. When consumed, they are removed from the queue header (unless the message priority is used).
3.5. The consumer must answer the queue successfully after receiving the message successfully
PS: If you want to send every message should be successfully processed, then you need to peer-to mode.


5. Pub/sub (Publish/Subscribe mode)
  
  1. Pub/sub mode
  2. The concepts involved

2.1. Theme (Topic)
2.2. Publisher (publisher)
2.3, Subscribers (subscriber)

PS: The client sends the message to the topic. Multiple publishers send messages to topic, which the system passes to multiple subscribers.
  
  3. Pub/sub (Publish/Subscribe mode) features

3.1. Each message can have more than one consumer
3.2. There is a time dependence between the publisher and the Subscriber. Subscribers to a topic must create a subscriber before they can consume the publisher's message, and the Subscriber must remain running in order to consume the message.
3.3. To mitigate such strict time dependencies, JMS allows subscribers to create a persistent subscription. This way, even if the Subscriber is not activated (running), it can receive the message from the publisher.
3.4. Each message is routed to multiple message consumers called subscribers. Subscribers have many types, including persistent, non-persistent, and dynamic.
3.5. Publishers often don't know and realize that a Subscriber is receiving a topic message.
3.6. The message is pushed to the consumer, which means that the message is delivered to the consumer without a request.

PS: You can use the PUB/SUB model if you want to send messages that can be processed without being processed, handled by a single message, or can be processed by multiple consumers.


6, about the consumption of information
In JMS, the generation of messages and the message is asynchronous. For consumption, JMS messages can consume messages in two ways.
  

  6.1. Synchronization

A subscriber or consumer calls the Receive method to receive a message, and the receive method blocks until it is able to receive the message (or before it times out)

  6.2. Asynchronous

Subscribers or consumers can register as a message listener. When the message arrives, the system automatically calls the listener's OnMessage method.

7. JMS Application Interface

7.1, ConnectionFactory interface (Connection factory)
creates a factory for connection objects, depending on the message type, the user will use the Queue connection factory, or the subject connection factory,
do not have Q Ueueconnectionfactory and Topicconnectionfactory two species. You can find the ConnectionFactory object through Jndi.

7.2, Destination Interface (target)
Destination is a managed object that wraps the message target identifier, which is the location where the message is published and received, or whether it is a queue, or a subject. The
is the message producer's message to send the target or message to the consumer's source. For a message producer, its destination is a queue or a topic (TOPIC);
for the message consumer, its destination is also a queue or topic (that is, the source of the message).
So, destination is actually two types of objects: Queue, topic can find destination through Jndi.

  7.3. Connection Interface (connection)
Connection represents the link established between the client and the JMS system (the wrapper to the TCP/IP socket).
Connection can produce one or more sessions. Like ConnectionFactory, there are two types of connection: Queueconnection and Topicconnection.

  7.4. Session interface (Sessions)
The session is the interface where we manipulate the message. Represents a single thread context for sending and receiving messages.
Because the session is single-threaded, the message is sequential, meaning that the message is received one at a time in the order in which it was sent.
You can create producers, consumers, messages, etc. through the session. The session provides the functionality of the transaction. These send/Receive actions can be placed in a transaction when we need to send/receive multiple messages using the session.
Similarly, queuesession and topicsession are also divided.

  7.5. MessageProducer interface (producer of messages)
The message producer is created by the session and is used to send messages to destination. Consumers can synchronize (block mode), or asynchronous (non-blocking) messages that receive queues and subject types.
Similarly, there are two types of message producers: Queuesender and Topicpublisher. You can call the message producer's method (send or Publish method) to send the message.

  7.6. Messageconsumer interface (message consumer)
The message consumer is created by the session to receive messages sent to destination. Two types: QueueReceiver and TopicSubscriber.
Can be created by Createreceiver (Queue) or Createsubscriber (Topic) of the session, respectively.
Of course, you can also create a persistent subscriber by the Creatdurablesubscriber method of the session.

  7.7. Message interface (Messages)
Is the transfer of objects between consumers and producers, i.e. from one application to another. There are three main parts of a message:
1. Message header (required): Contains the action settings used to identify and find the route for the message.
2, a set of message properties (optional): Contains additional attributes that support compatibility with other providers and users. You can create custom fields and filters (message selectors).
3, a message body (optional): Allows users to create five types of messages (text messages, map messages, byte messages, stream messages, and object messages). The messaging interface is flexible and provides many ways to customize the content of the message.

The messaging interface is flexible and provides many ways to customize the content of the message.

  7.8, MessageListener
Message listeners. If a message listener is registered, the listener's OnMessage method is automatically invoked once the message arrives.
An MDB (Message-driven Bean) in an EJB is a messagelistener.

Iv. JMS Provider Implementation

To use the Java Messaging Service, you must have a JMS provider that manages sessions and queues. There are now both open source and proprietary providers.
Open source providers include: Apache ActiveMQ, Kafka, WebMethods, Ali's ROCKETMQ, etc.



Java Messaging Service JMS specification and rationale

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.