How to configure an Oracle J2CA JMS RA and use it with a java client

Posted by Steve Racanovic | Posted in , , | Posted on 2:20 PM

0

This article explain’s in point form to create a java client that excesses a resource adapter, from an oc4j instance that is configure with fast connection failover to RAC database. It extends from my previous JMS posts.

I am using:
OAS 10.1.3.2.0
RAC DB 10.2.0.3.0
Jdev 10.1.3.2

1. First logged in the database as “sys” user and created the queue.

I ran the following script:


------------------------------------------------------------
drop user jmsuser cascade;
------------------------------------------------------------
create user jmsuser identified by jmsuser;

grant create database link, create sequence, create session,
create synonym, create public synonym, drop public synonym,
create table, create view, create indextype,
create procedure, create trigger, create type,
unlimited tablespace
to jmsuser;

grant aq_administrator_role to jmsuser;
grant execute on sys.dbms_aqadm to jmsuser;
grant execute on sys.dbms_aq to jmsuser;

grant execute on sys.dbms_aqin to jmsuser;
grant execute on sys.dbms_aqjms to jmsuser;

grant force any transaction to jmsuser;

------------------------------------------------------------
connect jmsuser/jmsuser@judd;
------------------------------------------------------------
create or replace package ROUTERAdmin
AUTHID DEFINER as

procedure createQueueTable(qtablename IN VARCHAR2);
procedure createTopicTable(ttablename IN VARCHAR2);

procedure createQueue(
qname IN VARCHAR2,
qtablename IN VARCHAR2);

procedure createTopic(
tname IN VARCHAR2,
ttablename IN VARCHAR2);

end ROUTERAdmin;
/
show errors;
------------------------------------------------------------
------------------------------------------------------------
create or replace package body ROUTERAdmin as

---------------------------------
---------------------------------
procedure createQueue(
qname IN VARCHAR2,
qtablename IN VARCHAR2) is
begin
--
createQueueTable(qtablename);

--
DBMS_AQADM.CREATE_QUEUE(
Queue_name => qname,
Queue_table => qtablename,
max_retries => '2');

--
DBMS_AQADM.START_QUEUE(
queue_name => qname);
end;
---------------------------------
procedure createTopic(
tname IN VARCHAR2,
ttablename IN VARCHAR2) is
begin
--
createTopicTable(ttablename);

--
DBMS_AQADM.CREATE_QUEUE(
Queue_name => tname,
Queue_table => ttablename,
max_retries => '2');

--
DBMS_AQADM.START_QUEUE(
queue_name => tname);
end;

---------------------------------
procedure createQueueTable(qtablename IN VARCHAR2) is
begin
DBMS_AQADM.CREATE_QUEUE_TABLE(
Queue_table => qtablename,
Queue_payload_type => 'SYS.AQ$_JMS_MESSAGE',
sort_list => 'PRIORITY,ENQ_TIME',
multiple_consumers => false,
compatible => '9.2');
exception
when others then
dbms_output.put_line('EXC: createQueueTable');
return;
end;
---------------------------------
procedure createTopicTable(ttablename IN VARCHAR2) is
begin
DBMS_AQADM.CREATE_QUEUE_TABLE(
Queue_table => ttablename,
Queue_payload_type => 'SYS.AQ$_JMS_MESSAGE',
sort_list => 'PRIORITY,ENQ_TIME',
multiple_consumers => TRUE,
compatible => '9.2');
exception
when others then
dbms_output.put_line('EXC: createTopicTable');
return;
end;
---------------------------------

end ROUTERAdmin;
/
show errors;


------------------------------------------------
----- create topics and queues for jmsrouteruser
------------------------------------------------
connect jmsuser/jmsuser@judd;

begin
---- Queues ----
ROUTERAdmin.createQueue('RPQueue','RPTABLE');
ROUTERAdmin.createQueue('RPLogQ','RPQLOGTABLE');
end;
/

2. I am using the oc4j instance “OC4J_Apps”

Created a connection pool called “fcfConnPoolJMS”

ULR: jdbc:oracle:thin:@(DESCRIPTION=(LOAD_BALANCE=on)(...))

Username: jmsuser
Password: jmsuser

Connection Factory Properites:
connectionCachingEnabled = true
fastConnectionFailoverEnabled =true

Initial size of Connection Cache = 10
Minimum Number of Connections = 10
Maximum Number of Connections = 20





Save

3. Create a data source

Name: fcfDSJMS
JNDI Location: jdbc/fcfDSJMS
Connection Pool: fcfConnPoolJMS



Save

4. Create a Database Persistence.

OC4J_Apps -> Administration -> Database Persistence -> Deploy

Resource Adapter Module Name = mySimpleTestRA

Add new Resource Provider
Resource Provider Name = mySimpleTestRP
Datasource JNDI Location = jdbc/fcfDSJMS



Saved and then restarted OC4J instance “OC4J_Apps”.

5. Created Connection Factory.

OC4J_Apps -> Applications -> Standalone Resource Adapters (select from combo box) -> mySimpleTestRA (Resource Adapter Module)



Connection Factories -> Create

Connection Factory Interface = javax.jms.QueueConnectionFactory
JNDI Location = mySimpleTestRA



Save.

6. Create Administration Objects

Administration Objects -> create

Object Class = oracle.j2ee.ra.jms.generic.AdminObjectQueueImpl

JNDI Location = mySimpleTestRA/AutoWrap

Configuration Properties
ResourceProviderName = mySimpleTestRP



Save.

7. Create my java test client in Jdev.

package jmsracclient;

import java.util.Hashtable;

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

import javax.jms.*;

public class JMSClient {

public static void main(String[] args) {
final String queueName =
"java:comp/resource/mySimpleTestRP/Queues/RPQueue";
final String queueConnectionFactoryName =
"java:comp/resource/mySimpleTestRP/QueueConnectionFactories/QCF";
final String oc4juser = "oc4jadmin";
final String oc4juserpassword = "welcome1";
final String urlProvider = "opmn:ormi://";
final String jmsProviderHost = "sracanov-au2.au.oracle.com";
final String colon = ":";
final String opmnPort = "6005";
final String oc4jinstance = "OC4J_Apps";
final int NUMBER_OF_MESSAGES = 5;
Context jndiContext = null;
QueueConnectionFactory queueConnectionFactory = null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue queue = null;
QueueSender queueSender = null;
TextMessage message = null;

/*
* Set the environment for a connection to the OC4J instance
*/
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"oracle.j2ee.rmi.RMIInitialContextFactory");
env.put(Context.SECURITY_PRINCIPAL, oc4juser);
env.put(Context.SECURITY_CREDENTIALS, oc4juserpassword);
env.put(Context.PROVIDER_URL,
urlProvider + jmsProviderHost + colon + opmnPort + colon +
oc4jinstance + "/default");

/*
* Set the Context Object.
* Lookup the Queue Connection Factory.
* Lookup the JMS Destination.
*/
try {
jndiContext = new InitialContext(env);
System.out.println("Env set");
queueConnectionFactory =
(QueueConnectionFactory)jndiContext.lookup(queueConnectionFactoryName);
System.out.println("Got QCF");
queue = (Queue)jndiContext.lookup(queueName);
System.out.println("Got Q");
} catch (NamingException e) {
System.out.println("JNDI lookup failed: " + e.toString());
System.exit(1);
}
System.out.println("End of test client");
/*
* Create connection.
* Create session from connection.
* Create sender.
* Create text message.
* Send messages.
* Send non text message to end text messages.
* Close connection.
*/
try {
queueConnection = queueConnectionFactory.createQueueConnection();
queueSession =
queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queueSender = queueSession.createSender(queue);
message = queueSession.createTextMessage();
for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
message.setText("Message " + (i + 1));
System.out.println("Producing message: " + message.getText());
queueSender.send(message);
}
queueSender.send(queueSession.createBytesMessage());
} catch (JMSException e) {
System.out.println("Exception occurred: " + e.toString());
} finally {
if (queueConnection != null) {
try {
queueConnection.close();
} catch (JMSException e) {
System.out.println("Closing error: " + e.toString());
}
}
}
}
}


8. Ensure the project properties java options are set correctly for -Doracle.ons.oraclehome



This directory must contain opmn\conf\ons.config file. My ons.config file looks like:

localport=6100
remoteport=6202
nodes=aulnx11-vip.au.oracle.com:6202,aulnx12-vip.au.oracle.com:6202

9. The project should have the following libraries (in this order)

* JDBC Driver 10.2.0.3
* Apache Ant
* JSP Runtime
* Ons.jar
* J2EE



10. Run the java client. Received the following result:

Env set
Got QCF
Got Q
End of test client
Producing message: Message 1
Producing message: Message 2
Producing message: Message 3
Producing message: Message 4
Producing message: Message 5



Note: I didn’t explain the ons configuration on the oc4j server side for my OC4J_Apps instance.

If you have applied any patches to your Application Server, then use all the libraries from the server (step 9).