Introduction to JMS (II)-simple example of JMS

Source: Internet
Author: User

1.The previous section briefly introduced the basic concepts of JMS. This section uses an example to help you understand the basic concepts of the previous section. the first thing to do is to select a JMS provider. You do not need to consider this in the javaee environment. we chooseActivemq, Official address: http://activemq.apache.org /.
There are many documents about activemq on the Internet, so I will not introduce them here.
2.Follow the diagram described above:
Connectionfactory ----> connection ---> session ---> message
Destination + session ------------------------------------> producer
Destination + session ------------------------------------> messageconsumer
A) first, you must obtain connectionfactoy and destination. Create a one-to-one queue as destination.
Connectionfactory factory = new activemqconnectionfactory ("VM: // localhost ");
Queue queue = new activemqqueue ("testqueue ");
B) then create a connection in connectionfactory and then start the connection:
Connection connection = factory. createconnection ();
Connection. Start ();
C) Create a session by connection:
Session session = connection. createsession (false, session. auto_acknowledge)
The parameter description is not required for the time being. It will be detailed later.
D) You can create a message. Here, a textmessage is created.
Message message = session. createtextmessage ("Hello JMS! ");
E) To send the created message, you need to create a message producer by session and destination:
Messageproducer producer = session. createproducer (Queue );
F) The created message can be sent as follows:
Producer. Send (Message );
G) after the message is sent, we need to create a consumer to receive the message:
Messageconsumer comsumer = session. createconsumer (Queue );
Message recvmessage = comsumer. Receive ();
H) after receiving the message, the message consumer can obtain its content:

System. Out. println (textmessage) recvmessage). gettext ());

3.So far, a simple JMS example is complete. The following are all source code:

/*** @ Author administrator * @ desctiption combined with an example to gain an in-depth understanding of the basic concepts of JMS * the consumer of a message can receive messages in two ways: * 1. consumer. receive () or consumer. receive (INT timeout); * 2. Register a messagelistener. * In the first method, the Message Receiver waits until a message arrives or times out. * The latter method registers a listener. When a message arrives, it calls back its onmessage () method. */Package COM. WL. JMS; import javax. JMS. connection; import javax. JMS. connectionfactory; import javax. JMS. jmsexception; import javax. JMS. message; import javax. JMS. messageconsumer; import javax. JMS. messagelistener; import javax. JMS. messageproducer; import javax. JMS. queue; import javax. JMS. session; import javax. JMS. textmessage; import Org. apache. activemq. activemqconnectionfactory; import Org. apache. activemq. comma Nd. activemqqueue; public class messagesendandreceive {/*** @ Param ARGs * @ throws jmsexception */public static void main (string [] ARGs) throws jmsexception {// todo auto-generated method stubconnectionfactory factory = new activemqconnectionfactory ("VM: // localhost"); connection = factory. createconnection (); connection. start (); // create the message's destination queue = new activemqqueue ("testqueue "); Final Session session = connection. createsession (false, session. auto_acknowledge); // create the message to be sent = session. createtextmessage (" Hello JMS! "); // Create a message producer to send a message messageproducer producer = session. createproducer (Queue); producer. Send (Message); system. Out. println (" Send message completed! "); // Create the Message Receiver messageconsumer comsumer = session. createconsumer (Queue); // The first method for the consumer of the message to receive the message: consumer. receive () or consumer. receive (INT timeout); // message recvmessage = comsumer. receive (); // system. out. println (textmessage) recvmessage ). gettext (); // The second method for the consumer of the message to receive the message: register a messagelistener comsumer. setmessagelistener (New messagelistener () {public void onmessage (Message MSG) {// todo auto-generated method stubtextmessage textmsg = (textmessage) MSG; try {system. out. println (textmsg. gettext ();} catch (jmsexception e) {e. printstacktrace ();}}});}}

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.