JMS message Producer and Consumer class- JMSProducerConsumer.java

Asynchronous Client - ASyncClient.java

/*
 * @author : Chandar
 * @version 1.0

 *
 * Development Environment : Oracle9i JDeveloper
 *
 * Name of the File        : JMSProducerConsumer.java
 *
 * Creation / Modification History
 *

 *    Chandar          12-Sep-2003        Created
 *
 *  Overview :
 *  This class serves as a producer and consumer of JMS messages.
 *  It gets the input message from user and enqueues it to the
 *  queue. It then dequeues the message fro the queue and
 *  publishes it to the topic. The class uses the domain
 *  unification concept introdued in JMS 1.1 that uses common

 *  interfaces like Connection, Session, Destination, MessageConsumer,
 *  and MessageProducer to send and receive messages from queue
 *  and topic.
 */


package oracle.otnsamples.jms;

// JNDI related imports

import javax.naming.InitialContext;
import javax.naming.Context;


// jms related imports
import javax.jms.Queue;
import javax.jms.Topic;
import javax.jms.Session;
import javax.jms.Connection;
import javax.jms.Destination;

import javax.jms.TextMessage;
import javax.jms.MessageProducer;
import javax.jms.MessageConsumer;
import javax.jms.ConnectionFactory;

import java.util.Hashtable;


public class JMSProducerConsumer{



   /**
    * Empty Constructor
    */
   public JMSProducerConsumer()
   {}

   /**
    * Definition of main method of class
    */
   public static void main (String args[]) throws Exception{

     try{

       // create instance of class
       JMSProducerConsumer obj = new JMSProducerConsumer();
       String message = new String();
         
       // construct message from argument list   
       for(int i=0; i<args.length; i++)
         message = message + " " +args[i];


       // perform operations on queue and topic
       obj.performMessagingOperations(message);
     }
     catch(Exception e){

     e.printStackTrace();
     }
   }


   /**
    * This method uses JMS 1.1 common interfaces between
    * queue and topic to send and receive messages from
    * either of the destinations.
    */
   private void performMessagingOperations(String userMsg)
                                          throws Exception{

     // environment object for initial context

     Hashtable env = new Hashtable();

     // set the environment properties
     env.put(Context.INITIAL_CONTEXT_FACTORY,
             "com.evermind.server.rmi.RMIInitialContextFactory");
     env.put(Context.PROVIDER_URL, ConnectionParams.PROVIDER_URL);
     env.put(Context.SECURITY_PRINCIPAL, ConnectionParams.USERNAME);
     env.put(Context.SECURITY_CREDENTIALS, ConnectionParams.PASSWORD);

     // get the initial context
     InitialContext ctx = new InitialContext(env);

     // lookup the queue and topic objects
     Destination queue = (Destination) ctx.lookup("jms/sampleQueue");
     Destination topic = (Destination) ctx.lookup("jms/sampleTopic");


     // lookup a connection factory
     ConnectionFactory factory = (ConnectionFactory) ctx.
     lookup("jms/sampleConnectionFactory");

     // create a connection
     Connection connection = factory.createConnection();

     // create a transacted session
     Session session = connection.createSession(true,
                                        Session.AUTO_ACKNOWLEDGE);

     // create a message producer for the queue
     MessageProducer sender = session.createProducer(queue);

     // create a message consumer for the queue

     MessageConsumer receiver = session.createConsumer(queue);

     // create a message producer for the topic
     MessageProducer publisher = session.createProducer(topic);

     // start the connection
     connection.start();

     System.out.println("Sending Message to the queue");

     // send the user message to the queue
     sender.send(session.createTextMessage(userMsg));

     // commit the session (put messages on queue)
     session.commit();

     System.out.println("Consuming messages from queue"+

                        " and sending it to the topic");

     // consume messages from queue
     TextMessage msg = (TextMessage) receiver.receive();

     // publish the message to the topic
     publisher.send(msg);

     // commit receive and send operations
     session.commit();

     // close connection
     connection.close();
  }

}

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy