Java Message Queue-JMS overview, java queue-jms

Source: Internet
Author: User

Java Message Queue-JMS overview, java queue-jms
1. What is JMS?

Java Message Service (JMS) is a Java platform-oriented Message-oriented middleware (MOM) API. It is used between two applications, or send messages in a distributed system for asynchronous communication. Java Message Service is an API unrelated to a specific platform. Most MOM providers provide support for JMS (overview provided by Baidu encyclopedia ). We can simply understand that two applications need to communicate with each other. We use a JMS service for intermediate forwarding. Through the use of JMS, we can remove the coupling between the two applications.

 

2. Advantages of JMS

 

3. Message model of JMS

JMS has two communication modes:

1. Point-to-Point Messaging Domain (Point-to-Point)

2. Publish/Subscribe Messaging Domain (publishing/subscription Mode)

Before the emergence of the jms api, most products use either of the "point-to-point" or "Publish/subscribe" methods for message communication. JMS defines the specifications of the two message sending models, which are independent of each other. Any JMS provider can implement one or two models, which are their own choices. The JMS specification provides a common interface to ensure that the program we write based on the jms api is applicable to any model.

 

(1) Point-to-Point Messaging Domain (Point-to-Point communication model)

A. Pattern diagram:

      

 

 

B. Concepts involved:

In point-to-point communication mode, an application consists of a message queue, a sender, and a receiver. Each message is sent to a specific queue, and the receiver obtains the message from the queue. The queue retains messages until they are consumed or time-out.

 

C. features:

      • Each message only needs one consumer.
      • The sender and receiver have no time constraints in terms of time. That is to say, after the sender sends the message, no matter whether the receiver accepts the message or not, the sender will not be affected to send the message to the message queue.
      • Whether or not The sender is sending a message, the receiver can go to the message Queue (The receiver can fetch message whether it is running or not when the sender sends the message)
      • After receiving the message, the receiver must respond to the Message Queue successfully.

 

  

(2) Publish/Subscribe Messaging Domain (Publish/Subscribe communication model)

A. Pattern diagram:

        

      

B. Concepts involved:

In the publish/subscribe message model, the publisher publishes a message that is delivered to all clients through topics. In this mode, both the publisher and the subscriber are anonymous, that is, both the publisher and the subscriber do not know who the subscriber is. You can also dynamically publish and subscribe to topics. A Topic is used to save and transmit messages, and stores messages until the messages are delivered to the client.

 

 

C. features:

      • A message can be transmitted to multiple subscribers (that is, a message can have multiple receivers)
      • The publisher and subscriber have time constraints. For a subscriber of a Topic, a subscriber must be created before the subscriber can consume the message of the publisher, the subscriber must be in the running status.
      • To ease such strict time relevance, JMS allows subscribers to create a persistent subscription. In this way, even if the subscriber is not activated (running), it can receive messages from the publisher.

 

 

4. receive messages through JMS

In JMS, messages are generated asynchronously. For consumption, JMS message consumers can consume messages in two ways.

(1) Synchronous)

In synchronous consumption information mode, the subscriber/receiver receives messages by calling the receive () method. In the receive () method, the thread blocks until the message arrives or the message does not arrive after the specified time.

(2) Asynchronous)

To receive messages asynchronously, the message subscriber needs to register a message listener, which is similar to an event listener. As long as the message arrives, the JMS service provider will deliver the message by calling the onMessage () of the listener.

 

 

5. JMS Programming Model

    

 

 

(1) Connection Factories

Create a Connection object factory. For two different jms message models, QueueConnectionFactory and TopicConnectionFactory. You can use JNDI to find the ConnectionFactory object. The client uses a connection factory object to connect to the JMS service provider, which creates a connection between the JMS service provider and the client. The JMS client (such as the sender or receiver) searches for and obtains the connection in the JNDI namespace. With this connection, the client can communicate with the destination and send/receive messages to the queue or topic.

      

QueueConnectionFactory queueConnFactory = (QueueConnectionFactory) initialCtx.lookup ("primaryQCF");Queue purchaseQueue = (Queue) initialCtx.lookup ("Purchase_Queue");Queue returnQueue = (Queue) initialCtx.lookup ("Return_Queue");

(2) Destination

The destination indicates the destination of the message to be sent and the source of the message received by the client. JMS uses two destinations: queue and topic. The following code specifies a queue and topic:

Create a queue Session:

QueueSession ses = con.createQueueSession (false, Session.AUTO_ACKNOWLEDGE);  //get the Queue object  Queue t = (Queue) ctx.lookup ("myQueue");  //create QueueReceiver  QueueReceiver receiver = ses.createReceiver(t); 

Create a Topic Session:

QueueSession ses = con.createQueueSession (false, Session.AUTO_ACKNOWLEDGE);  //get the Queue object  Queue t = (Queue) ctx.lookup ("myQueue");  //create QueueReceiver  QueueReceiver receiver = ses.createReceiver(t); 

 

 

(3) Connection

Connection indicates the link established between the client and the JMS System (packaging the TCP/IP socket ). Connection can generate one or more sessions. Like ConnectionFactory, there are two types of Connection: QueueConnection and TopicConnection.

The connection object encapsulates the virtual connection with the JMS provider. If we have a ConnectionFactory object, we can use it to create a connection.

      

Connection connection = connectionFactory.createConnection();

 

 

(4) Session

Session is an interface for message operations. You can use session to create producers, consumers, and messages. The Session provides the transaction function. If you need to use the session to send/receive multiple messages, you can put these send/receive actions into one transaction.

We can create a session after the connection is created:

Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

Two parameters are provided. The first parameter indicates whether transactions are supported, and the second parameter indicates the transaction type.

 

(5), Producter

The message producer is created by the Session to send messages to the destination. The producer implements the MessageProducer interface. We can create producers for the destination, queue, or topic;

      

MessageProducer producer = session.createProducer(dest);MessageProducer producer = session.createProducer(queue);MessageProducer producer = session.createProducer(topic);

    

(6) Consumer

A message consumer is created by a Session to receive messages sent to Destination.

 

MessageConsumer consumer = session.createConsumer(dest);MessageConsumer consumer = session.createConsumer(queue);MessageConsumer consumer = session.createConsumer(topic);

    

(7) MessageListener

Message listener. If a message listener is registered, the onMessage method of the listener is automatically called once the message arrives. Message-Driven Bean in EJB is a MessageListener.

 

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.