Search This Blog

Friday, February 13, 2009

How to Send/Browse Messages to AQ via OC4J in a Standalone Java program

Set up your queues and AQ resource adapter etc using the following article.

http://rreddy.blogspot.com/2009/02/how-to-use-simple-mdb-with-oracle.html

The only difference with the Servlet and the standalone code with the connection factories and the queues JNDI lookup’s.

<resource-ref-mapping location="TEST_AQ/QueueCF" name="CommonConnectionFactory">

For the standalone java program we use the ConnectionFactory as java:comp/resource/TEST_AQ/QueueConnectionFactories/QueueCF

This can be found from the file oc4j-ra.xml file                    

For the queue we use the JNDI location for the Queue as

<message-destination-ref-mapping name="TestQueue" location="TEST_AQ/MyQueue/Queues/MYQUEUE" />

But for the standalone java client we use the  java:comp/resource/TEST_AQ/Queues/MYQUEUE                             
Example to Send a Message to JMS

package mdbaq;
import java.io.*;

import java.net.URL;

import java.util.Date;

import java.util.Hashtable;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.jms.* ;
import javax.jms.Queue;
import javax.naming.*;

public class SendJMS {
    public SendJMS() {
    }

    public static void main(String[] args) {
        SendJMS sendJMS = new SendJMS();
        Queue queue=null;
        QueueSession queueSession=null;
        QueueSender queueSender=null;
        Context jndiContext = null;
        QueueConnectionFactory queueConnectionFactory = null;
        QueueConnection queueConnection = null;

            try {

                  Hashtable env = new Hashtable();
                  //  Standalone OC4J connection details
                  env.put(Context.INITIAL_CONTEXT_FACTORY, "oracle.j2ee.rmi.RMIInitialContextFactory");
                  env.put(Context.SECURITY_PRINCIPAL, "oc4jadmin");
                  env.put(Context.SECURITY_CREDENTIALS, "welcome1");
                  env.put(Context.PROVIDER_URL, "ormi://localhost:23791");
                  jndiContext = new InitialContext(env);

            } catch (NamingException e)
            { System.out.println("Exception occurred: " + e.toString()); }

            try {
             //   queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("jms/qcf");
        queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("java:comp/resource/TEST_AQ/QueueConnectionFactories/QueueCF");
         //       queue = (Queue)jndiContext.lookup("jms/demoQueue");
                 queue = (Queue)jndiContext.lookup("java:comp/resource/TEST_AQ/Queues/MYQUEUE");
            queueConnection = queueConnectionFactory.createQueueConnection();
            queueSession= queueConnection.createQueueSession(true,Session.AUTO_ACKNOWLEDGE);
            queueSender = queueSession.createSender(queue);
            queueConnection.start();
            } catch (Exception e) {
            System.out.println("Exception occurred: " + e.toString());
            e.printStackTrace();
            }

        try {

        TextMessage message = queueSession.createTextMessage();
        message.setText("Test "+new Date());
        queueSender.send(message);
        queueSession.commit();
        } catch (JMSException e) {
        System.out.println("Exception occurred: " +e.toString());
            e.printStackTrace();
        }

        System.out.println( " Message Sent .....");

        }

    }

Example For Browsing the message

package mdbaq;

import java.util.Enumeration;

import java.util.Hashtable;

import javax.naming.InitialContext;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.Message;
import javax.jms.QueueSession;
import javax.jms.QueueBrowser;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;

import javax.jms.TextMessage;

import javax.naming.Context;

public class Browser
{
    public static void main(String[] args) throws Exception
    {
       // get the initial context
        Hashtable env = new Hashtable();
        //  Standalone OC4J connection details
        env.put(Context.INITIAL_CONTEXT_FACTORY, "oracle.j2ee.rmi.RMIInitialContextFactory");
        env.put(Context.SECURITY_PRINCIPAL, "oc4jadmin");
        env.put(Context.SECURITY_CREDENTIALS, "welcome1");
        env.put(Context.PROVIDER_URL, "ormi://localhost:23791");
       InitialContext ctx = new InitialContext(env);
       // lookup the queue object
      // Queue queue = (Queue) ctx.lookup("jms/demoQueue");
       Queue queue = (Queue) ctx.lookup("java:comp/resource/TEST_AQ/Queues/MYQUEUE");
       // lookup the queue connection factory
       QueueConnectionFactory connFactory =
       //(QueueConnectionFactory) ctx.lookup("jms/qcf");
      //  (QueueConnectionFactory) ctx.lookup("java:comp/resource/TEST_AQ/QueueConnectionFactories/QueueCF");
       (QueueConnectionFactory) ctx.lookup("java:comp/resource/TEST_AQ/QueueConnectionFactories/QueueCF");
       // create a queue connection
       QueueConnection queueConn = connFactory.createQueueConnection();
       // create a queue session
       QueueSession queueSession = queueConn.createQueueSession(false,
           Session.AUTO_ACKNOWLEDGE);
       // create a queue browser
       QueueBrowser queueBrowser = queueSession.createBrowser(queue);
       // start the connection
       queueConn.start();
       // browse the messages
       Enumeration e = queueBrowser.getEnumeration();
       int numMsgs = 0;
       // count number of messages
       while (e.hasMoreElements()) {
          Message message = (Message) e.nextElement();
         TextMessage msg = (TextMessage) message;
           System.out.println(msg.getText());
          numMsgs++;
       }
       System.out.println(queue + " has " + numMsgs + " messages");
       // close the queue connection
       queueConn.close();
    }
}

Run the Java program using the following option

java -classpath D:\MDB-AQ\classes;D:\temp2\jms.jar;D:\temp2\jta.jar;D:\temp2\pcl.jar;D:\temp2\aqapi13.jar;D:\temp2\ojdbc14.jar;D:\temp2\connector.jar;D:\temp2\jazncore.jar; D:\temp2\dms.jar;D:\temp2\bcel.jar;D:\temp2\mail.jar;D:\temp2\oc4j-internal.jar;D:\temp2\oc4jclient.jar;D:\temp2\adminclient.jar   mdbaq.Browser

No comments: