JMS details ~

Source: Internet
Author: User
Java Message Service (JMS) detailed explanation time: Source: Author: Click: 49 times
This article introduces the Message Service JMS in Java. Java Message Service provides the point-to-point mode and the publish-subscribe mode. These two services are described in detail in this article.
This article introduces the Message Service JMS in Java. Java Message Service helps the point-to-point mode and the publish-subscribe mode. The two services are described in detail in this article.

Java Message Service (JMS Java Message Services) helps point-to-point queue and publish-subscribe topics ).

Queue only allows one message to be sent to one customer (one-to-one ):

There is no time dependency between the receiver and the sender of Java Message Service JMS. No matter whether the receiver is running when the sender sends the message, the receiver has no score to extract the message. The recipient provides a receipt for messages that are successfully processed.

Topics has multiple clients (one-to-multiple, many-to-many) with no score ):

The client program that subscribes to a topic can only receive messages published after it subscribes. To receive messages, the subscriber must stay in the party state. Therefore, there is a time-dependent relationship between the publisher and the subscriber.

Point-to-Point message mode occurs after a queue. The producer of the message writes the message to the queue, and the subscriber of the message extracts the message from the queue. The publish-subscribe message mode goes through a hierarchical framework composed of a topic node. The message producer publishes a message to the topic framework, and the message subscriber subscribes to the topic framework.

The message-driven bean has only one bean class. In some respects, the JMS message-driven bean is similar to the stateless conversation Bean: the message-driven bean does not retain data or conversation status for specific customers.

@ Messagedriven (activationconfig = {
@ Activationconfigproperty (propertyname = "destinationtype", propertyvalue = "javax. JMS. queue "),
@ Activationconfigproperty (propertyname = "destination", propertyvalue = "queue/JMS ")
})
@ Messagedriven the comment indicates that this is a message-driven bean, and applies the @ activationconfigproperty annotation to configure various attributes of the message. The destinationtype attribute specifies the message category. The message has two categories: topics and queues, the following describes the two types of messages:

Topics does not have multiple clients. Topic publishing allows one-to-multiple or multiple-to-multiple communication channels. The publisher of a message is called publisit, and the receiver of Java Message Service is called subscriber. Destinationtype attribute corresponds to value: javax. JMS. Topic

Queue only allows one message to be sent to a customer. A sender puts a message into the message queue. The receiver extracts and obtains the message from the queue, and the message disappears in the queue. After the first receiver extracts and obtains the message, more people can no longer obtain the message. Destinationtype attribute corresponds to value: javax. JMS. the queue destination attribute is used to specify the message path. When a message-driven bean is published, if the path does not exist, the container automatically creates the path. when the container is closed, the path is automatically deleted.

When a message arrives in the queue/JMS queue, the onmessage trick is triggered and the message is passed in as a parameter.

Package com. julycn. JMS;

Import javax. EJB. activationconfigproperty;
Import javax. EJB. messagedriven;
Import javax. JMS. jmsexception;
Import javax. JMS. message;
Import javax. JMS. messagelistener;
Import javax. JMS. textmessage;

@ Messagedriven (activationconfig = {
@ Activationconfigproperty (propertyname = "destinationtype", propertyvalue = "javax. JMS. queue "),
@ Activationconfigproperty (propertyname = "destination", propertyvalue = "queue/JMS ")})
Public class messagequeue implements messagelistener {

Public messagequeue (){

}

Public void onmessage (message ){
Textmessage tmsg = (textmessage) message;
Try {
System. Out. println (tmsg. gettext ());
} Catch (jmsexception e ){
E. printstacktrace ();
}
}

}
Package com. julycn. client;

Import javax. JMS. jmsexception;
Import javax. JMS. Queue;
Import javax. JMS. queueconnection;
Import javax. JMS. queueconnectionfactory;
Import javax. JMS. queuesender;
Import javax. JMS. queuesession;
Import javax. JMS. textmessage;
Import javax. Naming. initialcontext;
Import javax. Naming. namingexception;

Public class messagequeueclient {

Public static void main (string [] ARGs ){
Queueconnection conn;
Queuesession;
Queue queue;
Queuesender sender;
Textmessage MSG;

Try {
Initialcontext CTX = new initialcontext ();
Queueconnectionfactory qcf = (queueconnectionfactory) CTX
. Seeup ("connectionfactory ");
Conn = qcf. createqueueconnection ();
Session = conn. createqueuesession (false,
Queuesession. auto_acknowledge );
Queue = (Queue) CTX. seeup ("queue/JMS ");
MSG = session. createtextmessage ("hello, I haven't seen you for a long time! ");
Sender = session. createsender (Queue );
Sender. Send (MSG );
Sender. Close ();
} Catch (namingexception e ){
E. printstacktrace ();
} Catch (jmsexception e ){
E. printstacktrace ();
}

}

}
(1) Get a JNDI initialization context (context );

Example source code:

Properties props = new properties ();
Props. setproperty ("Java. Naming. Factory. Initial", "org. jnp. inte programming CES. namingcontextfactory ");
Props. setproperty ("Java. Naming. provider. url", "www.verybc.com: 1099 ");
Props. setproperty ("Java. Naming. Factory. url. pkgs", "org. JBoss. Naming ");
Initialcontext CTX = new initialcontext (props );

Note: No score is written in the source code or in the JNDI. properties document.

(2) Search for a connection factory topicconnectfactory/queueconnectionfactory Based on the context (there are two connection factories, and the corresponding categories are applied based on the topic/Queue );

Example source code:

Queueconnectionfactory qcf = (queueconnectionfactory) CTX. seeup ("connectionfactory ");

(3) Get a connection from the connection Factory (connect has two [topicconnection/queueconnection]);

Source code of the example: conn = qcf. createqueueconnection ();

(4) Establish a session through a connection );

Source code of the example: Session = conn. createqueuesession (false, queuesession. auto_acknowledge );

The source code indicates that a dialog is created that does not require transactions and can automatically receive Java Message Service receipts. In non-transaction sessions, there are three methods for passing JMS messages:
Session. auto_acknowledge: when the client calls the receive trick to return a victory, or when messagelistenser successfully processes the message, the session will automatically receive the message receipt.

Session. client_acknowledge: the client receives messages by calling the message's acknowingledge trick. Received in addition to the Session Layer. When a consumed message is received, all messages that have been consumed by the session are automatically received. For example, if the consumer consumes 10 messages and receives 15 messages, the receipt of the first 10 messages will be received in the 15 messages.

Session. dups_acknowledge: indicates that the session receives messages slowly.

(5) Search for the destination (topic/Queue );

Source code of the example: queue = (Queue) CTX. seeup ("queue/JMS ");

(6) create a message producer (topicpublisit/queuesender) and a consumer (topicsubscriber/queuereceiver) based on the conversation and destination ).
Example source code:

MSG = session. createtextmessage ("hello, I haven't seen you for a long time! ");
Sender = session. createsender (Queue );
Sender. Send (MSG );

NOTE: If javax. Naming. namenotfoundexception: JMS bound occurs during the runtime, JBoss does not create a queue object by itself. Therefore, you need to manually configure the queue object. No score /Server/default/deploy list to create a programming-service. XML document, where the programming did not get any value, but must be with the "-service" suffix, for example, abc-service.xml. This document has no score on deploy or its sublist (Multi-Level sublist. The content of this document is as follows:


JBoss. MQ: service = destinationmanager The title in the element's title attribute value must be JMS, which must be consistent with the/section following queue/JMS.

[Very recommended for programming]

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.