User: pra     
  Date: 01/08/07 14:08:59

  Added:       src/examples/org/jboss/docs/jms/mdb/bean HelloListener.java
                        HelloMDB.java HelloWorkerBean.java
  Log:
  Added example code for new JMS chapter
  
  Revision  Changes    Path
  1.1                  
manual/src/examples/org/jboss/docs/jms/mdb/bean/HelloListener.java
  
  Index: HelloListener.java
  ===================================================================
  /*
  * JBoss, the OpenSource EJB server
  *
  * Distributable under LGPL license.
  * See terms of license at gnu.org.
  */
  package org.jboss.docs.jms.mdb.bean;
  
  import javax.ejb.MessageDrivenBean;
  import javax.ejb.MessageDrivenContext;
  import javax.ejb.EJBException;
  import javax.ejb.CreateException;
  
  import javax.naming.InitialContext;
  import javax.naming.Context;
  
  import javax.jms.MessageListener;
  import javax.jms.Message;
  import javax.jms.TextMessage;
  
  import org.jboss.docs.jms.mdb.interfaces.HelloWorker;
  import org.jboss.docs.jms.mdb.interfaces.HelloWorkerHome;
  
  /**
   * Simple MDB showing the listener pattern. Gets messages from a destination and 
invokes a woker session bean (actually it could as well do that work itself).
   *
   *
   * Created: Thu Jul 26 13:20:32 2001
   *
   * @author Peter Antman
   * @version $Revision: 1.1 $ $Date: 2001/08/07 21:08:59 $
   */
  
  public class HelloListener implements MessageDrivenBean, MessageListener {
     
     /**
      * Session worker bean
      */
     private HelloWorkerHome workerHome = null;
     
     private MessageDrivenContext ctx = null;
     
     public HelloListener() {
        
     }
     
     //--- MessageDrivenBean
     public void setMessageDrivenContext(MessageDrivenContext ctx)
        throws EJBException {
        this.ctx = ctx;
     }
     
     public void ejbCreate() throws CreateException{
        // Get the worker bean home
        try {
         Context initCtx = new InitialContext();
         workerHome = (HelloWorkerHome)initCtx.lookup("java:comp/env/ejb/worker");
        }catch(Exception ex) {
         throw new CreateException("Could not get worker: " + ex);
        }
     }
     
     public void ejbRemove() {ctx=null;}
     
     //--- MessageListener
     public void onMessage(Message message) {
        System.out.println("HelloListen got message" + message.toString() );
        try {
         // Get the worker
         HelloWorker worker = workerHome.create();
         
         // Get the message, here we could have an ObjectMessage containg
         // an object of a known class. We use Text here for simplicity
         if (message instanceof TextMessage) {
            TextMessage m = (TextMessage)message;
            
            // invoke the worker bean
            worker.doWork(m.getText());
         }
         
        }catch(Exception ex) {
         // In a container managed transaction with required, this will roll
         // back the message receipt and also all beans and resource 
         // calles from this one that inherits the transactional context.
         throw new EJBException("Could not call worker " + ex);
        }
     }
     
  } // HelloListener
  
  
  
  1.1                  manual/src/examples/org/jboss/docs/jms/mdb/bean/HelloMDB.java
  
  Index: HelloMDB.java
  ===================================================================
  /*
  * JBoss, the OpenSource EJB server
  *
  * Distributable under LGPL license.
  * See terms of license at gnu.org.
  */
  package org.jboss.docs.jms.mdb.bean;
  
  import javax.ejb.MessageDrivenBean;
  import javax.ejb.MessageDrivenContext;
  import javax.ejb.EJBException;
  
  import javax.jms.MessageListener;
  import javax.jms.Message;
  
  /**
   * Simple HelloWorld Message Driven Bean. May be bound to both a Topic or a Queue 
through the deployment descriptor.
   *
   *
   * Created: Thu Jul 26 13:20:32 2001
   *
   * @author Peter Antman
   * @version $Revision: 1.1 $ $Date: 2001/08/07 21:08:59 $
   */
  
  public class HelloMDB implements MessageDrivenBean, MessageListener {
  
     private MessageDrivenContext ctx = null;
     
     public HelloMDB() {
        
     }
     
     //--- MessageDrivenBean
     public void setMessageDrivenContext(MessageDrivenContext ctx)
        throws EJBException {
        this.ctx = ctx;
     }
     
     public void ejbCreate() {}
     
     public void ejbRemove() {ctx=null;}
     
     //--- MessageListener
     public void onMessage(Message message) {
        System.err.println("Bean got message" + message.toString() );
     }
     
  } // HelloMDB
  
  
  
  1.1                  
manual/src/examples/org/jboss/docs/jms/mdb/bean/HelloWorkerBean.java
  
  Index: HelloWorkerBean.java
  ===================================================================
  /*
  * JBoss, the OpenSource EJB server
  *
  * Distributable under LGPL license.
  * See terms of license at gnu.org.
  */
  package org.jboss.docs.jms.mdb.bean;
  
  import javax.ejb.SessionBean;
  import javax.ejb.SessionContext;
  import javax.ejb.CreateException;
  import java.rmi.RemoteException;
  
  /**
   * A worker session bean. When invokes through a MDB listener this bean will
   * be invokes asynchonously by sending messages to the JMS destination the MDB
   * is configured to subscribe to.
   *
   *
   * @author Peter Antman
   * @version $Revision: 1.1 $
   */
  
  public class HelloWorkerBean implements SessionBean {
     private SessionContext ctx;
     
     public HelloWorkerBean() {
        
     }
     
     // The usual sessions ejb methods.
     public void setSessionContext(javax.ejb.SessionContext ctx) throws 
RemoteException { this.ctx = ctx; }
     public void unsetSessionContext() throws RemoteException { this.ctx = null; }
     public void ejbActivate() throws RemoteException {}
     public void ejbPassivate() throws RemoteException {}
     public void ejbRemove() throws RemoteException {}
     public void ejbCreate() throws CreateException {}
     
     /**
      * Do the work, invoked from MDB listener. I.e, this bean will be 
      * invoked asynchrously by sending messages to th
      */
     public void doWork(String work) {
           System.out.println("WorkerBean doing work: " + work);
       }
  } // HelloWorkerBean
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to