In this exercise, you will create and build the TravelAgent EJB. This simple bean illustrates the use of a stateless session bean and mirrors the code shown in Chapter 4 of the EJB section of this book.
If you already have JBoss running, there is no reason to restart it. Otherwise, start it up as instructed in the JBoss Installation and Configuration chapter.
The database should contain the 100 rows created by a successful
execution of the test programs from the previous exercise,
Client_1
and Client_2.
Perform the following steps:
Open a command prompt or shell terminal and change to the
ex04_2
directory created by the extraction
process.
Set the JAVA_HOME
and
JBOSS_HOME
environment variables to point to where
your JDK and JBoss 4.0 are installed. Examples:
Windows:
C:workbookex04_2> set JAVA_HOME=C:jdk1.4.2 C:workbookex04_2> set JBOSS_HOME=C:jboss-4.0
|
Unix:
$ export JAVA_HOME=/usr/local/jdk1.4.2 $ export JBOSS_HOME=/usr/local/jboss-4.0
|
Add ant
to your execution path. Ant is the build
utility.
Windows:
C:workbookex04_2> set PATH=..antin;%PATH%
|
Unix:
$ export PATH=../ant/bin:$PATH
|
Perform the build by typing ant
.
As in the last exercise, you will see titan.jar
rebuilt, copied to the JBoss deploy
directory,
and redeployed by the application server.
In this example, the jboss.xml
deployment
descriptor overrides the default JNDI binding for the deployed EJBs.
CabinEJB
is bound to
CabinHomeRemote
and
TravelAgentEJB
is bound to
TravelAgentHomeRemote
.
<jboss> <enterprise-beans> <entity> <ejb-name>CabinEJB
</ejb-name> <jndi-name>CabinHomeRemote
</jndi-name> </entity> <session> <ejb-name>TravelAgentEJB
</ejb-name> <jndi-name>TravelAgentHomeRemote
</jndi-name> <ejb-ref> <ejb-ref-name>ejb/CabinHomeRemote</ejb-ref-name> <jndi-name>CabinHomeRemote</jndi-name> </ejb-ref> </session> </enterprise-beans> </jboss>
The EJB book describes how you must use
<ejb-ref>
declarations when one EJB
references another. The TravelAgent EJB references the Cabin entity
bean, so the following XML is required in
ejb-jar.xml
.
<ejb-ref>
<ejb-ref-name>ejb/CabinHomeRemote
</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<home>com.titan.cabin.CabinHomeRemote</home>
<remote>com.titan.cabin.CabinRemote</remote>
</ejb-ref>
If you have a
<ejb-ref-name>
declared in your
ejb-jar.xml
file, you must have a corresponding
<ejb-ref>
declaration in your
jboss.xml
file that maps the portable JNDI name
used by the TravelAgent EJB to the real JNDI name of the Cabin EJB.
<jboss> <enterprise-beans> <entity> <ejb-name>CabinEJB
</ejb-name> <jndi-name>CabinHomeRemote
</jndi-name> </entity> <session> <ejb-name>TravelAgentEJB
</ejb-name> <jndi-name>TravelAgentHomeRemote
</jndi-name> <ejb-ref> <ejb-ref-name>ejb/CabinHomeRemote</ejb-ref-name> <jndi-name>CabinHomeRemote</jndi-name> </ejb-ref> </session> </enterprise-beans> </jboss>
The example program in this section invokes the TravelAgent EJB to list cabins that meet certain criteria.
... Context jndiContext = getInitialContext( ); Object ref = jndiContext.lookup("TravelAgentHomeRemote"); TravelAgentHomeRemote home = (TravelAgentHomeRemote) PortableRemoteObject.narrow(ref,TravelAgentHomeRemote.class); TravelAgentRemote travelAgent = home.create( ); // Get a list of all cabins on ship 1 with a bed count of 3. String list [] = travelAgent.listCabins(SHIP_ID,BED_COUNT); for(int i = 0; i < list.length; i++) { System.out.println(list[i]); } ...
The client code does a JNDI lookup for the TravelAgent home and does
a simple create( )
method invocation to obtain a
reference to a TravelAgent EJB. The client then calls
listCabins( )
and receives a list of cabin names
that meet the provided criteria.
Let’s examine a little of the code in
TravelAgent EJB’s
listCabins( )
method to see how it works.
public String [] listCabins(int shipID, int bedCount) { try { javax.naming.Context jndiContext = new InitialContext( ); Object obj = jndiContext.lookup("java:comp/env/ejb/CabinHomeRemote"); CabinHomeRemote home = ...
When a deployed EJB in JBoss wants to access JNDI, all
that’s needed is a simple new InitialContext( )
. JBoss will automatically create an
optimized, in-process reference to the JNDI server running inside the
application server, to avoid the overhead of a distributed network
call when accessing it. The rest of listCabins( )
is pretty straightforward, so you can just go on to running the
client application.
Run the Client_3
application by invoking
ant
run.client_42
at the
command prompt. Remember to set your JBOSS_HOME
and PATH
environment variables.
The output of Client_3
should look something
like this:
C:workbookex04_2>ant run.client_42 Buildfile: build.xml prepare: compile: ejbjar: run.client_42: [java] 1,Master Suite,1 [java] 3,Suite 101,1 [java] 5,Suite 103,1 [java] 7,Suite 105,1 [java] 9,Suite 107,1 [java] 12,Suite 201,2 [java] 14,Suite 203,2 [java] 16,Suite 205,2 [java] 18,Suite 207,2 [java] 20,Suite 209,2 [java] 22,Suite 301,3 [java] 24,Suite 303,3 [java] 26,Suite 305,3 [java] 28,Suite 307,3 [java] 30,Suite 309,3