How to configure the OC4J resource provider for OJMS
using JDBC URL in application.xml?
Date: 24-JUN-2003
After reading this how-to document you should be able
to
Introduction
This document demonstrates how to configure Resource
Providers to access a queue in the database and enqueue a message through
it. This how-to configures the ResourceProvider through application.xml.
Software Requirements
-
Oracle9i Enterprise Edition
Release 9.2.0.2.0 - Production (and above) downloadable from here
-
Oracle9iAS
Containers for J2EE (OC4J) version 9.0.3 or later. You can download
the OC4J from Oracle Technology Network.
-
JDK1.3.x or above This can be downloaded
from here .
Description
The ResourceProvider interface enables you to plug
in third-party message providers for JMS connections. For Oracle JMS (OJMS),
this allows EJBs, Servlets and OC4J clients to access many different queue
implementations. With third-party message providers, only EJBs can access
queue implementations. The resources are available under
java:comp/resource/ as the default JMS resources.
Setup the Database
Before starting to understand the intricacies of ResourceProviders,
first let us create the database queues required for this how-to. Connect
to your database through sqlplus and do the following. Please make sure
that the database user has the execute privileges on dbms_aqadm,
dbms_aq and dbms_aqin
packages.
-- Create a Queue Table. The payload is SYS.AQ$_JMS_TEXT_MESSAGE.
-- This type can store simple String values.
BEGIN dbms_aqadm.create_queue_table( queue_table=>'RPTable', queue_payload_type=>'SYS.AQ$_JMS_TEXT_MESSAGE', multiple_consumers => false, comment => 'Queue Table For Resource Provider' );
END;
/
-- Create a Queue.
BEGIN dbms_aqadm.create_queue ( queue_name => 'RPQueue', queue_table => 'RPTable'); END; /
-- Start this queue. This enables the queue to receive messages.
EXECUTE dbms_aqadm.start_queue (queue_name=>'RPQueue') /
|
This completes the database setup.
Configuring a Custom Resource Provider
The OC4J resource provider for OJMS is implemented
by the class oracle.jms.OjmsContext. Each
OJMS resource provider instance is a <resource-provider>
XML element in the config file. To add a custom <resource-provider>,
add the following tag to your application.xml
file. If the ResourceProvider should be available locally to a specific
deployed application within OC4J, then add this tag to orion-application.xml
and package it with the ear.
<resource-provider class="oracle.jms.OjmsContext" name="TestRP"> <description> OTN How-to to configure Resource Provider</description> <property
name="url" value="jdbc:oracle:thin:rp/rp@ind712r.idc.oracle.com:1521:otn9i"> </property> </resource-provider>
|
For the above tag, please note the following
-
TestRP is the JNDI name by which the
application identifies the resource provider. This name will be used
in finding the resource provider in the application's JNDI as java:comp/resource/TestRP/.
- Replace the value attribute in the property tag with your database
connection parameters. The general construct is
jdbc:oracle:thin:<db_user>/<db_pwd>@<db_hostname>:<db_port>:<db_sid>
|
This completes the configuration of the Resource Provider.
Using the Custom Resource Provider
To access Oracle JMS queues through JMS, you must do
the following in the Java client (Java class, EJBs, Servlets, JSPs, etc...)
// Get the Initial Context Context jndiContext = new InitialContext();
// LookUp the QueueTable and create the QueueConnectionFactory QueueConnectionFactory queueCF = (QueueConnectionFactory)jndiContext.lookup ("java:comp/resource/TestRP/QueueConnectionFactories/RPTable");
// Lookup the Queue and create its object Queue queue = (Queue)jndiContext.lookup("java:comp/resource/TestRP/Queues/RPQueue");
// Create a Connection to the QueueTable QueueConnection queueConnection = queueCF.createQueueConnection();
// Start the Connection queueConnection.start();
// Create a Session to the queue QueueSession queueSession = queueConnection.createQueueSession (true, Session.AUTO_ACKNOWLEDGE);
// Create an object through which this program enqueues QueueSender sender = queueSession.createSender(queue);
// Create a Text Message to be enqueued Message msg = queueSession.createTextMessage("Did you have your OTN Sample today?");
// Enqueue the message in the queue sender.send(msg);
|
Please click here to see the
complete JSP which uses the above code to demonstrate the feature of ResourceProvider.
Executing the JSP
- Copy the
ResourceProvider.jsp
to the <oc4j_home>\j2ee\home\default-web-app\examples.
<oc4j_home> is the folder where OC4J
is installed. For example, on Windows it is D:\oc4j
and in Linux or Solaris it is /home/oc4j.
- Start the OC4J server.
- Open the browser and access the URL
http://<host_name>:<oc4j_port>/examples/ResourceProvider.jsp.
- On the first page, you will see a TextBox. Enter some text in it.
This text message will be enqueued to the database queue. Press the
Enqueue Message button.
- Now, you will see a
Message Enqueued Successfully
!!!. This means that the message provided by you has been enqueued
successfully through the ResourceProvider. To check this, login to the
database user (where the queue was created) through sqlplus and run
the following
set serveroutput on;
declare deqopt dbms_aq.dequeue_options_t; mprop dbms_aq.message_properties_t; msgid RAW(16); payload SYS.AQ$_JMS_TEXT_MESSAGE;
begin deqopt.navigation := DBMS_AQ.FIRST_MESSAGE; deqopt.wait := 1; dbms_aq.dequeue( queue_name => 'RPQueue', dequeue_options => deqopt, message_properties => mprop, payload => payload, msgid => msgid
); commit;
dbms_output.put_line('The Message Sent to the Queue is: ' || payload.TEXT_VC);
end; /
|
After this, you should see the message enqueued by you on the sql prompt.
Resources
|