Home | News | FAQ | Download | Lists | Issues | ||||||||
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 exampleHere are some basic steps you need to perform to get started with OpenEJB
Download and install OpenEJBYou can download OpenEJB from 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. If you look under this directory, you will find a few more directories:
Setup your development environmentCreate a working directoryAssuming you are in your home directory, create a directory named projects karan@poweredge:~$ mkdir projects Go to the projects directory karan@poweredge:~$ cd projects We will do all our work in this directory. Install JavaDownload and install Java (version 5 or higher). Also set it up so that you can run the java and javac commands from any directory Set OPENEJB_HOMEWe will setup this variable to refer to the openejb install location. karan@poweredge:~/projects$ export OPENEJB_HOME=/home/karan/install/openejb-3.0 Write an EJBWhatever files you create should be placed under the projects directory Create the Remote InterfaceUsing your favorite editor, create a file named Hello.java (shown below) package org.acme; import javax.ejb.Remote; @Remote public interface Hello{ public String sayHello(); } Create the Bean ClassNow create a file named HelloBean.java (shown below) package org.acme; import javax.ejb.Stateless; @Stateless public class HelloBean implements Hello{ public String sayHello(){ return "Hello World!!!!"; } } Compile the source codeSince 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/javaee-5.0-1.jar. karan@poweredge:~/projects$ javac -cp $OPENEJB_HOME/lib/javaee-5.0-1.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. Package the EJBTo package the EJB into a JAR, run the following command while you are in the projects directory karan@poweredge:~/projects$ jar cvf hello.jar org The above command will package everything under the org directory (including the org directory itself) into a jar file named hello.jar. Below is the output from running the above command: karan@poweredge:~/projects$ jar cvf hello.jar org added manifest adding: org/(in = 0) (out= 0)(stored 0%) adding: org/acme/(in = 0) (out= 0)(stored 0%) adding: org/acme/Hello.class(in = 203) (out= 168)(deflated 17%) adding: org/acme/HelloBean.class(in = 383) (out= 275)(deflated 28%) Write an EJB ClientNow we will write a Client class which will lookup the EJB , invoke the sayHello() business method and print the value returned from the method. package org.acme; import java.util.Properties; import javax.naming.InitialContext; import javax.naming.Context; import javax.rmi.PortableRemoteObject; public class HelloClient{ public static void main(String[] args) throws Exception{ Properties props = new Properties(); props.put(Context.INITIAL_CONTEXT_FACTORY,"org.apache.openejb.client.RemoteInitialContextFactory"); props.put(Context.PROVIDER_URL,"ejbd://127.0.0.1:4201"); Context ctx = new InitialContext(props); Object ref = ctx.lookup("HelloBeanRemote"); Hello h = (Hello)PortableRemoteObject.narrow(ref,Hello.class); String result = h.sayHello(); System.out.println(result); } } Compile HelloClient.javaRun the following command: karan@poweredge:~/projects$ javac -d . HelloClient.java Start the ServerGo to the OpenEJB install directory (i.e. OPENEJB_HOME) and run the following command: karan@poweredge:~/install/openejb-3.0$ bin/openejb start Once the Server starts, you will see an output similar to the below in your console:
karan@poweredge:~/install/openejb-3.0$ bin/openejb start
Apache OpenEJB 3.0 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!
Take out a minute to browse through the conf and logs directories. You should now see some configuration and log files under the respective directories. Deploy the EJBWe will now use the deploy command to deploy the EJB in hello.jar. While you are in the projects directory, run the following command: karan@poweredge:~/projects$ $OPENEJB_HOME/bin/openejb deploy hello.jar The above command should give you the following output:
karan@poweredge:~/projects$ $OPENEJB_HOME/bin/openejb deploy hello.jar
Application deployed successfully at "hello.jar"
App(id=/home/karan/projects/hello.jar)
EjbJar(id=hello.jar, path=/home/karan/projects/hello.jar)
Ejb(ejb-name=HelloBean, id=HelloBean)
Jndi(name=HelloBeanRemote)
Notice how the output neatly lays out various deployment details. One thing you might want to note from the output is the JNDI name. This is the JNDI name we used in the client to lookup the EJB Run the ClientWhile you are in the projects directory, run the following command to run the client: karan@poweredge:~/projects$ java -cp $OPENEJB_HOME/lib/openejb-client-3.0.jar:$OPENEJB_HOME/lib/javaee-5.0-1.jar:. org.acme.HelloClient The above should give you the following output: Hello World!!!! Help! , it didn't work for me!!.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. |
||||||||
|