Example of JMS

Source: Internet
Author: User

1. 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 only forwarded to 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 detailed applications, the jms jdbc store may be more extensive, but no information is found at the moment)
(3) configure the JMS Server
(4) configure the JMS queue or JMS topic in destinations of the JMS Server
The message consumers and consumers are provided with the JNDI of the JMS connection factory and the JNDI of the JMS queue or JMS topic.
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:

Package myjms; import Java. util. *; import javax. naming. *; import javax. JMS. *; public class messageproducter {public static void main (string [] ARGs) {string queueconnectionfactoryname = "myjmsconnectionfactory"; // JNDI string queuename of JMS connection factory = "myjmsqueue "; // JMS queue or JMS topic's JNDI Boolean transacted = false; // transaction mode int acknowledgementmode = session. auto_acknowledge; // ackno Wledgement mode string message = "message need to send"; // simulate the message to be sent properties = new properties (); properties. put (context. initial_context_factory, "weblogic. JNDI. wlinitialcontextfactory "); properties. put (context. provider_url, "T3: // localhost: 7001"); try {context = new initialcontext (properties); object OBJ = context. lookup (queueconnectionfactoryname); queueconnectionfactory queueconn Ectionfactory = (queueconnectionfactory) OBJ; // obtain OBJ = context for JMS connection factory. lookup (queuename); queue = (Queue) OBJ; // obtain queueconnection = queueconnectionfactory from the JMS queue or JMS topic. createqueueconnection (); // generate the connection queueconnection. start (); queuesession = queueconnection. createqueuesession (transacted, acknowledgementmode); textmessage = queueses Sion. 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, you must 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:

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 response = "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 (cont Ext. provider_url, "T3: // localhost: 7001"); try {context = new initialcontext (properties); object OBJ = context. lookup (queueconnectionfactoryname); queueconnectionfactory = (queueconnectionfactory) OBJ; OBJ = context. lookup (queuename); 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 easy to generate an MDB in JBuilder.

Example of JMS

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.