Chapter 14. Third Party Examples

Table of Contents

Running the Examples from Enterprise JavaBeans, by Richard Monson-Haefel (Unix)
Deploying the Pet Store 1.1.2 Demo in JBoss
Deploying Cocoon 2 in JBoss
Using SSL with JBoss

Running the Examples from Enterprise JavaBeans, by Richard Monson-Haefel (Unix)

Author:Sebastien Alborini <sebastien.alborini@m4x.org>

This page describes how to run the examples from Richard Monson-Haefel's book Enterprise JavaBeans, 2nd Edition (Chapter 4) in JBoss.

You can download the examples (zip file) from O'Reilly's site. I will assume you have unzipped this file and you work in the chapter4/EJB11 directory.

These examples need to be slightly modified to run with JBoss. You can download the modified version here, but I recommend you to follow these instructions which tell exactly what has to be modified.

  • Setup your environment.

    JBoss libraries will be needed to compile and run the examples, so you have to set the environment variable JBOSS_HOME to your JBoss installation. For example:

    • export JBOSS_HOME=$HOME/jboss_pr4 if you have the binary version in your home directory

    • export JBOSS_HOME=$HOME/jboss/dist if you have the CVS version.

  • Compile and deploy the beans.

    The beans are almost ok for JBoss, the only difference is about the reference made by TravelAgentBean to CabinBean: a bean must lookup in the java:comp/env namespace. Edit com/titan/travelagent/TravelAgentBean.java, and replace

         Object obj = jndiContext.lookup("ejb/CabinHome");
    with
         Object obj = jndiContext.lookup("java:comp/env/ejb/CabinHome"); 

    This ejb-reference from TravelAgentBean (in travelagent.jar) to CabinBean (in cabin.jar) is an external reference (different ejb-jar file). You must then provide the full jndi name for CabinBean in a jboss.xml for TravelAgentBean: create and edit com/titan/travelagent/jboss.xml

         <?xml version="1.0"?>                                                 
         <jboss>
           <enterprise-beans>
             <session>
               <ejb-name>TravelAgentBean</ejb-name>
               <ejb-ref>
                 <ejb-ref-name>ejb/CabinHome</ejb-ref-name>
                 <jndi-name>CabinBean</jndi-name>
               </ejb-ref>
             </session>
           </enterprise-beans>
         </jboss>
         

    You don't jave to change anything in ejb-jar.xml. You can now use the following script jbossMakeIt.sh to compile and deploy:

     
         

         #!/bin/sh                                                             

         # make cabin bean

         javac -classpath $JBOSS_HOME/lib/ext/ejb.jar:. \
           com/titan/cabin/Cabin*.java

         cp com/titan/cabin/ejb-jar.xml META-INF/ejb-jar.xml
         jar cvf cabin.jar com/titan/cabin/Cabin*.class META-INF/ejb-jar.xml


         # make travelagent bean

         javac -classpath $JBOSS_HOME/lib/ext/ejb.jar:. \
           com/titan/travelagent/TravelAgent*.java

         cp com/titan/travelagent/ejb-jar.xml \
            com/titan/travelagent/jboss.xml \
            META-INF/

         # JBoss needs the Home, Remote and primary key (PK) classes
         # of the Cabin in travelagent.jar so that TravelAgent*.class
         # can access the Cabin bean

         jar cvf travelagent.jar \
           com/titan/cabin/CabinHome.class \
           com/titan/cabin/Cabin.class \
           com/titan/cabin/CabinPK.class \
           com/titan/travelagent/TravelAgent*.class \
           META-INF/ejb-jar.xml META-INF/jboss.xml

         rm -f META-INF/ejb-jar.xml
         rm -f META-INF/jboss.xml

         # deploy
         cp cabin.jar travelagent.jar $JBOSS_HOME/deploy
         

  • Compile the clients.

    The clients from the examples perform a lookup on the java:comp/env namespace, which is not currently supported by JBoss for the clients. You have to make the following edits: (CabinBean is the jndi name under which the Cabin Bean is deployed, see the jndi howto) In com/titan/cabin/Client_1.java replace

     
         Object obj = jndiContext.lookup("java:comp/env/ejb/CabinHome");
    by
     
         Object obj = jndiContext.lookup("CabinBean");
    In com/titan/cabin/Client_2.java replace
     
         Object obj = jndiContext.lookup("ejb/CabinHome");
    by
     
         Object obj = jndiContext.lookup("CabinBean");
    In com/titan/travelagent/Client_1.java replace
     
         Object obj = jndiContext.lookup("ejb/TravelAgentHome");
    by
     
         Object obj = jndiContext.lookup("TravelAgentBean");
    You can now use jBossMakeClients.sh to compile:
         
         #!/bin/sh                                                             

         javac -classpath $JBOSS_HOME/lib/ext/ejb.jar:.  \
             com/titan/cabin/Client*.java                \
             com/titan/travelagent/Client*.java

         
         

  • Run the clients

    We don't use Sun's RI runclient tool, so RunIt.sh won't work. Instead, we provide the following script: jBossRunClient.sh. This file includes all the jBoss libraries needed in the classpath.

         
         #!/bin/sh                                                             

         CP=$JBOSS_HOME/client/ejb.jar
         CP=$CP:$JBOSS_HOME/client/jndi.jar
         CP=$CP:$JBOSS_HOME/client/jta-spec1_0_1.jar
         CP=$CP:$JBOSS_HOME/client/jboss-client.jar
         CP=$CP:$JBOSS_HOME/client/jnp-client.jar

         CP=$CP:.

         java -cp $CP $1
         
         
    You also have to set the jndi properties to connect to the server. This is done in the jndi.properties file (this file must be in the same directory as jBossRunClient.sh)
                                                                           
         java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
         java.naming.provider.url=localhost
         java.naming.factory.url.pkgs=org.jboss.naming;
         
    You can now run the clients. The script take the name of the client as an argument, try
     
         
         ./jBossRunClient.sh com.titan.cabin.Client_1
         ./jBossRunClient.sh com.titan.cabin.Client_2
         ./jBossRunClient.sh com.titan.travelagent.Client_1
         

    NOTES: the clients will only run once, since they use the EJBHome.create() method: at second run, a DuplicateKeyException will occur. I recommend you to turn off debug logging for these examples. Edit $JBOSS_HOME/conf/jboss.conf, in the ConsoleLogging section, set the first ARG to "Error".