Using TIBCO Enterprise Message Service with WebLogic Server Message Driven Beans


This section describes how to use TIBCO Enterprise Message Service to drive a Message Driven Bean (MDB) within WebLogic Server. It assumes you have already set up your environment as described in the previous section.

Configure WebLogic Server for the Sample MDB

This example assumes that the application directory for the MDB is C:\Tibco\EMS\samples\client\bea\MDB. You can change the configuration appropriately for a different directory.

  1. Create an ejb-jar.xml file in the application directory as follows:
  2.  
       <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD 
       Enterprise JavaBeans 2.0//EN" 
       "http://java.sun.com/dtd/ejb-jar_2_0.dtd"> 
       <ejb-jar> 
          <enterprise-beans> 
             <message-driven> 
                <ejb-name>MDB</ejb-name> 
                <ejb-class>MDB</ejb-class> 
                <transaction-type>Container</transaction-type> 
                <message-driven-destination> 
                   <destination-type>javax.jms.Queue 
                   </destination-type> 
                </message-driven-destination> 
                <security-identity> 
                   <run-as> 
                      <role-name>everyone</role-name> 
                   </run-as> 
                </security-identity> 
             </message-driven> 
          </enterprise-beans> 
          <assembly-descriptor> 
             <security-role> 
                <role-name>everyone</role-name> 
             </security-role> 
          </assembly-descriptor> 
       </ejb-jar> 
     

    This file defines the EJB with a class name of "MDB" (file name MDB.class). The EJB container can manage transactions, and it specifies that the MDB reads from a Queue. Security for the EJB and MDB is set so that everyone can run it.

    Elements in this file are defined in the EJB 2.0 specification (see the EJB documentation at http://java.sun.com for more details).

  3. Create a weblogic-ejb-jar.xml file in the same directory as follows:
  4.  
       <?xml version="1.0"?> 
       <!DOCTYPE weblogic-ejb-jar PUBLIC "-//BEA Systems, Inc.//DTD 
       WebLogic 6.0.0 EJB//EN" 
       "http://www.bea.com/servers/wls600/dtd/weblogic-ejb-jar.dtd"> 
       <weblogic-ejb-jar> 
          <weblogic-enterprise-bean> 
             <ejb-name>MDB</ejb-name> 
             <message-driven-descriptor> 
                <pool> 
                   <max-beans-in-free-pool>200 
                   </max-beans-in-free-pool> 
                   <initial-beans-in-free-pool>20 
                   </initial-beans-in-free-pool> 
                </pool> 
                <destination-jndi-name>myQueue</destination-jndi-name> 
                <initial-context-factory> 
                   com.tibco.tibjms.naming.TibjmsInitialContextFactory 
                </initial-context-factory> 
                <provider-url>tibjmsnaming://localhost:7222 
                </provider-url> 
                <connection-factory-jndi-name>QueueConnectionFactory 
                </connection-factory-jndi-name> 
             </message-driven-descriptor> 
             <jndi-name>MDB</jndi-name> 
          </weblogic-enterprise-bean> 
          <security-role-assignment> 
             <role-name>everyone</role-name> 
             <principal-name>system</principal-name> 
          </security-role-assignment> 
       </weblogic-ejb-jar> 
     

    This file further defines the EJB named MDB to indicate how to get to the connection factory and the queue by specifying the JNDI names for the factory and the destination and also the URL for the provider. This example points to the same JNDI provider that was set up in the prior section, that is the TIBCO Enterprise Message Service server, and it identifies myQueue as the JNDI destination name for the MDB. This configuration also indicates that initially 20 beans will be deployed and up to 200 may be run if necessary to handle the message traffic.

    The elements in this file are WebLogic Server-specific extensions to the EJB 2.0 specification. This file is more completely described at http://edocs.bea.com/wls/docs60/ejb/reference.html#1071166.

  5. The source code for the message driven bean, MDB.java, is below. Save this source as a file named MDB.java to the MDB directory.
  6.  
       import javax.ejb.CreateException; 
       import javax.ejb.MessageDrivenBean; 
       import javax.ejb.MessageDrivenContext; 
       import javax.jms.Message; 
       import javax.jms.MessageListener; 
       import javax.jms.TextMessage; 
     
       public class MDB implements MessageDrivenBean, MessageListener { 
          private MessageDrivenContext context; 
     
       // Required - public constructor with no argument 
       public MDB () {} 
     
       // Required - ejbActivate 
       public void ejbActivate() {} 
     
       // Required - ejbRemove 
       public void ejbRemove() { 
          context = null; 
       } 
     
       // Required - ejbPassivate 
       public void ejbPassivate() {} 
     
       public void setMessageDrivenContext( 
          MessageDrivenContext mycontext) { 
          context = mycontext; 
       } 
     
       // Required - ejbCreate() with no arguments 
       public void ejbCreate () throws CreateException {} 
     
       // Implementation of MessageListener - throws no exceptions 
       public void onMessage(Message msg) { 
          try { 
             System.out.println("MDB: " + ((TextMessage)msg).getText() 
                + " Thread: " + Thread.currentThread().getName()); 
             Thread.sleep(2000, 0); 
          } 
          catch(Exception e) { // Catch any exception 
          e.printStackTrace(); 
          } 
       } 
    } 
    
  7. The following commands build the JAR file for the EJB. You should use the same console window where you invoked C:\bea\wlserver6.1\config\mydomain\setEnv.cmd in the last section.
  8.  
       mkdir build build\META-INF 
       copy ejb-jar.xml  build\META-INF 
       copy weblogic-ejb-jar.xml build\META-INF 
       javac -d build  MDB.java 
       cd build 
       jar cvf myejb.jar META-INF MDB.class 
       cd .. 
       java weblogic.ejbc -compiler javac build\myejb.jar MDB.jar 
    
  9. Add MDB.jar to the CLASSPATH in startWebLogic.cmd.
  10. The configuration file, ../config/mydomain/config.xml, must be updated to deploy the EJB by adding the following lines before the final </domain> line:
  11.  
       <Application Name="MDB" 
       Path="C:\TIBCO\EMS\samples\client\bea\MDB"> 
          <EJBComponent Name="MDB" URI="MDB.jar" Targets="myserver"/> 
       </Application> 
    
  12. The program, t.java, must be modified so that it only creates a foreign JMS provider message and sends it (take out the lines after TIBCOobject.JMSSend(msg); so you do not receive it — that is what the MDB is for).
  13. Recompile the test program using javac t.java.
Run This Example
  1. The TIBCO Enterprise Message Service server should still be running from the previous section, and therefore the administered objects QueueConnectionFactory and myQueue have already been created. In this example, the WebLogic Server, rather than the test program, reads from that queue.
  2. Restart WebLogic Server so that the modifications for the MDB take effect.

    When WebLogic Server starts, it echoes the CLASSPATH it is using. Make sure the CLASSPATH includes the TIBCO Enterprise Message Service JAR files as well as the MDB.jar file.

  3. Run the test program.
  4.  
       > java t 
     

    You should see the messages that are sent by the client and received by the MDB printing in the WebLogic Server window.


TIBCO Enterprise Message Service™ Application Integration Guide
Software Release 4.3, February 2006
Copyright © TIBCO Software Inc. All rights reserved
www.tibco.com