|
Page Edited:
OPENEJB :
Hello World
Hello World has been edited by Miroslav Rudisin (May 03, 2006). Change summary: update compile warnings for 1.0 version A basic EJB example Before startingWindows C:\my\app> RunIt Linux/Unix [[EMAIL PROTECTED] app]# ./RunIt.sh If you ran into any problems, first check your openejb.log file at c:\openejb\openejb.log. Look for any lines that begin with ** , ** , or ** . If the log file doesn't help you, email it to the OpenEJB user mailing list and let people know you are using the This example assumes you have already downloaded and installed OpenEJB in the directory c:\openejb. Refer to the Quickstart guide if you haven't yet installed OpenEJB. We also assume that you are running your client from the directory c:\my\app. The classes and interfacesCreate the package where we will place our ejb and application files, let's say "c:\my\app" for our example. Windows
Linux/Unix
Create the files below in our new "c:\my\app\org\acme\" directory Create the bean classHelloBean.java package org.acme; import java.rmi.RemoteException; import javax.ejb.*; public class HelloBean implements SessionBean { private SessionContext sessionContext; public void ejbCreate() { } public void ejbRemove() { } public void ejbActivate() { } public void ejbPassivate() { } public void setSessionContext(SessionContext sessionContext) { this.sessionContext = sessionContext; } public String sayHello() throws java.rmi.RemoteException { return "Hello World!!!!!"; } } Create the EJB Home interfaceHelloHome.java package org.acme; import java.rmi.*; import javax.ejb.*; import java.util.*; public interface HelloHome extends EJBHome { public HelloObject create() throws RemoteException, CreateException; } Create the EJB Object interfaceHelloObject.java package org.acme; import java.rmi.*; import javax.ejb.*; import java.util.*; public interface HelloObject extends EJBObject { public String sayHello() throws RemoteException; } The deployment descriptorThe deployment descriptor tells the EJB Container how to put together the classes above and run your component. Windows
Linux/Unix
Now, create an ejb-jar.xml file in your META-INF directory. ejb-jar.xml <?xml version="1.0" encoding="UTF-8"?> <ejb-jar> <enterprise-beans> <session> <ejb-name>Hello</ejb-name> <home>org.acme.HelloHome</home> <remote>org.acme.HelloObject</remote> <ejb-class>org.acme.HelloBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> <assembly-descriptor> <container-transaction> <method> <ejb-name>Hello</ejb-name> <method-name>*</method-name> </method> <trans-attribute>Required</trans-attribute> </container-transaction> </assembly-descriptor> </ejb-jar> Compile the EJBCompile your bean. Windows
Linux/Unix
Make sure you don't make the mistake of trying to compile your classes while sitting inside the org/acme/ directory.
Package the EJBNow, package your EJB classes and your META-INF directory into a jar. Windows
Linux/Unix
That command should give you output like the following. added manifest adding: org/(in = 0) (out= 0)(stored 0%) adding: org/acme/(in = 0) (out= 0)(stored 0%) adding: org/acme/HelloBean.class(in = 946) (out= 467)(deflated 50%) adding: org/acme/HelloObject.class(in = 234) (out= 177)(deflated 24%) adding: org/acme/HelloHome.class(in = 263) (out= 188)(deflated 28%) ignoring entry META-INF/ ignoring entry META-INF/MANIFEST.MF adding: META-INF/ejb-jar.xml(in = 733) (out= 319)(deflated 56%) Check to make sure at least the three classes are there and the ejb-jar.xml is there and that everything is in the directories you see above. Deploy the EJB jarUse the OpenEJB Deploy Tool to deploy your jar. Windows
Linux/Unix
A basic client applicationCreate a basic client application to access your HelloWorld bean. HelloWorld.java package org.acme; import javax.rmi.*; import javax.naming.*; import java.util.*; public class HelloWorld { public static void main( String args[]) { try{ Properties p = new Properties();p.put("java.naming.factory.initial", "org.openejb.client.RemoteInitialContextFactory"); p.put("java.naming.provider.url", "127.0.0.1:4201"); p.put("java.naming.security.principal", "myuser"); p.put("java.naming.security.credentials", "mypass"); InitialContext ctx = new InitialContext( p ); Object obj = ctx.lookup("/Hello"); HelloHome ejbHome = (HelloHome) PortableRemoteObject.narrow(obj,HelloHome.class); HelloObject ejbObject = ejbHome.create(); String message = ejbObject.sayHello(); System.out.println( message ); } catch (Exception e){ e.printStackTrace(); } } } JNDI properties for the Local Server would look like the following. Be sure to read the Local Server documentation if you run into any problems. Properties p = new Properties(); p.put("java.naming.factory.initial", "org.openejb.client.LocalInitialContextFactory"); p.put("openejb.home", "c:\\openejb"); InitialContext ctx = new InitialContext(p); JNDI properties for the Remote Server would look like the following. Be sure to start the Remote Server before running your application. See the Remote Server documentation for more information on using the Remote Server. Properties p = new Properties(); p.put("java.naming.factory.initial", "org.openejb.client.RemoteInitialContextFactory"); p.put("java.naming.provider.url", "127.0.0.1:4201"); p.put("java.naming.security.principal", "myuser"); p.put("java.naming.security.credentials", "mypass"); InitialContext ctx = new InitialContext(p); Update the HelloWorld.java to contain the right JNDI properties. Compile the applicationCompile your client code. Don't forget to add your EJBs in the classpath! Windows
Linux/Unix
Run it!When you run OpenEJB in embedded server mode, you need all the server libraries in your classpath along with your beans and client code. Here is a simple script that will add those classes automactically. Feel free to use this script, or add it's contents to you own scripts. Example Windows Batch script. RunIt.bat @echo off set OPENEJB_HOME=C:\openejb set PATH=%PATH%;%OPENEJB_HOME%\bin set JAVA=%JAVA_HOME%\bin\java set CP= for %%i in (%OPENEJB_HOME%\lib\*.jar) do call cp.bat %%i for %%i in (%OPENEJB_HOME%\dist\*.jar) do call cp.bat %%i for %%i in (%OPENEJB_HOME%\beans\*.jar) do call cp.bat %%i set CLASSPATH=%JAVA_HOME%\lib\tools.jar;%CP% %JAVA% %OPTIONS% -Dopenejb.home=%OPENEJB_HOME% org.acme.HelloWorld Example Linux/Unix Batch script. RunIt.sh #!/bin/sh
# Set OPENEJB_HOME to the full path where you
# installed your OpenEJB distribution
export OPENEJB_HOME=/openejb
# Set JAVA_HOME to the full path where you
# installed your JDK distribution
export JAVA_HOME=/usr/java/jdk1.3.1
export PATH=${PATH}:${OPENEJB_HOME}/bin
export JAVA=${JAVA_HOME}/bin/java
export CP=
CP=`echo $OPENEJB_HOME/lib/*.jar | tr ' ' ':'`:${CP}
CP=`echo $OPENEJB_HOME/dist/*.jar | tr ' ' ':'`:${CP}
CP=`echo $OPENEJB_HOME/beans/*.jar | tr ' ' ':'`:${CP}
export CLASSPATH=$JAVA_HOME/lib/tools.jar:${CP}
$JAVA -Dopenejb.home=$OPENEJB_HOME org.acme.HelloWorld
Now run the script! Windows
Linux/Unix
What if it didn't workIf you ran into any problems, first check your openejb.log file at c:\openejb\openejb.log. Look for any lines that begin with ** , ** , or ** . If the log file doesn't help you, email it to the OpenEJB user mailing list and let people know you are using the Hello World example. |
Unsubscribe or edit your notifications preferences
