(Original) receive and send jms ptp messages

Source: Internet
Author: User

To understand the message-driven bean of EJB, it is inevitable to study JMS first.

In RMI, the RMI technology allows two components distributed on different physical nodes to communicate directly using method calls, in addition to Cora and WebService, they can all implement roughly the same functions. However, in the face of Systems with increasing scale and complexity, these technologies have the following shortcomings:

1) synchronous communication: Synchronous communication means that after the client calls the Server Object method, the client program cannot go down before the server method returns.

2) lifecycle coupling between the client and server:The call code of the client and the called method of the server must be executed synchronously, and both sections of the Code must be executed normally. If the called method of the server is abnormal or the network communication is abnormal, the client will receive an exception.

3) point-to-point communication:The client can call only one service method at a time.

To solve the above problems, a message-oriented application architecture emerged, so JMS came out.

Here, we use Programming to Implement message transmission in the PTP model. Of course, either model is inseparable from the message server.

Because the server is based on the weblogic12c server, and the version of 12 has changed significantly compared with the previous version, to create a queue, you must first create a JMS module.

 

Step 1: Create persistent storage:

Click service> persistent storage> New> Select File storage.

Note: The Directory of the stored file must exist in advance and the server will not automatically create it.

Step 2: Create a JMS Server:

 

Click service-message transmission-JMS server-select new server

Note: In the last step, it is not necessary to configure the module because we need to manually configure it.

Step 3: Create the JMS module:

Click service-message transmission-JMS module-select new module

 

After creation, we can add a queue:

Step 4: Add a queue:

Click to enter the created module. Click create.

 

At this time, the entire server is set up, and the rest is to send and receive messages.

Send message:

Public class simplesendmessage {public void sendmessage () throws jmsexception, namingexception {// defines the JNDI final string connection_factory_jndi = "weblogic. JMS. connectionfactory "; // get cotext context CTX = getinitialcontext () required by the JNDI service; // obtain the connection factory connectionfactory connfactory = (connectionfactory) CTX through the JNDI search. lookup (connection_factory_jndi); // connection factory creates connection conn = connfactory. createconnection (); // obtain the destination DEST = (destination) CTX. lookup ("messagequeue"); // create a JMS session = Conn. createsession (false/* not a transactional session */, session. auto_acknowledge); // create a message producer messageproducer sender = session in the JMS session. createproducer (DEST); // sets the message transmission mode produced by the message producer. The effective time is sender. setdeliverymode (deliverymode. persistent); sender. settimetolive (20000); // creates a text message textmessage MSG = session through a JMS session. createtextmessage (); // sets the message content MSG. settext ("hello"); // send the message sender. send (MSG); // send MSG again. settext ("wellocome to JMS"); // closes the resource session. close (); Conn. close ();} private context getinitialcontext () {context CTX = NULL; properties props = new properties (); props. put (context. initial_context_factory, "weblogic. JNDI. wlinitialcontextfactory "); props. put (context. provider_url, "T3: // localhost: 7001"); try {CTX = new initialcontext (props);} catch (namingexception e) {e. printstacktrace ();} return CTX;} public static void main (string ARGs []) {simplesendmessage MP = new simplesendmessage (); try {MP. sendmessage ();} catch (jmsexception e) {e. printstacktrace ();} catch (namingexception e) {e. printstacktrace ();}}}

 

After the above Code is run, nothing can be seen, because nothing is printed on the console, but the actual message has been sent.

 

Below is a message receiving class:

Public class simplerecmessage {public void receivemessage () throws jmsexception, namingexception {// defines the default connection factory final string connection_factory_jndi = "weblogic. JMS. connectionfactory "; // get cotext context CTX = getinitialcontext () required by the JNDI service; // obtain the connection factory connectionfactory connfactory = (connectionfactory) CTX through the JNDI search. lookup (connection_factory_jndi); // obtain the Message destination DEST = (destination) CTX through the JNDI lookup. lookup ("messagequeue"); // connection factory creates connection conn = connfactory. createconnection (); // start the JMS connection and let him start to transmit the JMS message Conn. start (); // create a painting session with a JMS connection = Conn. createsession (false/* not a transactional session */, session. auto_acknowledge); // create a message consumer messageconsumer consumer ER = session in the JMS session. createconsumer (DEST); // synchronously receives a message. If no message is received, this method blocks the thread textmessage MSG = (textmessage) Consumer er. receive (); system. out. println (MSG); system. out. println ("synchronous received message:" + MSG. gettext (); // closes the resource session. close (); Conn. close ();} private context getinitialcontext () {context CTX = NULL; properties props = new properties (); props. put (context. initial_context_factory, "weblogic. JNDI. wlinitialcontextfactory "); props. put (context. provider_url, "T3: // localhost: 7001"); try {CTX = new initialcontext (props);} catch (namingexception e) {e. printstacktrace ();} return CTX;} public static void main (string ARGs []) {simplerecmessage S = new simplerecmessage (); try {S. receivemessage ();} catch (jmsexception e) {e. printstacktrace ();} catch (namingexception e) {e. printstacktrace ();}}}

 

Run the message sending class first, so that a message exists in the queue and then read the message using the receiving class. The running result is as follows:

 

 

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.