The JMS Getting Started tutorial

Source: Internet
Author: User
Tags message queue

Reprint: http://www.cnblogs.com/jjj250/archive/2012/08/08/2628552.html


Basic Articles

JMS is an application interface between application systems or components that communicates with each other, making it easy for us to achieve remote communication between different JVMs. RPC can do the same with remote communication, but RPC inevitably increases the degree of coupling between different systems, and JMS can greatly reduce the coupling between different application systems.

To learn about JMS, there are several concepts that need to be understood:

L Messaging (message notification, message communication)

A means of communicating with each other in an application system or component.

L Message (News)

The message is the carrier of the message communication, including message Headers, news properties, messages bodies

L JMS has two modes of message communication: Point-to-Point (Peer-to-peer) and Publish/subscriber (pub/sub)

Peer-to-peer mode is one-to-one, a message has only one receiver, by default the Peer-to-peer message is persistent, meaning that a message sent by the sender (sender) is sent to the message Queue (queue), It is not removed from the message queue until the message receiver (receiver) receives it, and the message that is not received will always exist in the JMS container. This approach is a bit like postal communications, with only one recipient of the letter, which is kept in the mailbox until it is received.

Pub/sub Way of workflow, first Subscriber (subscribers) to the JMS container subscription (Listen to) their interest in topic (subject), multiple subscribers can simultaneously subscribe to a topic, the message Publisher released a message, All subscribers who subscribe to this topic receive this message. By default, messages in the Pub/sub mode are not persistent, which means that once a message is sent, no one receives it, it is not saved, and subscribers can only receive messages from the publisher after they have subscribed. It's a bit like subscribing to a newspaper or magazine, a newspaper that can be subscribed to by more than one person, but subscribers can only receive journals issued by newspapers after they start subscribing.

L JMS (Java messaging Service)

is a technique in Java EE that defines a complete set of interfaces to implement message communication between different systems or applications. This means that our application (client), which is written for the JMS interface, can run under any container that implements the standard JMS interface, and our application and container implement the real decoupling, which is one of the benefits of interfacing programming. This is similar to JDBC programming.

L JMS Provider (JMS Provider)

JMS providers, also known as JMS servers or JMS containers, are providers of JMS services, and mainstream Java-EE containers typically provide JMS services (such as Jboss,bea WEBLOGIC,IBM websphere,oracle application Server, etc. supported)

L Connection Factory (Connection factories)

A connection factory is a factory used to create a JMS connection between a client and a JMS container, and there are two connection factories: (Queueconnectionfactory and Topicconnectionfactory), which are used to create queueconnection and Topicconnection's.

Context ctx = new InitialContext ();
Queueconnectionfactory queueconnectionfactory = 
(queueconnectionfactory) ctx.lookup ("QueueConnecti Onfactory ");
Topicconnectionfactory topicconnectionfactory = 
(topicconnectionfactory) ctx.lookup ("TopicConnecti Onfactory ");

L Destination (destinations)

The destination is the destination where the message producer (producer) messages are sent, and the source where the consumer (consumer) receives the message, it is a bit like a mailbox, the postman sends the letter to the mailbox, and the recipient takes the letter from the mailbox. To Peer-to-peer way, destination is queue, to pub/sub way, destination is topic. We want to get a reference to this destination, only through Jndi lookup (lookup), because the destination is registered with the JMS server (the later section will talk about how to register a destination).

Topic mytopic = (Topic) ctx.lookup ("Mytopic");
Queue myqueue = (queue) ctx.lookup ("Myqueue");

L Connection (Connection)

The connection referred to here refers to the connection between the client and the JMS provider (container). The connections are also divided into two types: Queueconnection and topicconnection, respectively corresponding to Peer-to-peer and pub/sub connections.

Queueconnection queueconnection = Queueconnectionfactory.createqueueconnection ();
Topicconnection topicconnection = Topicconnectionfactory.createtopicconnection ();

You must remember to close the connection when it is finished, otherwise the connection resources will not be released. Closing the connection automatically closes the session, the creator, and the consumer.

L Sessions (session)

A session is a single-threaded environment used to create a message producer and a message consumer that you can use to create message producers, consumers, messages, and to maintain message sniffing.

Topicsession topicsession = Topicconnection.createtopicsession (false, Session.auto_acknowledge);
Queuesession queuesession = Queueconnection.createqueuesession (true, 0);

L Messaging Producer (message producers)

The producer of the message, or the sender of the message, is Queuesenderin a peer-to-peer fashion, and it is topicpublisherin the Pub/sub way. It is an object created by the session to send messages to the destination.

Queuesender Queuesender = Queuesession.createsender (myqueue);
Topicpublisher Topicpublisher = Topicsession.createpublisher (mytopic);

Once you create a good producer, you can use it to send messages

Queuesender.send (message);
Topicpublisher.publish (message);

L Messaging Consumer (message Consumer)

The message consumer is the recipient or user of the message, which is queuereceiverin a peer-to-peer fashion and is topicsubscriberin pub/sub mode. This is an object that is created by the session to receive messages from the destination. The JMS container is responsible for delivering the message from the destination to the message consumer who has registered the destination.

QueueReceiver queuereceiver = Queuesession.createreceiver (myqueue);
TopicSubscriber TopicSubscriber = Topicsession.createsubscriber (mytopic);

Once you create a good news consumer, it is active, you can use it to receive messages, and you can use the close () method to invalidate it (Inactive). The consumer will not receive any messages until you invoke the connection start () method. Both receivers have a Receive method, which is a synchronized method, meaning that the program executes to this method and is blocked until the message is received.

Queueconnection.start ();
Message M = queuereceiver.receive ();
Topicconnection.start ();
Message m = topicsubscriber.receive (1000); Time out after a second

If we don't want it to be blocked, we need to receive the message asynchronously, and then we have to use the message Listener.

L Message Listener (Messages Listener)

A message listener is an object that acts as an asynchronous event handler for a message and implements the MessageListener interface, which has only one method OnMessage, in which you can define what to do when the message is received. You can use the Setmessagelistener method to register a listener for the message consumer.

MessageListener listener = new MessageListener ({
      public void OnMessage (message msg) {       //
      }
});
Topicsubscriber.setmessagelistener (listener); //

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.