A small example of JMS

Source: Internet
Author: User
JMS is a message service provided by. It can accept the messages sent by the message provider and forward the messages to the Message consumer ).
2. JMS provides two types of Message Service: (1) queue, that is, point-to-point. Each message is forwarded to only one message consumer. (2) topic: publish and subscribe. Each message can be forwarded to all subscribers (consumers ).
3. JMS configuration under WebLogic 8:
(1) configure the JMS connection Factory
(2) configure the JMS file store. (currently, all documents are configured with file store. In fact, in specific applications, the jms jdbc store may be more extensive, but no information is found)
(3) configure the JMS Server
(4) configure the JMS queue or JMS topic in destinations of the JMS Server
The JNDI of the JMS connection factory and the JNDI of the JMS queue or JMS topic are provided to message consumers and consumers.
4. Steps for a message producer to send a message to JMS:
(1) Use JNDI to query objects JMS connectionfactory and destination (JMS queue/topic)
(2) Use the management object JMS connectionfactory to establish a connection
(3) Use connection to establish a session
(4) use the session and management object destination to create the messagesender for the message producer.
(5) Use messagesender to send messages
Example of a message sender:
Java code
Package myjms;

Import java. util .*;
Import javax. Naming .*;
Import javax. JMS .*;

Public class messageproducter {
Public static void main (string [] ARGs ){
String queueconnectionfactoryname = "myjmsconnectionfactory"; // JNDI of the JMS connection Factory
String queuename = "myjmsqueue"; // The JNDI of the JMS queue or JMS topic

Boolean transacted = false; // transaction mode
Int acknowledgementmode = session. auto_acknowledge; // acknowledgement Mode
String message = "message need to send"; // simulate the message to be sent

Properties Properties = new properties ();
Properties. Put (context. initial_context_factory, "weblogic. JNDI. wlinitialcontextfactory ");
Properties. Put (context. provider_url, "T3: // localhost: 7001 ");

Try {
Context context = new initialcontext (properties );
Object OBJ = context. Lookup (queueconnectionfactoryname );
Queueconnectionfactory = (queueconnectionfactory) OBJ; // obtain the JMS connection Factory

OBJ = context. Lookup (queuename );
Queue queue = (Queue) OBJ; // obtain the JMS queue or JMS topic

Queueconnection = queueconnectionfactory. createqueueconnection (); // generate a connection
Queueconnection. Start ();
Queuesession = queueconnection. createqueuesession (transacted, acknowledgementmode );
Textmessage = queuesession. createtextmessage ();
Textmessage. clearbody ();
Textmessage. settext (Message );
Queuesender = queuesession. createsender (Queue );
Queuesender. Send (textmessage );
If (transacted ){
Queuesession. Commit ();
}

If (queuesender! = NULL ){
Queuesender. Close ();
}
If (queuesession! = NULL ){
Queuesession. Close ();
}
If (queueconnection! = NULL ){
Queueconnection. Close ();
}

}
Catch (exception ex ){
Ex. printstacktrace ();
}
}
}
Package myjms;

Import java. util .*;
Import javax. Naming .*;
Import javax. JMS .*;

Public class messageproducter {
Public static void main (string [] ARGs ){
String queueconnectionfactoryname = "myjmsconnectionfactory"; // JNDI of the JMS connection Factory
String queuename = "myjmsqueue"; // The JNDI of the JMS queue or JMS topic

Boolean transacted = false; // transaction mode
Int acknowledgementmode = session. auto_acknowledge; // acknowledgement Mode
String message = "message need to send"; // simulate the message to be sent

Properties Properties = new properties ();
Properties. Put (context. initial_context_factory, "weblogic. JNDI. wlinitialcontextfactory ");
Properties. Put (context. provider_url, "T3: // localhost: 7001 ");

Try {
Context context = new initialcontext (properties );
Object OBJ = context. Lookup (queueconnectionfactoryname );
Queueconnectionfactory = (queueconnectionfactory) OBJ; // obtain the JMS connection Factory

OBJ = context. Lookup (queuename );
Queue queue = (Queue) OBJ; // obtain the JMS queue or JMS topic

Queueconnection = queueconnectionfactory. createqueueconnection (); // generate a connection
Queueconnection. Start ();
Queuesession = queueconnection. createqueuesession (transacted, acknowledgementmode );
Textmessage = queuesession. createtextmessage ();
Textmessage. clearbody ();
Textmessage. settext (Message );
Queuesender = queuesession. createsender (Queue );
Queuesender. Send (textmessage );
If (transacted ){
Queuesession. Commit ();
}

If (queuesender! = NULL ){
Queuesender. Close ();
}
If (queuesession! = NULL ){
Queuesession. Close ();
}
If (queueconnection! = NULL ){
Queueconnection. Close ();
}

}
Catch (exception ex ){
Ex. printstacktrace ();
}
}
}

5. Steps for a message consumer to receive messages from JMS:
(1) Use JNDI to query objects JMS connectionfactory and destination (JMS queue/topic)
(2) Use the management object JMS connectionfactory to establish a connection
(3) Use connection to establish a session
(4) create a message consumer messagereceiver using the session and the management object destination
(5) When the message consumer messagereceiver is used to receive the message, use setmessagelistener to bind the messagelistener interface to messagereceiver.
The message consumer must implement the messagelistener interface and define the onmessage event method.
Example of a message consumer:

Java code
Package myjms;

Import java. util .*;
Import javax. Naming .*;
Import javax. JMS .*;

Public class messagereciever
Implements messagelistener {
Public void onmessage (message ){
If (Message instanceof textmessage ){
Textmessage = (textmessage) message;
Try {
System. Out. println ("message content is:" + textmessage. gettext ());
}
Catch (jmsexception e ){
E. printstacktrace ();
}
}
}

Public static void main (string [] ARGs ){

Messagereciever msgrcvr = new messagereciever ();
String queueconnectionfactoryname = "myjmsconnectionfactory ";
String queuename = "myjmsqueue ";

Boolean transacted = false;
Int acknowledgementmode = session. auto_acknowledge;

Properties Properties = new properties ();
Properties. Put (context. initial_context_factory,
"Weblogic. JNDI. wlinitialcontextfactory ");
Properties. Put (context. provider_url, "T3: // localhost: 7001 ");

Try {
Context context = new initialcontext (properties );
Object OBJ = context. Lookup (queueconnectionfactoryname );
Queueconnectionfactory = (queueconnectionfactory)
OBJ;

OBJ = context. Lookup (queuename );
Queue queue = (Queue) OBJ;

Queueconnection = queueconnectionfactory.
Createqueueconnection ();
Queueconnection. Start ();
Queuesession = queueconnection. createqueuesession (transacted,
Acknowledgementmode );
Queuereceiver = queuesession. createreceiver (Queue );

Queuereceiver. setmessagelistener (msgrcvr );

Synchronized (msgrcvr ){
Msgrcvr. Wait (100000 );
}

If (queuereceiver! = NULL ){
Queuereceiver. Close ();
}
If (queuesession! = NULL ){
Queuesession. Close ();
}
If (queueconnection! = NULL ){
Queueconnection. Close ();
}

}
Catch (exception ex ){
Ex. printstacktrace ();
}
}
}
Package myjms;

Import java. util .*;
Import javax. Naming .*;
Import javax. JMS .*;

Public class messagereciever
Implements messagelistener {
Public void onmessage (message ){
If (Message instanceof textmessage ){
Textmessage = (textmessage) message;
Try {
System. Out. println ("message content is:" + textmessage. gettext ());
}
Catch (jmsexception e ){
E. printstacktrace ();
}
}
}

Public static void main (string [] ARGs ){

Messagereciever msgrcvr = new messagereciever ();
String queueconnectionfactoryname = "myjmsconnectionfactory ";
String queuename = "myjmsqueue ";

Boolean transacted = false;
Int acknowledgementmode = session. auto_acknowledge;

Properties Properties = new properties ();
Properties. Put (context. initial_context_factory,
"Weblogic. JNDI. wlinitialcontextfactory ");
Properties. Put (context. provider_url, "T3: // localhost: 7001 ");

Try {
Context context = new initialcontext (properties );
Object OBJ = context. Lookup (queueconnectionfactoryname );
Queueconnectionfactory = (queueconnectionfactory)
OBJ;

OBJ = context. Lookup (queuename );
Queue queue = (Queue) OBJ;

Queueconnection = queueconnectionfactory.
Createqueueconnection ();
Queueconnection. Start ();
Queuesession = queueconnection. createqueuesession (transacted,
Acknowledgementmode );
Queuereceiver = queuesession. createreceiver (Queue );

Queuereceiver. setmessagelistener (msgrcvr );

Synchronized (msgrcvr ){
Msgrcvr. Wait (100000 );
}

If (queuereceiver! = NULL ){
Queuereceiver. Close ();
}
If (queuesession! = NULL ){
Queuesession. Close ();
}
If (queueconnection! = NULL ){
Queueconnection. Close ();
}

}
Catch (exception ex ){
Ex. printstacktrace ();
}
}
}

6. Message-driven Bean
MDB is actually a client program of the message consumer. It is managed by the as EJB iner. It is very easy to generate an MDB in JBuilder. Turn: http://jiasudu.iteye.com/blog/181498

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.