Java Message Queuing activemq (i)--JMS basic concepts

Source: Internet
Author: User

Summary: The Java Message Service (JMS) API is a messaging standard, allows application components based on the Java platfor M Enterprise Edition (Java EE) to create, send, receive, and read messages. IT enables distributed communication is loosely coupled, reliable, and asynchronous.

The JMS (JAVA message Service,java message Service) API is a standard or specification for a messaging service that allows application components to create, send, receive, and read messages based on the Java EE platform. It enables less coupling of distributed communications, more reliable messaging services, and asynchronous

1. Basic Concepts

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

2, the message mode point-to-point (peer)
      1. Peer mode diagram

      2. The concepts involved
        1. Message Queuing (queue)
        2. Sender (Sender)
        3. Receiver (receiver)
        4. 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.
      3. The characteristics of peer-to

        1. Only one consumer per message (Consumer) (that is, once consumed, the message is no longer in the message queue)
        2. There is no dependency on time between the sender and the receiver, that is, when the sender sends the message, regardless of whether the recipient is running, it does not affect the message being sent to the queue
        3. The recipient needs to answer the queue successfully after receiving the message successfully

        If you want to send every message should be processed successfully, then you need to peer mode.

Pub/sub
      1. Pub/sub mode diagram

      2. The concepts involved
        1. Theme (Topic)
        2. Publisher (publisher)
        3. Subscribers (subscriber)
          The client sends the message to the topic. Multiple publishers send messages to topic, which the system passes to multiple subscribers.
      3. Features of Pub/sub

        1. Each message can have multiple consumers
        2. There is a time dependency between the Publisher and the Subscriber. Subscribers to a topic (TOPIC) must create a subscriber before they 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 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.

        If you want to send a message that can be processed without being processed, or handled by a single message, or can be processed by multiple consumers, then you can use the PUB/SUB model

3, the consumption of the message

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

Synchronization: A subscriber or receiver 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).

Async: A Subscriber or receiver can register as a message listener. When the message arrives, the system automatically calls the listener's OnMessage method.

4. JMS programming model in JMS, the generation of messages and the message is asynchronous. For consumption, JMS messages can consume messages in two ways.
A subscriber or receiver 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)

1, ConnectionFactory

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.

2, Destination

Destination means that the message producer's message is sent to the target or to the source of the message consumer. For a message producer, its destination is a queue or a topic (Topic), and 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.

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.

4. Session

The session is the interface where we manipulate the message. 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.

5. Producers of messages

The message producer is created by the session and is used to send messages to destination. 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.

6. Message Consumers

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, 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.

Examples are as follows:

ConnectionFactory ConnectionFactory;//Connection FactoryConnection Connection =NULL;//ConnectionSession session;//session, the thread that accepts or sends the messageDestination Destination;//Destination of messageMessageconsumer Messageconsumer;//the consumer of the message//instantiating a connection factoryConnectionFactory =Newactivemqconnectionfactory (USERNAME, PASSWORD, Brokerurl); Try {            //getting connections from a connection factoryConnection =connectionfactory.createconnection (); //Start ConnectionConnection.start (); //Create sessionSession=connection.createsession (false, Session.auto_acknowledge); //Create a message queue that connects HelloWorldDestination=session.createqueue ("Mqtest"); //Create a consumer modelMessageconsumer =Session.createconsumer (destination);  while(true) {TextMessage TextMessage= (TextMessage) messageconsumer.receive (100000); if(textmessage!=NULL) {System.out.println ("Received message:" +Textmessage.gettext ()); }            }                    } Catch(JMSException ex) {ex.printstacktrace (); }
5. Benefits of the Enterprise messaging system

For example, application A sends a message to the server, and then application B receives a messages from the server, and through this diagram we analyze the benefits of JMS:

    1. Provides message flexibility
    2. Loosely coupled
    3. of Asynchrony

Java Message Queuing activemq (i)--JMS basic concepts

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.