ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

OpenEJB: EJB for Tomcat

by Jacek Laskowski

There has probably been a day in your life when you've asked yourself if your web application might use other J2EE concepts besides Java servlets. Your dilemma has most likely been "Although I could read the specifications, I don't have time to set up the containers and enhance the application."

In most cases, that's what keeps people away from leveraging J2EE technologies in their applications. It happens with other architectures, too. However, this doesn't have to last forever, as there are tools to make your life easier. OpenEJB is one--you can run it with no problems after having spent a few minutes reading a quick introduction.

Before we delve into the topic, I'd like to mention that you can find out more details about OpenEJB at the OpenEJB home page.

OpenEJB allows you to use EJBs in your applications with very few configuration files and commands. After reading this article, you will be able to use EJB as you might have been using JDBC with databases like HSQL or Cloudscape. You won't have to spend a lot of your precious time to configure and run an application server. Merely deploy a simple web application from OpenEJB distribution and map the beans deployed in OpenEJB to their respective names in Tomcat's naming system.

Related Reading

Enterprise JavaBeans
By Richard Monson-Haefel

OpenEJB can run in two server modes--local (AKA IntraVM) and remote. The main difference between them is the address space (or JVM) in which they run. OpenEJB Local Server runs in the same JVM as the application that uses it. On the other hand, OpenEJB Remote Server runs within its own JVM, probably on a different server than the client application.

The aim of this article is to present an easy yet powerful way of combining Tomcat with OpenEJB so that a web application deployed in Tomcat becomes an OpenEJB client in a fully J2EE-compliant manner. You shall see an EJB example deployed into OpenEJB and accessed by a JSP as part of a web application deployed into Tomcat. I don't want you write the EJB and web application, thus the article provides these components in a downloadable form. We'll concentrate on architecture instead.

I think you'll be amazed at how fast and easy is to enhance your web application with OpenEJB. Home and remote interfaces, stubs, and skeletons will not frighten you anymore!

Getting and Installing OpenEJB

Needless to say, before we start working with OpenEJB, it needs to be installed on your system. Download OpenEJB. Be sure to define to define the OPENEJB_HOME environment variable to point to the directory where OpenEJB is installed. After that, let's try it out and see how OpenEJB responds.

> ./openejb.sh start  
--------------SUPPORT INFO-------------
HP-UX B.11.00 A
Using JAVA_HOME:     /opt/java1.4
Using OPENEJB_HOME:  /disk/home/jacekl/artykul/openejb-0.9.1
OpenEJB Remote Server 0.9.1    build: 20030118-2102
[init] OpenEJB Container System
[init] OpenEJB Remote Server
  ** Starting Services **
  NAME             IP              PORT
  ejb server       4201
  admin console       4200
To log into the admin console, telnet to:
 telnet 4200

This means everything is indeed "Ready" to go. Press Ctrl-C to stop the session.

Deploying Hello World EJB

The code outlined here will not differ in any way from what you might have seen at the OpenEJB pages. It's a simple stateless session bean with a business method--sayHello(). The JSP will call this method. Download the bean's .jar.

Deploying the bean is easy. Invoke the following command under Cygin or any other Unix-like system:

> ./openejb deploy -a -f -c <the jar location>

The options given on the command line above are to automate deployment as much as possible (the -a option) and forcefully (the -f option) and to copy (the -c option) the .jar to the OPENEJB_HOME/beans directory, overwriting any previously existing .jar with the same name.

Run the following command to get acquainted with the deploy options:

> ./openejb.sh deploy

When deploying the Hello bean, you shall see the following output:

> ./openejb.sh deploy -a -f -c ~/myHelloEjb.jar 
--------------SUPPORT INFO-------------
HP-UX B.11.00 A
Using JAVA_HOME:     /opt/java1.4
Using OPENEJB_HOME:  /disk/home/jacekl/artykul/openejb-0.9.1
OpenEJB Deploy Tool 0.9.1    build: 20030118-2102

This jar contains the following beans:

Deploying bean: Hello

==--- Step 1 ---==

Auto assigning the ejb-name as the deployment id for this bean.

Deployment ID: Hello
==--- Step 2 ---==

Auto assigning the container the bean will run in.

Container: Default Stateless Container

Done collecting deployment information!
Creating the openejb-jar.xml file...done
Writing openejb-jar.xml to the jar...done

Congratulations! Your jar is ready to use with OpenEJB.

If the OpenEJB remote server is already running, you will
need to restart it in order for OpenEJB to recognize your bean.

NOTE: If you move or rename your jar file, you will have to
update the path in this jar's deployment entry in your 
OpenEJB config file. 

Pages: 1, 2

Next Pagearrow