Friday, October 3, 2008

Lookup Oracle database queue (AQ) with jndi and LDAP

In my previous blog entry I explained how you can register tnsnames entries and database connections in OpenLDAP, this blog entry goes a little further. This blog explains how you can register Oracle database queues (AQ) in ldap and use them with JNDI. I don't use OID because the product takes too many resources. I just want to lookup some queues. So I downloaded openldap and create the oracle ldap schema and add this to openldap. Now I can register oracle object in LDAP and use it in java.

Here you see how my LDAP tree looks like. Click here to get the LDIF file
SCOTT.TEST is a queue called test in the scott oracle schema. SCOTT.TEST_TABLE is the queue table of the test queue.
The LDAP attributes of the queue entry and this queue has a pointer to the queue table (first attribute)
The queue table attributes

The database connection registration which you can use for the connection factory
Here is the plsql code to create the queue and queue table

begin
sys.dbms_aqadm.create_queue_table(
queue_table => 'TEST_TABLE',
queue_payload_type => 'SYS.AQ$_JMS_MESSAGE',
sort_list => 'PRIORITY',
compatible => '10.0.0',
primary_instance => 0,
secondary_instance => 0,
storage_clause => 'tablespace USERS pctfree 10 initrans 1 maxtrans 255 storage ( initial 64K minextents 1 maxextents unlimited )');
end;
/
begin
sys.dbms_aqadm.create_queue(
queue_name => 'TEST',
queue_table => 'TEST_TABLE',
queue_type => sys.dbms_aqadm.normal_queue,
max_retries => 5,
retry_delay => 0,
retention_time => 0);
end;
/

The java code where we do a lookup of the database connection to create the connection factory and do a lookup to create a queue.

package jms2;

import java.util.Hashtable;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;

import javax.jms.TextMessage;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

//import oracle.jms.AQjmsOracleDebug;


public class jmsclient2 {

Hashtable env = null;
boolean envSet = false;
private QueueConnection connection = null;
private QueueSession session = null;
private QueueSender sender = null;
private QueueReceiver receiver = null;
private QueueConnectionFactory queueConnectionFact = null;;
private Queue queue = null;;


public void testRegistration() {
// AQjmsOracleDebug.setLogStream(out);
// AQjmsOracleDebug.setTraceLevel(AQjmsOracleDebug.AQ_ORA_TR6);
// AQjmsOracleDebug.setDebug(true);

env = new Hashtable(5, 0.75f);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL , "ldap://localhost:389");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL , "o=sgi,c=us");
env.put(Context.SECURITY_CREDENTIALS , "secret");

DirContext dirContext = null;
try {
dirContext = new InitialDirContext(env);

dirContext=(DirContext)dirContext.lookup("cn=ORCL, cn=OracleContext, ou=Services, o=sgi,c=us");
DirContext destctxCF = (DirContext)dirContext.lookup("cn=oracledbconnections");
DirContext destctxQF = (DirContext)dirContext.lookup("cn=OracleDBQueues");

queueConnectionFact = (QueueConnectionFactory)destctxCF.lookup("cn=SCOTT");
queue = (Queue) destctxQF.lookup("cn=SCOTT.TEST");

connection = queueConnectionFact.createQueueConnection();
session = connection.createQueueSession(true, QueueSession.AUTO_ACKNOWLEDGE);
connection.start();

sender = session.createSender(queue);
String xmlData = "1111";
TextMessage message = session.createTextMessage(xmlData);
sender.send(message);

receiver = session.createReceiver(queue);
TextMessage textMessage = (javax.jms.TextMessage)receiver.receive();
String xmlText = textMessage.getText();
System.out.println(xmlText);


} catch (NamingException ne) {
ne.printStackTrace();
} catch (JMSException jmse) {
jmse.printStackTrace();
}
}

public static void main (String[] args) {
jmsclient2 client = new jmsclient2();
client.testRegistration();

}
}


That's all.

5 comments:

  1. Hi,
    In your LDIF file you have javaClassName: oracle.jms.AQjmsDestination
    , so does that mean that we have copy oracle jars to ldap server?

    ReplyDelete
  2. Hi,

    no this is not necessary, this is a reference to the java client which java class it should use after the ldap lookup.

    thanks Edwin

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Hi,

    i have one question why are using aq instead of JMS,i mean both adapters doing same work,so what is main difference

    ReplyDelete
    Replies
    1. Hi,

      AQ is based on an Oracle object type ( too much Oracle ) and JMS is the industry standard but for WebLogic you need to configure a Foreign Server to use it in a JMS resource adapter.

      thanks

      Delete