Implementing a Message-Driven Bean in Orion.

(Tested with orion 1.5.4)

1.) ==========================  The MDB code - LogBean

package mycode.ejb.mdb;

import javax.ejb.*;
import javax.jms.*;

public class LogBean implements MessageDrivenBean, MessageListener {

 protected MessageDrivenContext ctx;

 public void setMessageDrivenContext(MessageDrivenContext ctx) {
  this.ctx = ctx;
 }

 public void ejbCreate() {
  System.out.println("ejbCreate()");
 }

 public void onMessage(Message msg) {

  TextMessage tm = (TextMessage) msg;

  try {
   String text = tm.getText();
   System.out.println("Received new message : " + text);
  }
  catch(JMSException e) {
   e.printStackTrace();
  }
 }

 public void ejbRemove() {
  System.out.println("ejbRemove()");
 }
}

2.) ========================== The deployment descriptor (ejb.jar)

<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.2//EN" 
"http://java.sun.com/j2ee/dtds/ejb-jar_1_2.dtd";>

<ejb-jar>
        <display-name>Test Message Bean</display-name>
        <description>A test of message beans</description>
        <enterprise-beans>
         <message-driven>

                 <!-- The name for the bean - used in binding with jms -->
                 <ejb-name>Log</ejb-name>

                 <!-- The fully qualified package name of the bean class -->
                 <ejb-class>mycode.ejb.mdb.LogBean</ejb-class>

                 <!-- The type of transaction supported (see Chapter 10) -->
                 <transaction-type>Container</transaction-type>

                 <!-- Whether I'm listening to a topic or a queue -->
                 <message-driven-destination>
                        <destination-type>javax.jms.Queue</destination-type>
                 </message-driven-destination>

                </message-driven>

        </enterprise-beans>
        <assembly-descriptor>
        
        </assembly-descriptor>
</ejb-jar>



3.) ==========================  Activate (uncomment) jms in server.xml



4.) ==========================  Update jms.xml to include the queue

        <queue name="Log" location="jms/theLog">
                <description>Test queue for MDB LogBean</description>
        </queue>


5.) ==========================  Update web.xml to include the resource references to 
jms factory and queue


        <resource-ref>
                <res-ref-name>jms/theQueueConnectionFactory</res-ref-name>
                <res-type>javax.jms.QueueConnectionFactory</res-type>
                <res-auth>Container</res-auth>
        </resource-ref>

        <resource-ref>
                <res-ref-name>jms/theLog</res-ref-name>
                <res-type>javax.jms.Queue</res-type>
                <res-auth>Container</res-auth>
        </resource-ref>

6.) ==========================  Send a message to the jms queue from application code 
(jsp in this case)


<%@ page import="javax.naming.*" %>
<%@ page import="javax.jms.*" %>

<%

        try
        {
                Context context = new InitialContext();
          QueueConnectionFactory factory = 
(QueueConnectionFactory)context.lookup("java:comp/env/jms/theQueueConnectionFactory");
                

                QueueConnection connection = factory.createQueueConnection();
                Queue queue = (Queue)context.lookup("java:comp/env/jms/theLog");
                connection.start();

                QueueSession sess = connection.createQueueSession(false, 
QueueSession.AUTO_ACKNOWLEDGE);

                QueueSender sender = sess.createSender(queue);

                int delivery_mode = DeliveryMode.PERSISTENT;
                TextMessage message = sess.createTextMessage("well hello there...");
                sender.send(message, delivery_mode, 1, 0);

                sess.close();
                connection.close();

        } catch(JMSException e) {
                System.err.println("JMS Communication error: " + e.getMessage());
        } catch(NamingException e) {
                System.err.println("Naming Error looking up objects: " + 
e.getMessage());
        }

%>

<html>
<head>
<title>Asp Services - Test</title>
<link rel ="stylesheet" type="text/css" href="./stylesheet.css" title="Style">
<style>

</style>
</head>
<body>
Done.
</body>
</html>

7.) ==========================  Done. 

Note: LogBean example was taken from "Mastering Enterprise Beans II (draft)" from 
theServerSide.com



 


-----Original Message-----
From: Michael Maurer [mailto:[EMAIL PROTECTED]]
Sent: 29 April 2002 01:20 PM
To: Orion-Interest
Subject: Message Driven Beans 



I am trying to deploy a message driven bean on the Oracle9iAS
(9.0.3.0.0) 

Are there some good examples where all necessary settings are described?
(Starting from jms.xml to orion-ejb-jar.xml!)

Or can anybody help me out with some important hints ?

 
Regards Michael





Reply via email to