Using Tomcat, OpenEJB, and MySQL run the session and CMP entity bean
example from Goodwill's Chpt12.
Source Code: Developing Java Servlets Second Edition (SAMS 2001):
http://www.virtuas.com/books/source.zip
Prerequisites:
J2SDK 1.4.2
(SDK not JRE) http://java.sun.com/j2se/1.4.2/download.html
Set system environment variable JAVA_HOME
to point to installation directory
Tomcat 5.x: http://jakarta.apache.org/site/binindex.cgi
Set system environment variable TOMCAT_HOME
to point to installation directory
OpenEJB: http://www.openejb.org/download.html
NOTE: Copy lib/ejb-2.0.jar
to TOMCAT_HOME/common/lib folder for access to all web apps.
Suggested Database for entity beans: (feel free to use other databases)
MySQL 4.x: http://www.mysql.com/downloads/mysql-4.0.html
MySQL JDBC Driver:
http://www.mysql.com/downloads/api-jdbc-stable.html
NOTE: Copy the driver
mysql-connector-java-3.x.x-stable-bin.jar to TOMCAT_HOME\common\lib
folder for access to all web apps.
Optional:
Eclipse http://eclipse.org and database plugin: http://jfacedbc.sf.net
Step by Step How To: (Note: Eclipse users see the Back to Basics article)
1) Download source
code and create 2 projects.
a)
Copy the /Writings/DJS2/Chapter 12/src files into two projects, the
server side EJB project and the client project.
In these instructions I will use /chpt12-ejb
as the EJB project and /chpt12
as the client project (web application). Please note where all
the files are to be placed below.
Project Hierarchy Reference: Projects should eventually
look as follows: (the additional files will be covered later).
chpt12-ejb/ src/org/nycjava/*.java (exclude EJBTestServlet.java - move it to client project) META-INF/ejb-jar.xml META-INF/openejb-jar.xml org/nycjava/*.class quote.sql
/chpt12/ src/org/nycjava/EJBTestServlet.java WEB-INF/ /classes/org/nycjava/EJBTestServlet.class /web.xml index.jsp /META-INF/context.xml
Note: we don't need
Quote.properties so you can delete it
b)
Add a Java package name org.nycjava
to the source code files
We will use a named Java package
instead of
using a
default Java package. Add the
package statements to each file. (Eclipse: use Refactor->Move)
2) Add the
following jar files to your classpath to compile the source code:
TOMCAT_HOME/common/lib/ejb-2.0.jar
and
TOMCAT_HOME/common/lib/servlet-api.jar
(note from prerequisite: you should
have copied ejb-2.0.jar from OpenEJB's lib folder to Tomcat's
common/lib)
3) Edit ejb-jar.xml and add the package names to the home, remote, ejb class and prim key class
tags.
Note: Correct the typo in the quote bean <entity> section: the field, "nterestRate" should read "interestRate" also add:
<primkey-field>id</primkey-field>
4) Add the
following missing methods to the QuoteBean (due to EJB spec requirement
since the publication of the book).
public void
ejbPostCreate(Integer d) { }
public void
ejbPostCreate(Integer d, String name) { }
5)
Compile and build the ejb jar file from the chpt12-ejb folder.
(Note: add JAVA_HOME/bin to your run path so that you can use the javac and jar commands from command line)
If compiling from the command line:
javac -classpath
%TOMCAT_HOME%\common\lib\ejb-2.0.jar;%TOMCAT_HOME%\common\lib\servlet.jar
-d . src\org\nycjava\*.java
the -d . will place your class
files in the current directory and not the src folder. (we'll not
deploy source files).
To create the jar file: (note how one lists the folders to
include into the jar file as arguments following the jar name)
jar cvf chpt12Ejb.jar
META-INF org
6) Deploy your EJB
to OpenEJB. From the openejb folder:
openejb.bat
deploy -a -m "c:\chpt12-ejb\chpt12Ejb.jar"
-m Moves jar (and adds openejb-jar.xml descriptor file into
the jar) to the OPENEJB_HOME/beans directory
-a Automates deployment as
much as possible (use openejb help
for all commands)
When prompted for an OQL statement for the CMP bean:
SELECT
id FROM quote WHERE name = $1
Congratulations you've
created and deployed your EJB jar file.
7) Install the MySQL database server
and create the database for this example. (Eclipse users can use the JFaceDbc plugin
- see Basics article)
Use the utilities included in the bin folder of the mysql installation
to monitor and start the database:
c:\mysql\bin\winmysqladmin.exe
- starts mysql server and provides a tray icon to stop and start
the mysql service.
alternatively you can start the server from the command line
using either mysqld
(linux and MacOSX users should use this) or mysqld-max-nt (recommended
for WinNT and above - this is what the above command runs).
c:\mysql\bin\mysql.exe
- this is the monitor
client console where you can execute SQL commands (same for all
platforms)..
a) Start
the database.server.
b) From the
mysql monitor
prompt run the following script to create the database for this example:
>mysql -uroot -p[password] (if you use a password)
mysql> source
c:\chpt12-ejb\quote.sql
(ignore the access errors if any)
c) To view
the database you just created: (remember
that all SQL statements must end in semicolon)
mysql> use goodwill;
mysql> show tables;
mysql> explain quote;
8) Configure
OpenEJB to use our MySQL database for CMP.
a) Edit openejb.conf (found in OpenEJB's conf folder) and
comment out or delete the container id section named <Container id="Default CMP Container" ctype="CMP_ENTITY">
(and everything within the enclosing tags).
Replace it with the following and add the additional sections:
(note: use the correct
location of your openejb installation)
<Container id="Default CMP Container"
ctype="CMP_ENTITY">
Global_TX_Database
c:/openejb/conf/mysql.cmp_global_database.xml
Local_TX_Database
c:/openejb/conf/mysql.cmp_local_database.xml
</Container>
<Connector id="MySQL Database">
JdbcDriver
com.mysql.jdbc.Driver
JdbcUrl
jdbc:mysql://localhost/goodwill
UserName
goodwill
Password
goodwill
</Connector>
<Deployments
jar="beans/chpt12Ejb.jar"/>
b)
Add the following files to the OpenEJB conf folder:
(this provides the MySQL to CMP mappings)
mysql.cmp_global_database.xml
mysql.cmp_local_database.xml
mysql.cmp_mapping.xml
9) Create the
META-INF/context.xml file with the following context:
(we
need this so we can lookup our beans from our client webapp)
10) Now we create
the client but first we need to edit
EJBTestServlet.java to change the JNDI lookups to use the local openejb
server:
a) Use org.openejb.client.LocalInitialContextFactory
as
the context factory and comment out the line referencing a provider URL
(note we're using a local and not a remote factory in this example
because we are running OpenEjb as a Tomcat webapp. If running
separately use)
b) Use the
following lookups:
Object
obj= ctx.lookup("java:comp/env/ejb/calculateloan");
...
Object
obj= ctx.lookup("java:comp/env/ejb/quote");
11) Add the servlet and the ejb references to web.xml.
12) Deploy the OpenEJB EJB Container by copying
openejb-0.9.2/dist/openejb_loader-0.9.2.war toTomcat's webapps
folder and starting Tomcat (either before or after copying the war
file).
Note: check that the webapps/openejb_loader-0.9.2/WEB-INF/web.xml's
openejb.home init parameter
matches your OpenEJB installation folder. This not an issue if you didn't
rename the openejb installation folder, otherwise change it in
the above web.xml and
restart Tomcat.
Test the deployment by using the OpenEJB and Tomcat Integration
interface: http://localhost:8080/openejb_loader-0.9.2
13) Test
EJBTestServlet. Note: your
webapps/chpt12/WEB-INF/classes folder should only contain one file: the
EJBTestServlet class file.
a) Create a
chpt12.war file and deploy it to Tomcat by copying to it to the webapps
folder. Note to avoid deploying see the Basics article.
b) Run the EJBTestServlet as a URL (or create an index.jsp):
http://localhost:8080/chpt12/EJBTestServlet?months=4&principal=10.0&name=goodwill&phone=555-5555
c) To test the
quote bean, view the database to see if there is a new
entry. (Eclipse: use the database plugin to view database)
mysql> use goodwill;
mysql> select * from quote;
Additional Notes:
Since
we are using OpenEjb as an embedded ejb container, we only
need the EJBTestServlet.class in the WEB-INF folder of the client web
application. Normally (as with most app servers) we also need the
remote and home interfaces of
the beans but not the beans themselves. You can test this by using the
renote container (i.e. run OpenEJB not as an embedded container but as
a standalone app server).
In our example:
- The remote interfaces are
Quote.java and CalculateLoan.java.
- The home interfaces are
QuoteHome.java and CalculateLoanHome.java.
- The EJBs are QuoteBean.java and
CalculateLoanBean.java.
How does OpenEJB run inside Tomcat? This diagram
illustrates how OpenEJB runs as a servlet that proxies requests to the
embedded instance of the OpenEJB container. Both OpenEJB (the ejb
container) and Tomcat (the web container) run in the same Java VM
instance.
|