tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] OpenEJB 3.0.x documentation > Hello World
Date Wed, 08 Jun 2011 02:11:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=OPENEJBx30&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/OPENEJBx30/Hello+World">Hello
World</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~dblevins">David
Blevins</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        Link to OpenEJB 3.0 distribution.  Libraries are not right for OpenEJB 3.1 as the
ejb31-experimental-api jar is required to run the client<br />
    </div>
        <br/>
                         <h4>Changes (2)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >h2. Download and install OpenEJB <br>
<br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">You
can download OpenEJB from [here | http://openejb.apache.org/download.html]. 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.  <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">This
example pertains to OpenEJB 3.0 which can be [downloaded here|http://archive.apache.org/dist/openejb/3.0].
 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.  <br></td></tr>
            <tr><td class="diff-unchanged" > <br>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: <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <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="/confluence/display/OPENEJBx30/Examples" title="Examples">Examples</a>
page for a full list of examples that range from <a href="/confluence/display/OPENEJBx30/Simple+Stateless+Example"
title="Simple Stateless Example">@Stateles</a> and <a href="/confluence/display/OPENEJBx30/Simple+Stateful+Example"
title="Simple Stateful Example">@Stateful</a> beans, to <a href="/confluence/display/OPENEJBx30/Injection+of+env-entry+Example"
title="Injection of env-entry Example">Dependency Injection</a>, JDBC <a href="/confluence/display/OPENEJBx30/Injection+of+DataSource+Example"
title="Injection of DataSource Example">DataSources</a>, JPA <a href="/confluence/display/OPENEJBx30/Injection+of+EntityManager+Example"
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>This example pertains to OpenEJB 3.0 which can be <a href="http://archive.apache.org/dist/openejb/3.0"
class="external-link" rel="nofollow">downloaded 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">"Hello
World!!!!"</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">"org.apache.openejb.client.RemoteInitialContextFactory"</span>);
                props.put(Context.PROVIDER_URL,<span class="code-quote">"ejbd:<span
class="code-comment">//127.0.0.1:4201"</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">"HelloBeanRemote"</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">"hello.jar"</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="/confluence/display/OPENEJBx30/Examples" title="Examples">here</a>.</p>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/OPENEJBx30/Hello+World">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=26494&revisedVersion=13&originalVersion=12">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/OPENEJBx30/Hello+World?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message