Java Message Queuing--JMS overview

Source: Internet
Author: User
Tags sessions

1. What is JMS

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, with the vast majority of MOM providers providing support for JMS (an overview given by Baidu Encyclopedia). We can simply understand: two applications need to communicate, we use a JMS service, intermediate forwarding, through the use of JMS, we can release two programs between the coupling.

2. Advantages of JMS
    1. Asynchronous (asynchronous)

      JMS is asynchronous by default. So-to-receive a message, the client is not required to send the request. The message will arrive automatically to the client as they become available. (JMS is inherently an asynchronous messaging service, and when clients get messages, they don't have to send unsolicited requests, and the messages automatically Send to Available clients)

    2. Reliable (reliable)

      JMS provides the facility of assurance that the message would delivered once and only once. You know that duplicate messages create problems. JMS helps you avoiding such problems. (JMS guarantee messages are delivered only once.) Everyone has encountered a duplicate creation message problem, and JMS can help you avoid the problem. )

3. JMS Message Model

JMS has two modes of communication:

1, point-to-point Messaging Domain (Point-to-point)

2, Publish/subscribe Messaging domaimrencai888.cnn (Publish/Subscribe mode)

Before the JMS API appeared, most products used a "point-to-point" and "Publish/subscribe" In any way for message communication. JMS defines the specifications for these two message-sending models, which are independent of each other. Any JMS provider can implement one or both of these models, which is their own choice. The JMS specification provides a common interface to ensure that the programs we write based on the JMS API are suitable for any model.

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

A, Mode diagram:

      

b, the 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 recipient obtains the message from the queue. The queue retains messages until they are consumed or timed out.

C, Characteristics:

        • Every message as long as a consumer
        • The sender and receiver have no time constraint on time, that is, the sender does not affect the sender sending the message to the message queue, regardless of whether the recipient has received the message after sending the message.
        • Regardless of whether the sender is sending a message, the receiving party can go to the message from the message queue (the receiver could fetch messages whether it is running or not when the sender sends the MESSAG E
        • The receiver needs to answer the message queue successfully after receiving the message

  

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

A, Mode diagram:

        

      

b, the concepts involved:

In the Publish/Subscribe message model, the Publisher publishes a message that is passed through topic to all clients. In this mode, both the Publisher and the Subscriber are anonymous, that is, neither the publisher nor the Subscriber knows who the other person is. and can dynamically publish and subscribe topic. Topic is primarily used to save and deliver messages, and the message is persisted until the message is passed to the client.

C, Characteristics:

        • A message can pass multiple subscribers (that is, a message can have more than one recipient)
        • Publishers and Subscribers have time constraints, and for a topic (Topic) Subscribers, it must create a subscriber to consume the publisher's message, and in order to consume the message, the Subscriber must remain in a running state.
        • To mitigate such strict time dependencies, JMS allows subscribers to create a durable subscription. This way, even if the Subscriber is not activated (running), it can receive the message from the publisher.

4. JMS receives messages

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

(1), Synchronization (synchronous)

In the synchronous consumption information mode mode, the Subscriber/receiver receives the message 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 (asynchronous)

When a message is received asynchronously, the message subscriber registers a message listener, similar to an event listener, that, as long as the message arrives, the JMS service provider will deliver the message by invoking the listener's OnMessage ().

5. JMS Programming Model
    1. Management Objects (administered objects)-Connection factory (Connection factories) and destination (Destination)
    2. Connection object (Connections)
    3. Session (Sessions)
    4. Messaging Producer (message producers)
    5. Messaging consumer (message consumers)
    6. Messages Listener (Message Listeners)

    

(1), Connection factories

The factory that created the connection object has two queueconnectionfactory and topicconnectionfactory for two different JMS message models. You can find the ConnectionFactory object through Jndi. The client connects to the JMS service provider using a connection factory object, which creates a connection between the JMS service provider and the client. The JMS client, such as the sender or recipient, searches for and obtains the connection in the Jndi namespace. With this connection, the client communicates with the destination, sending/receiving messages to the queue or topic.

      

Queueconnectionfactory queueconnfactory = (queueconnectionfactory) initialctx.lookup ("PrimaryQCF"); Queue Purchasequeue = (queue) initialctx.lookup ("Purchase_queue" chuangshi88.cn); Queue Returnqueue = (queue) initialctx.lookup ("Return_queue");

(2), Destination

Destination indicates the destination at which the message was sent and the source from which the client received the message. JMS uses two destinations, queues, and topics. 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  

Create a topic Session:

Queuesession ses = con.createqueuesession (false, Session.auto_acknowledge);  Get the Queue object  Queue T = (Queue) ctx.lookup ("Myqueue");  /fanku1.com/create QueueReceiver  

(3), 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.

The connection object encapsulates the virtual connection to the JMS provider, and if we have a ConnectionFactory object, you can use it to create a connection.

      

Connection Connection = Connectionfactory.createconnection ();

(4), Session

The session is our interface for manipulating messages, and we can create producers, consumers, messages, etc. through the session. The session provides the functionality of the transaction, which can be placed in a transaction if you need to send/receive multiple messages using the session.

We can create a session after connection creation is complete:

Session session = Connection.createsession (false, Session.auto_acknowledge);

This provides the parameter two parameters, the first parameter is whether the transaction is supported, the second is the type of the transaction

(5), Producter

The message producer is created by the session and is used to send messages toward the destination. The producer realizes the MessageProducer interface, we can create the producer for the destination, the queue or the topic;

      

MessageProducer producer = Session.createproducer (dest); MessageProducer producer = Session.createproducer (queue); MessageProducer producer = session.createproducer (topic);

    

(6), Consumer

The message consumer is created by the 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 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.

Java Message Queuing--JMS overview

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.