Search This Blog

Tuesday, February 03, 2009

How to use a Simple MDB with Oracle Database provider in 10. 1.3

1. Create the following entries in the data-sources.xml file

<managed-data-source connection-pool-name="AQ Connection Pool"
jndi-name="jdbc/aq"
name="AQ Connection Pool"/>
<connection-pool name="AQ Connection Pool">
<connection-factory
factory-class="oracle.jdbc.pool.OracleDataSource"
user="jmsuser" password="jmsuser"
url="jdbc:oracle:thin:@incq128ad:1521:ias1012"/>
</connection-pool>

2. Login to sql*plus and create the AQ objects

CONNECT system/manager;
DROP USER jmsuser CASCADE ;
GRANT CONNECT, RESOURCE,AQ_ADMINISTRATOR_ROLE TO jmsuser IDENTIFIED BY jmsuser ;
CONNECT jmsuser/jmsuser;
BEGIN
dbms_aqadm.create_queue_table ( queue_table=> 'TEST', queue_payload_type=> 'SYS.AQ$_JMS_TEXT_MESSAGE', sort_list=> '', comment=> '', multiple_consumers=> FALSE, message_grouping=> DBMS_AQADM.NONE, non_repudiation => DBMS_AQADM.NONE, storage_clause=> '', compatible=> '8.1', primary_instance=> '0', secondary_instance=> '0');
COMMIT;
END;
BEGIN
dbms_aqadm.create_queue(queue_name=> 'MYQUEUE', queue_table=> 'TEST', queue_type=> DBMS_AQADM.NORMAL_QUEUE, max_retries=> '5', retry_delay=> '0', retention_time=> '0', comment=> '');
COMMIT;
END;
BEGIN
dbms_aqadm.start_queue('JMSUSER.MYQUEUE', TRUE, TRUE);
COMMIT;
END;

3. Test whether the enqueing and dequing is working fine or not
Open a sqlplus window and login as jmsuser and run the following sql.

enque.sql
---------
DECLARE
enqueue_options dbms_aq.enqueue_options_t;
message_properties dbms_aq.message_properties_t;
message_handle RAW(16);
Message SYS.AQ$_JMS_TEXT_MESSAGE;
BEGIN
Message := Sys.Aq$_Jms_Text_Message.Construct;
Message.Set_Text(to_char(sysdate, 'DD/MM/YYYY HH24:MI:SS'));
dbms_aq.enqueue(queue_name => 'MYQUEUE',
enqueue_options => enqueue_options,
message_properties => message_properties,
payload => message,
msgid => message_handle);
COMMIT;
END;

Open another sqlplus window and login as jmsuser and run the following sql.

deque.sql
---------
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 => 'MYQUEUE',
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;

Now you should see the message in the sql*plus window as
The Message Sent to the Queue is: 13/03/2008 17:48:34
4. Start the oc4j container and access the EM Application Server Control
Go to the Administration TAB
Click on the Database Persistence link.

5. In this screen click on the deploy Button
Give the "Resource Adapter Module Name" as : TEST_AQ
And select the Radio Button
"Add a new resource provider to be used by this connector" Give the "Resource Provider Name" as TEST_AQ and
select the "Datasource JNDI Location" as jdbc/aq.

This will add the following values to the application.xml file

<resource-provider name="TEST_AQ" class="oracle.jms.OjmsContext">
<description></description>
<property name="datasource" value="jdbc/aq" />
</resource-provider>

Restart the oc4j container after this.

6. Now click on this Resource Adapter
and select the TAB
Connection Factory and select the QueueConnectionFactory
TEST_AQ/QueueCF
7. Now Click On "Administrated Objects" TAB and enter the following value

Choose the ObjectClass as oracle.j2ee.ra.jms.generic.AdminObjectQueueImpl
Give the JNDI Location as TEST_AQ/MyQueue and resourceProviderName as
TEST_AQ.

Software Requirements/Prerequisites

8. From the JDeveloper Create a Message driven and change the OnMessage Method

package mdbaq;
import javax.ejb.EJBException;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class MessageDrivenEJBBean implements MessageDrivenBean, 
                                             MessageListener {
    private MessageDrivenContext _context;
    public void ejbCreate() {
    }
    public void setMessageDrivenContext(MessageDrivenContext context) throws EJBException {
        _context = context;
    }
    public void ejbRemove() throws EJBException {
    }
    public void onMessage(Message message)
            {
                    System.out.println("Received message: " + message);
                    TextMessage msg = null;
            try {
                if (message instanceof TextMessage) {
                    msg = (TextMessage) message;
                    System.out.println("MESSAGE BEAN: Message received: " 
                        + msg.getText());
                } else {
                    System.out.println("Message of wrong type: " 
                        + message.getClass().getName());
                }
            } catch (JMSException e) {
                e.printStackTrace();
                       } catch (Throwable te) {
                te.printStackTrace();
            }
            }
}

9. Open the ejb-jar.xml and have the following entries

<?xml version = '1.0' encoding = 'windows-1252'?>
<ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd" version="2.1" xmlns="http://java.sun.com/xml/ns/j2ee">
  <enterprise-beans>
    <message-driven>
      <description>Message Driven Bean</description>
      <display-name>MessageDrivenEJB</display-name>
      <ejb-name>MessageDrivenEJB</ejb-name>
      <ejb-class>mdbaq.MessageDrivenEJBBean</ejb-class>
      <messaging-type>javax.jms.MessageListener</messaging-type>
      <transaction-type>Container</transaction-type>
      <message-destination-type>
javax.jms.Queue
</message-destination-type>
    </message-driven>
  </enterprise-beans>
  <assembly-descriptor/>
</ejb-jar>

Open the orion-ejb-jar.xml file

<?xml version = '1.0' encoding = 'windows-1252'?>
<orion-ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://xmlns.oracle.com/oracleas/schema/orion-ejb-jar-10_0.xsd" schema-major-version="10" schema-minor-version="0">
    <enterprise-beans>
        <message-driven-deployment name="MessageDrivenEJB"
                                   connection-factory-location="TEST_AQ/QueueCF"
                                   destination-location="TEST_AQ/MyQueue/Queues/MYQUEUE"/>
    </enterprise-beans>
</orion-ejb-jar>

11. Create a simple Servlet to send the message to the Queue

package mdbaq;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.jms.* ;
import javax.jms.Queue;
import javax.naming.*;
import java.util.* ;
public class HelloWorld extends HttpServlet {
Queue queue;
QueueSession queueSession;
QueueSender queueSender;
Context jndiContext = null;
QueueConnectionFactory queueConnectionFactory = null;
QueueConnection queueConnection = null;
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>Hello MDB </TITLE></HEAD>");
out.println("<BODY>");
try {
TextMessage message = queueSession.createTextMessage();
message.setText(req.getParameter("subject"));
queueSender.send(message);
queueSession.commit();
} catch (JMSException e) { System.out.println("Exception occurred: " +e.toString());}
out.println("<H3> " + req.getParameter("subject") +" Message Sent .....</H3>");
out.println("</BODY></HTML>");
}
public void init(ServletConfig config) throws ServletException
{
super.init(config);
try {
jndiContext = new InitialContext();
queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("CommonConnectionFactory");
queue = (Queue)jndiContext.lookup("TestQueue");
} catch (NamingException e)
{ System.out.println("Exception occurred: " + e.toString()); }
try {
queueConnection = queueConnectionFactory.createQueueConnection("jmsuser","jmsuser");
queueSession= queueConnection.createQueueSession(true,Session.AUTO_ACKNOWLEDGE);
queueSender = queueSession.createSender(queue);
queueConnection.start();
} catch (JMSException e) {System.out.println("Exception occurred: " + e.toString());}
}
}

12. The web.xml
Add the following entries to the web.xml file

<resource-ref>
<res-ref-name>CommonConnectionFactory</res-ref-name>
<res-type>javax.jms.QueueConnectionFactory</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
    <message-destination-ref>
        <description></description>
        <message-destination-ref-name>TestQueue</message-destination-ref-name>
<message-destination-type>javax.jms.Queue</message-destination-type>
<message-destination-usage>Produces</message-destination-usage>
<message-destination-link>TestQueue</message-destination-link>
    </message-destination-ref>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list> 

<?xml version="1.0"?>
<orion-web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://xmlns.oracle.com/oracleas/schema/orion-web-10_0.xsd"  deployment-version="10.1.3.3.0"
deployment-time="1205407531953"
jsp-cache-directory="./persistence"
jsp-cache-tlds="standard"
temporary-directory="./temp"
servlet-webdir="/servlet/"
context-root="/aq"
schema-major-version="10" schema-minor-version="0" >
<resource-ref-mapping location="TEST_AQ/QueueCF" name="CommonConnectionFactory">
</resource-ref-mapping>
<message-destination-ref-mapping name="TestQueue" location="TEST_AQ/MyQueue/Queues/MYQUEUE" />
<web-app>
</web-app>
</orion-web-app>

14. Create a simple index.html to invoke the Servlet that sends the message to the AQ.

<html>
<head>
</head>
<body>
<form action="helloworld">
<b>Subject: </b><input type="text" name="subject"><br>
<input type="submit" value="Log">
</form>
</body>
</html>

Configuring the Sample Code

use Jdeveloper to open the project

Running the Sample Code

Run the URL http://xxx.xx.oracle.com:8888/aq/helloworld?subject=test123

Sample Code Output

08/03/13 18:50:44 Received message: oracle.j2ee.ra.jms.generic.TextMessageWrapper@bb5e2d
08/03/13 18:50:44 MESSAGE BEAN: Message received: test123

No comments: