This page shows the basic steps required to create, build, and run an EJB and EJB client in its most minimum form. It does not hide steps or rely on special build tools or IDEs and is about the most stripped down you can get.
See the Examples page for a full list of examples that range from @Stateles and @Stateful beans, to Dependency Injection, JDBC DataSources, JPA EntityManagers and more.
A basic EJB example
Here are some basic steps you need to perform to get started with OpenEJB 1.
- Download and install OpenEJB
...
- Setup your development environment
...
- Write an EJB
...
- Write an EJB client
...
- Start the server
...
- Deploy the EJB
...
- Run the client
...
- Stop the server
Download and install OpenEJB
You can download OpenEJB from This example pertains to OpenEJB 3.0 which can be downloaded here. Once you have downloaded OpenEJB, you can then simply extract the contents of the downloaded file to whichever directory you want to install OpenEJB in.
After extracting the file contents, you should now see a directory named openejb-3.0-beta-1. If you look under this directory, you will find a few more directories: -
- bin: Contains commands to start/stop the server (You can also do a lot of other stuff like deploy/undeploy, but we will just talk about things needed to get you started)
...
- lib: Contains several jar files (you only need of few of these jars in your classpath to do EJB development)
...
- apps: Once you create your EJB's and jar them up, you can place your jar file in this directory and start the server. The server will automatically deploy all the EJB's contained in this JAR.
...
- conf: This directory contains all the configuration files. Although you may not see any file except for a README.txt file right now, but after you start the server, the required configuration files will be automatically created. It is highly recommeded to read the README.txt file under this directory
...
- logs: Contains log files.
Setup your development environment
...
Code Block |
---|
karan@poweredge:~/projects$ export OPENEJB_HOME=/home/karan/install/openejb-3.0-beta-1
|
Write an EJB
Whatever files you create should be placed under the projects directory
...
Since we have imported the javax.ejb.Stateless and javax.ejb.Remote annotations, we need these in our classpath to compile our source code. These annotations can be found in the $OPENEJB_HOME/lib/geronimojavaee-ejb_35.0_spec-1.0.jar.
Lets compile our source (make sure you are in the projects directory)
Code Block |
---|
karan@poweredge:~/projects$ javac -cp $OPENEJB_HOME/lib/geronimojavaee-ejb_35.0_spec-1.0.jar -d . *.java |
The above will compile all the .java files and also create the required packages. You should now see a package named org under the projects directory. All class files should be under org/acme directory.
...
Code Block |
---|
karan@poweredge:~/install/openejb-3.0-beta-1$0$ bin/openejb start |
Once the Server starts, you will see an output similar to the below in your console:
Code Block |
---|
karan@poweredge:~/install/openejb-3.0-beta-1$0$ bin/openejb start Apache OpenEJB 3.0-beta-1 build: 20070926-12:34 http://openejb.apache.org/ OpenEJB ready. [OPENEJB:init] OpenEJB Remote Server ** Starting Services ** NAME IP PORT httpejbd 0.0.0.0 4204 telnet 0.0.0.0 4202 ejbd 0.0.0.0 4201 hsql 0.0.0.0 9001 admin thread 0.0.0.0 4200 ------- Ready! |
...
Code Block |
---|
karan@poweredge:~/projects$ java -cp $OPENEJB_HOME/lib/openejb-client-3.0-beta-1.jar:$OPENEJB_HOME/lib/geronimojavaee-ejb_35.0_spec-1.0.jar:. org.acme.HelloClient |
...
No problem, we are here to help. Just send us an email at users@openejb.apache.org. If possible, send us the contents of logs/openejb.log file in the email.
Looking for more?
More EJB 3.0 examples, sample applications, tutorials and howtos available here.