blob: b9984e12a54c57af910e8bcf6cbe1ae0e61d1b03 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<!-- $PAGETITLE -->
<TITLE>OpenEJB - Hello World</TITLE>
<LINK href="http://openejb.apache.org/all.css" rel="stylesheet" type="text/css">
<!--[if IE]><link rel="stylesheet" type="text/css" media="screen, projection" href="openejb.apache.org/ie.css"><![endif]-->
<LINK rel="SHORTCUT ICON" href="http://openejb.apache.org/images/favicon.ico">
<META http-equiv="Content-Type" content="text/html;charset=UTF-8">
<SCRIPT language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js" type="text/javascript"></SCRIPT>
<SCRIPT language="javascript" src="http://openejb.apache.org/tweet/jquery.tweet.js" type="text/javascript"></SCRIPT>
<SCRIPT type="text/javascript">
$(document).ready(function(){
$(".tweet").tweet({
avatar_size: 32,
count: 4,
fetch:25,
username: "openejb",
list: "contributors",
template:"{avatar}{text}",
filter: function(t){ return /openejb/i.test(t["tweet_raw_text"]); },
loading_text: "loading list..."
});
});
</SCRIPT>
</HEAD>
<BODY>
<!-- Delay the loading of the external javascript file needed for labels (as it takes too long to load and visibly holds loading of the page body) -->
<!-- To do this without javascript errors over undefined functions, we need to declare stubs here (that are overrided later by the proper implementations) -->
<SCRIPT language="JavaScript" type="text/javascript">
function doAddLabel(hideTextfieldAfterAddParam)
{
// stub
}
function onAddLabel()
{
// stub
}
function showLabelsInput()
{
// stub
}
</SCRIPT>
<A name="top"></A>
<TABLE class="frameTable" cellpadding="0" cellspacing="0" border="0">
<TR class="Row1">
<TD class="Col1"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD>
<TD class="Col2"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD>
<TD class="Col3"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD>
<TD class="Col4"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD>
<TD class="Col5"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD>
</TR>
<TR class="Row2">
<TD class="Col1"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD>
<TD class="Col2">&nbsp;</TD>
<TD class="Col3" id="breadcrumbs">
<!-- $TOP_NAV_BAR -->
<A href="index.html" title="Index">Home</A> | <A href="news.html" title="News">News</A> | <A href="faq.html" title="FAQ">FAQ</A> | <A href="download.html" title="Download">Download</A> | <A href="mailing-lists.html" title="Mailing Lists">Lists</A> | <A href="http://issues.apache.org/jira/browse/OPENEJB" class="external-link" rel="nofollow">Issues</A>
</TD>
<TD class="Col4"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD>
<TD class="Col5">&nbsp;</TD>
</TR>
<TR class="Row3">
<TD class="Col1"><IMG alt="" class="Row3Img" id="thinLine" src="http://openejb.apache.org/images/line_sm.gif"></TD>
<TD class="Col2"><IMG alt="" class="Row3Img" src="http://openejb.apache.org/images/dotTrans.gif"></TD>
<TD class="Col3"><IMG alt="" class="Row3Img" src="http://openejb.apache.org/images/dotTrans.gif"></TD>
<TD class="Col4"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD>
<TD class="Col5"><IMG alt="" class="Row3Img" src="http://openejb.apache.org/images/dotTrans.gif"></TD>
</TR>
<TR class="Row4">
<TD class="Col1">
<SPAN id="Navigation">
<H3><A name="Navigation-Overview"></A>Overview</H3>
<UL class="alternate" type="square">
<LI><A href="index.html" title="Index">Home</A></LI>
<LI><A href="news.html" title="News">News</A></LI>
<LI><A href="faq.html" title="FAQ">FAQ</A></LI>
<LI><A href="download.html" title="Download">Download</A></LI>
<LI><A href="../OPENEJBx30/index.html" title="Index">Documentation</A></LI>
<LI><A href="examples.html" title="Examples">Examples</A></LI>
<LI><A href="http://cwiki.apache.org/confluence/display/OPENEJB/Lightening%20Demos" class="external-link" rel="nofollow">Lightning Demos</A></LI>
<LI><A href="mailing-lists.html" title="Mailing Lists">Mailing Lists</A></LI>
<LI><A href="source-code.html" title="Source Code">Source Code</A></LI>
<LI><A href="http://blogs.apache.org/openejb" class="external-link" rel="nofollow">Project Blog</A></LI>
</UL>
<H3><A name="Navigation-Servers"></A>Servers</H3>
<UL class="alternate" type="square">
<LI><A href="local-server.html" title="Local Server">Local</A></LI>
<LI><A href="remote-server.html" title="Remote Server">Remote</A></LI>
</UL>
<H3><A name="Navigation-Integrations"></A>Integrations</H3>
<UL class="alternate" type="square">
<LI><A href="../OPENEJBx30/tomcat.html" title="Tomcat">Tomcat</A></LI>
<LI><A href="geronimo.html" title="Geronimo">Geronimo</A></LI>
<LI><A href="webobjects.html" title="WebObjects">WebObjects</A></LI>
</UL>
<H3><A name="Navigation-Community"></A>Community</H3>
<UL class="alternate" type="square">
<LI><A href="team.html" title="Team">Team</A></LI>
<LI><A href="articles.html" title="Articles">Articles</A></LI>
<LI><A href="http://webchat.freenode.net/?channels=openejb" class="external-link" rel="nofollow">IRC</A></LI>
</UL>
<H3><A name="Navigation-RelatedProjects"></A>Related Projects</H3>
<UL class="alternate" type="square">
<LI><A href="http://activemq.apache.org/" class="external-link" rel="nofollow">ActiveMQ</A></LI>
<LI><A href="http://openjpa.apache.org/" class="external-link" rel="nofollow">OpenJPA</A></LI>
<LI><A href="http://cxf.apache.org/" class="external-link" rel="nofollow">CXF</A></LI>
</UL>
<H3><A name="Navigation-Index"></A>Index</H3>
<UL class="alternate" type="square">
<LI><A href="space-index.html" title="Space Index">Site Index</A></LI>
<LI><A href="../OPENEJBx30/space-index.html" title="Space Index">Doc Index</A></LI>
</UL>
<H3>
<A name="Navigation-Feeds"></A>
Feeds
</H3>
<UL class="feeds">
<LI>
<A href="http://cwiki.apache.org/confluence/spaces/rss.action?key=OPENEJB&newPages=false">
<IMG src="http://openejb.apache.org/images/rss.gif"></A>
<A class="feedsText" href="http://cwiki.apache.org/confluence/spaces/rss.action?key=OPENEJB&newPages=false">Site</A>
</LI>
<LI><A href="http://cwiki.apache.org/confluence/spaces/blogrss.action?key=OPENEJB">
<IMG src="http://openejb.apache.org/images/rss.gif"></A>
<A class="feedsText" href="http://cwiki.apache.org/confluence/spaces/blogrss.action?key=OPENEJB">News</A>
</LI>
</UL>
</SPAN>
</TD>
<TD class="Col2">&nbsp;</TD>
<TD class="Col3">
<TABLE id="PageHeader" border="0" width="100%">
<TR>
<TD>
<A href="http://openejb.org/">
<IMG hspace="0" src="http://openejb.apache.org/images/logo_openejb.gif" vspace="0">
</A>
</TD>
<TD align="right">
<A href="http://www.apache.org/">
<IMG src="http://www.apache.org/images/asf-logo.gif" width="258" height="66">
</A>
</TD>
</TR>
<TR>
<TD id="page_title">
<!-- $TITLE -->
Hello World
</TD>
<TD align="right">
<BR><BR>
<!-- Google CSE Search Box Begins -->
<FORM id="searchbox_010475492895890475512:_t4iqjrgx90" action="http://www.google.com/cse">
<INPUT type="hidden" name="cx" value="010475492895890475512:_t4iqjrgx90">
<INPUT type="hidden" name="cof" value="FORID:0">
<INPUT name="q" type="text" size="25">
<INPUT type="submit" name="sa" value="Search">
</FORM>
<SCRIPT type="text/javascript" src="http://www.google.com/coop/cse/brand?form=searchbox_010475492895890475512:_t4iqjrgx90"></SCRIPT>
<!-- Google CSE Search Box Ends -->
</TD>
</TR>
</TABLE>
<P>
<!-- $BODY -->
<DIV id="PageContent">
<P>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.</P>
<P><EM>See the <A href="../OPENEJBx30/examples.html" title="Examples">Examples</A> page for a full list of examples that range from <A href="../OPENEJBx30/simple-stateless-example.html" title="Simple Stateless Example">@Stateles</A> and <A href="../OPENEJBx30/simple-stateful-example.html" title="Simple Stateful Example">@Stateful</A> beans, to <A href="../OPENEJBx30/injection-of-env-entry-example.html" title="Injection of env-entry Example">Dependency Injection</A>, JDBC <A href="../OPENEJBx30/injection-of-datasource-example.html" title="Injection of DataSource Example">DataSources</A>, JPA <A href="../OPENEJBx30/injection-of-entitymanager-example.html" title="Injection of EntityManager Example">EntityManagers</A> and more.</EM></P>
<H2><A name="HelloWorld-AbasicEJBexample"></A>A basic EJB example</H2>
<P>Here are some basic steps you need to perform to get started with OpenEJB</P>
<OL>
<LI>Download and install OpenEJB</LI>
<LI>Setup your development environment</LI>
<LI>Write an EJB</LI>
<LI>Write an EJB client</LI>
<LI>Start the server</LI>
<LI>Deploy the EJB</LI>
<LI>Run the client</LI>
<LI>Stop the server</LI>
</OL>
<H2><A name="HelloWorld-DownloadandinstallOpenEJB"></A>Download and install OpenEJB</H2>
<P>You can download OpenEJB from <A href="http://openejb.apache.org/download.html" class="external-link" rel="nofollow">here </A>. 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. </P>
<P>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:</P>
<UL class="alternate" type="square">
<LI><B>bin</B>: 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)</LI>
<LI><B>lib</B>: Contains several jar files (you only need of few of these jars in your classpath to do EJB development)</LI>
<LI><B>apps</B>: 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.</LI>
<LI><B>conf</B>: 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</LI>
<LI><B>logs</B>: Contains log files.</LI>
</UL>
<H2><A name="HelloWorld-Setupyourdevelopmentenvironment"></A>Setup your development environment</H2>
<H3><A name="HelloWorld-Createaworkingdirectory"></A>Create a working directory</H3>
<P>Assuming you are in your home directory, create a directory named projects</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
karan@poweredge:~$ mkdir projects
</PRE>
</DIV></DIV>
<P>Go to the projects directory</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
karan@poweredge:~$ cd projects
</PRE>
</DIV></DIV>
<P>We will do all our work in this directory.</P>
<H3><A name="HelloWorld-InstallJava"></A>Install Java</H3>
<P>Download and install Java (version 5 or higher). Also set it up so that you can run the java and javac commands from any directory</P>
<H3><A name="HelloWorld-SetOPENEJBHOME"></A>Set OPENEJB_HOME</H3>
<P>We will setup this variable to refer to the openejb install location.</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
karan@poweredge:~/projects$ export OPENEJB_HOME=/home/karan/install/openejb-3.0
</PRE>
</DIV></DIV>
<H2><A name="HelloWorld-WriteanEJB"></A>Write an EJB</H2>
<P>Whatever files you create should be placed under the projects directory</P>
<H3><A name="HelloWorld-CreatetheRemoteInterface"></A>Create the Remote Interface</H3>
<P>Using your favorite editor, create a file named Hello.java (shown below)</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
<SPAN class="code-keyword">package</SPAN> org.acme;
<SPAN class="code-keyword">import</SPAN> javax.ejb.Remote;
@Remote
<SPAN class="code-keyword">public</SPAN> <SPAN class="code-keyword">interface</SPAN> Hello{
<SPAN class="code-keyword">public</SPAN> <SPAN class="code-object">String</SPAN> sayHello();
}
</PRE>
</DIV></DIV>
<H3><A name="HelloWorld-CreatetheBeanClass"></A>Create the Bean Class</H3>
<P>Now create a file named HelloBean.java (shown below)</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
<SPAN class="code-keyword">package</SPAN> org.acme;
<SPAN class="code-keyword">import</SPAN> javax.ejb.Stateless;
@Stateless
<SPAN class="code-keyword">public</SPAN> class HelloBean <SPAN class="code-keyword">implements</SPAN> Hello{
<SPAN class="code-keyword">public</SPAN> <SPAN class="code-object">String</SPAN> sayHello(){
<SPAN class="code-keyword">return</SPAN> <SPAN class="code-quote">&quot;Hello World!!!!&quot;</SPAN>;
}
}
</PRE>
</DIV></DIV>
<H3><A name="HelloWorld-Compilethesourcecode"></A>Compile the source code</H3>
<P>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/javaee-5.0-1.jar.<BR>
Lets compile our source (make sure you are in the projects directory)</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
karan@poweredge:~/projects$ javac -cp $OPENEJB_HOME/lib/javaee-5.0-1.jar -d . *.java
</PRE>
</DIV></DIV>
<P>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.</P>
<H3><A name="HelloWorld-PackagetheEJB"></A>Package the EJB</H3>
<P>To package the EJB into a JAR, run the following command while you are in the projects directory</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
karan@poweredge:~/projects$ jar cvf hello.jar org
</PRE>
</DIV></DIV>
<P>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:</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
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%)
</PRE>
</DIV></DIV>
<H2><A name="HelloWorld-WriteanEJBClient"></A>Write an EJB Client</H2>
<P>Now we will write a Client class which will lookup the EJB , invoke the sayHello() business method and print the value returned from the method.<BR>
While you are in the projects directory, create a new file named HelloClient.java . Add the following to this file:</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
<SPAN class="code-keyword">package</SPAN> org.acme;
<SPAN class="code-keyword">import</SPAN> java.util.Properties;
<SPAN class="code-keyword">import</SPAN> javax.naming.InitialContext;
<SPAN class="code-keyword">import</SPAN> javax.naming.Context;
<SPAN class="code-keyword">import</SPAN> javax.rmi.PortableRemoteObject;
<SPAN class="code-keyword">public</SPAN> class HelloClient{
<SPAN class="code-keyword">public</SPAN> <SPAN class="code-keyword">static</SPAN> void main(<SPAN class="code-object">String</SPAN>[] args) <SPAN class="code-keyword">throws</SPAN> Exception{
Properties props = <SPAN class="code-keyword">new</SPAN> Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,<SPAN class="code-quote">&quot;org.apache.openejb.client.RemoteInitialContextFactory&quot;</SPAN>);
props.put(Context.PROVIDER_URL,<SPAN class="code-quote">&quot;ejbd:<SPAN class="code-comment">//127.0.0.1:4201&quot;</SPAN>);
</SPAN> Context ctx = <SPAN class="code-keyword">new</SPAN> InitialContext(props);
<SPAN class="code-object">Object</SPAN> ref = ctx.lookup(<SPAN class="code-quote">&quot;HelloBeanRemote&quot;</SPAN>);
Hello h = (Hello)PortableRemoteObject.narrow(ref,Hello.class);
<SPAN class="code-object">String</SPAN> result = h.sayHello();
<SPAN class="code-object">System</SPAN>.out.println(result);
}
}
</PRE>
</DIV></DIV>
<H3><A name="HelloWorld-CompileHelloClient.java"></A>Compile HelloClient.java</H3>
<P>Run the following command:</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
karan@poweredge:~/projects$ javac -d . HelloClient.java
</PRE>
</DIV></DIV>
<H2><A name="HelloWorld-StarttheServer"></A>Start the Server</H2>
<P>Go to the OpenEJB install directory (i.e. OPENEJB_HOME) and run the following command:</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
karan@poweredge:~/install/openejb-3.0$ bin/openejb start
</PRE>
</DIV></DIV>
<P>Once the Server starts, you will see an output similar to the below in your console:</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
karan@poweredge:~/install/openejb-3.0$ bin/openejb start
Apache OpenEJB 3.0 build: 20070926-12:34
http:<SPAN class="code-comment">//openejb.apache.org/
</SPAN>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!
</PRE>
</DIV></DIV>
<P>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. </P>
<H2><A name="HelloWorld-DeploytheEJB"></A>Deploy the EJB</H2>
<P>We will now use the deploy command to deploy the EJB in hello.jar. While you are in the projects directory, run the following command:</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
karan@poweredge:~/projects$ $OPENEJB_HOME/bin/openejb deploy hello.jar
</PRE>
</DIV></DIV>
<P>The above command should give you the following output:</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
karan@poweredge:~/projects$ $OPENEJB_HOME/bin/openejb deploy hello.jar
Application deployed successfully at <SPAN class="code-quote">&quot;hello.jar&quot;</SPAN>
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)
</PRE>
</DIV></DIV>
<P>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</P>
<H2><A name="HelloWorld-RuntheClient"></A>Run the Client</H2>
<P>While you are in the projects directory, run the following command to run the client:</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
karan@poweredge:~/projects$ java -cp $OPENEJB_HOME/lib/openejb-client-3.0.jar:$OPENEJB_HOME/lib/javaee-5.0-1.jar:. org.acme.HelloClient
</PRE>
</DIV></DIV>
<P>The above should give you the following output:</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
Hello World!!!!
</PRE>
</DIV></DIV>
<H2><A name="HelloWorld-Help%21%2Citdidn%27tworkforme%21%21."></A>Help! , it didn't work for me!!.</H2>
<P>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. </P>
<H1><A name="HelloWorld-Lookingformore%3F"></A>Looking for more?</H1>
<P>More EJB 3.0 examples, sample applications, tutorials and howtos available <A href="../OPENEJBx30/examples.html" title="Examples">here</A>.</P>
</DIV>
</P>
</TD>
<TD class="Col4"><IMG src="http://openejb.apache.org/images/dotTrans.gif"></TD>
<TD class="Col5">
</TD>
</TR>
<TR class="Row5">
<TD class="Col1">&nbsp;</TD>
<TD class="Col2">&nbsp;</TD>
<TD class="Col3">
<BR>
<BR>
<IMG width="100%" height="1" src="http://openejb.apache.org/images/line_light.gif">
<TABLE width="100%">
<TR>
<TD>
<SPAN class="bodyGrey">
<SMALL>
<NOTICE><!-- $FOOTER -->
Apache OpenEJB is an project of The Apache Software Foundation (ASF)
</NOTICE>
<BR>
Site Powered by
<A href="http://atlassian.com/">Atlassian</A>
<A href="http://atlassian.com/confluence/">Confluence</A>
.
</SMALL>
</SPAN>
</TD>
<TD align="right">
<A style="color:#999;font-size:small;font-weight:normal;" href="https://cwiki.apache.org/confluence/pages/editpage.action?spaceKey=OPENEJB&title=Hello%20World">[ edit ]</A>
</TD>
</TR>
</TABLE>
<BR>
</TD>
<TD class="Col4"><IMG src="http://openejb.apache.org/images/dotTrans.gif"></TD>
<TD class="Col5">&nbsp;</TD>
</TR>
</TABLE>
<!-- Needed for composition plugin -->
<!-- delay the loading of large javascript files to the end so that they don't interfere with the loading of page content -->
<SPAN style="display: none">
<SCRIPT type="text/javascript" language="JavaScript" src="http://cwiki.apache.org/confluence/labels-javascript"></SCRIPT>
<SCRIPT src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</SCRIPT>
<SCRIPT type="text/javascript">
_uacct = "UA-2717626-1";
urchinTracker();
</SCRIPT>
</SPAN>
</BODY>
</HTML>