The JMS (JAVA message Service,java messaging) API is a standard or specification of a messaging service that allows application components to create, send, receive, and read messages based on the Java EE platform. It makes distributed communication less coupled, and messaging services more reliable and asynchronous.
This blog post we mainly introduce an important specification JMS in Java, because the application of this specification in the enterprise is very extensive, but also more important, we mainly introduce the basic concept of JMS and its model, the consumption of messages and JMS programming steps. I. Basic Concepts
JMS is a Java messaging service that enables asynchronous message transmission between JMS clients through the JMS service. second, the message model
1.0point-to-point (Peer-to-peer)
2.0publish/subscribe (Pub/sub)
Point to Point and publish subscription model third, Peer-to-peer
1. Peer-to-peer mode diagram
2. The concepts involved
(1) Message Queuing (queue)
(2) Sender (Sender)
(3) Recipient (Receiver)
(4) Each message is sent to a specific queue, and the receiver obtains the message from the queue. Queues keep messages until they are consumed or timed out.
3. The characteristics of Peer-to-peer
(1) Each message has only one consumer (Consumer) (that is, once consumed, the message is no longer in the message queue)
(2) There is no time dependency between the sender and the receiver, meaning that when the sender sends the message, it does not affect the message being sent to the queue, regardless of whether the receiver is running or not.
(3) The receiver must reply to the queue successfully after receiving the message successfully, if you want to send every message should be successfully processed, then you need Peer-to-peer mode. Four, Pub/sub
1, Pub/sub mode diagram
2, the concept involved
(1) Theme (Topic)
(2) Publisher (publisher)
(3) Subscriber (subscriber)
The client sends the message to the subject. Multiple publishers send messages to topic, which the system passes to multiple subscribers.
3, the characteristics of pub/sub
(1) Each message can have multiple consumers
(2) There is a time dependency between the Publisher and the Subscriber. For a subscriber to a topic (Topic), it must create a subscriber before it can consume the publisher's message, and the Subscriber must remain in the running state in order to consume the message.
(3) to mitigate such stringent time dependencies, JMS allows subscribers to create a sustainable subscription. In this way, even if the Subscriber is not activated (running), it can also receive a message from the publisher. If you want to send a message that can be done without any processing, or be handled by a message, or can be handled by multiple consumers, then you can use the Pub/sub model v. Message consumption
In JMS, the message is generated and the message is asynchronous. For consumption, JMS message users can consume messages in two ways.
0 Sync
The Subscriber or recipient invokes the Receive method to receive the message, and the receive method blocks until it can receive the message (or before the timeout)
0 asynchronous
Subscribers or receivers can be registered as a message listener. When the message arrives, the system automatically invokes the listener's OnMessage method. six, JMS programming model
1, ConnectionFactory
The factory that created the connection object, for two different JMS message models, has Queueconnectionfactory and topicconnectionfactory two species respectively. You can find ConnectionFactory objects through Jndi.
2, Destination
Destination means the message producer's message sends the target or the message source of the consumer. For a message producer, its destination is a queue or a topic (Topic), and for a message consumer, its destination is also a queue or topic (i.e., a message source).
So, destination is actually two types of objects: Queue, topic can find destination through Jndi.
3, Connection
Connection represents the link established between the client and the JMS system (the wrapper over the TCP/IP socket). Connection can produce one or more sessions. Like ConnectionFactory, there are two types of connection: Queueconnection and Topicconnection.
4, session
The session is our interface for manipulating messages. You can create producers, consumers, messages, and so on by session. The session provides the functionality of the transaction. When we need to send/receive multiple messages using the session, these send/receive actions can be placed in a transaction. Likewise, queuesession and topicsession are also divided.
5, the producers of information
The message producer is created by session and is used to send messages to destination. Similarly, message producers are divided into two types: Queuesender and Topicpublisher. You can invoke the message producer's method (send or Publish method) to send a message.
6. News Consumer
Message consumers are created by the session to receive messages sent to destination. Two types: QueueReceiver and TopicSubscriber. Can be created individually through the session's Createreceiver (Queue) or Createsubscriber (Topic). Of course, you can also create a persistent subscriber by the Creatdurablesubscriber method of the session.
7, MessageListener
The message listener. 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. VII. Benefits of enterprise messaging systems
Let's take a look at the figure below, where application A sends the message to the server, and then application B receives the messages from a server, which we'll use to analyze the benefits of JMS:
1. Provide message flexibility
2. Loosely coupled
3. Asynchronous
So much for the basic concepts of JMS, the next blog post introduces a JMS implementation.
Reproduced from: http://blog.csdn.net/jiuqiyuliang/article/details/46701559