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 > Simple Stateless Example
Date Wed, 17 Nov 2010 03:59:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/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/Simple+Stateless+Example">Simple
Stateless Example</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~dblevins">David
Blevins</a>
    </h4>
        <br/>
                         <h4>Changes (3)</h4>
                                 
    
<div id="page-diffs">
            <table class="diff" cellpadding="0" cellspacing="0">
            <tr><td class="diff-unchanged" >h1. Overview <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{span:style=float:
right; margin-left: 20px;} <br>{html} <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&lt;object
width=&quot;400&quot; height=&quot;250&quot;&gt;&lt;param name=&quot;movie&quot;
value=&quot;http://www.youtube.com/v/aLx2jta96xU?fs=1&amp;amp;hl=en_US&amp;amp;rel=0&quot;&gt;&lt;/param&gt;&lt;param
name=&quot;allowFullScreen&quot; value=&quot;true&quot;&gt;&lt;/param&gt;&lt;param
name=&quot;allowscriptaccess&quot; value=&quot;always&quot;&gt;&lt;/param&gt;&lt;embed
src=&quot;http://www.youtube.com/v/aLx2jta96xU?fs=1&amp;amp;hl=en_US&amp;amp;rel=0&quot;
type=&quot;application/x-shockwave-flash&quot; allowscriptaccess=&quot;always&quot;
allowfullscreen=&quot;true&quot; width=&quot;400&quot; height=&quot;250&quot;&gt;&lt;/embed&gt;&lt;/object&gt;
<br> <br>{html} <br>{span} <br>{div} <br> <br> <br></td></tr>
            <tr><td class="diff-unchanged" >This example shows how to create a
Stateless session EJB using annotations.  As stated in the &quot;JSR 220: Enterprise JavaBeansTM,Version
3.0 - EJB Core Contracts and Requirements&quot;, <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >_This example is the &quot;simple-stateless&quot;
example located in the [openejb-examples.zip|OPENEJB:Download] available on the download page._
<br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{div}
<br>{div:style=clear:both;}{div} <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h1. The Code <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
        </table>
</div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h1><a name="SimpleStatelessExample-Overview"></a>Overview</h1>
<span style="float: right; margin-left: 20px;">

<object width="400" height="250"><param name="movie" value="http://www.youtube.com/v/aLx2jta96xU?fs=1&amp;hl=en_US&amp;rel=0"></param><param
name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed
src="http://www.youtube.com/v/aLx2jta96xU?fs=1&amp;hl=en_US&amp;rel=0" type="application/x-shockwave-flash"
allowscriptaccess="always" allowfullscreen="true" width="400" height="250"></embed></object>

</span>
<div>

<p>This example shows how to create a Stateless session EJB using annotations.  As stated
in the "JSR 220: Enterprise JavaBeansTM,Version 3.0 - EJB Core Contracts and Requirements",</p>

<div class="panel" style="border-width: 1px;"><div class="panelContent">
<p>"Stateless session beans are session beans whose instances have no conversational
state. This means that all bean instances are equivalent when they are not involved in servicing
a client-invoked method. The term 'stateless' signifies that an instance has no state for
a specific client."</p>
</div></div>

<p>What this means is quite simply that stateless beans are <b>shared</b>.
 They do in fact have state as you can assign values to the variables, etc. in the bean instance.
 The only catch is there are a <b>pool</b> of identical instances and you are
not guaranteed to get the exact same instance on every call.  For each call, you get whatever
instance happens to be available.  This is identical to checking out a book from the library
or renting a movie from the video store.  You are essentially checking out or renting a new
bean instance on each method call.</p>

<p>With EJB 3.0, it's now possible to write stateless session bean without specifying
a deployment descriptor; you basically have to write just a remote or local business interface,
which is a plain-old-java-interface, annotated with the @Remote or @Local annotation the stateless
session bean implementation, a plain-old-java-object which implements the remote or the local
business interface and is annotated with the @Stateless annotation</p>

<p><em>This example is the "simple-stateless" example located in the <a href="/confluence/display/OPENEJB/Download"
title="Download">openejb-examples.zip</a> available on the download page.</em></p>
</div>
<div style="clear:both;"></div>

<h1><a name="SimpleStatelessExample-TheCode"></a>The Code</h1>

<p>In this example we develop a simple EJB 3 Stateless session EJB.  Every stateless
session bean implementation must be annotated using the annotation <b>@Stateless</b>
or marked that way in the ejb-jar.xml file.</p>

<p>Our Stateless bean has 2 business interfaces: CalculatorRemote, a remote business
interface, and CalculatorLocal, a local business interface.  A minimum of one business interface
is required.</p>

<h2><a name="SimpleStatelessExample-Bean"></a>Bean</h2>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">@Stateless
<span class="code-keyword">public</span> class CalculatorImpl <span class="code-keyword">implements</span>
CalculatorRemote, CalculatorLocal {

	<span class="code-keyword">public</span> <span class="code-object">int</span>
sum(<span class="code-object">int</span> add1, <span class="code-object">int</span>
add2) {
		<span class="code-keyword">return</span> add1+add2;
	}

	<span class="code-keyword">public</span> <span class="code-object">int</span>
multiply(<span class="code-object">int</span> mul1, <span class="code-object">int</span>
mul2) {
		<span class="code-keyword">return</span> mul1*mul2;
	}

}
</pre>
</div></div>

<p>In EJB 3.0 session beans do not need to implement the javax.ejb.SessionBean interface.
 You can simply annotate it as @Stateless if you want it to be a stateless session bean.</p>

<p>Users of EJB 2.x may notice the bean actually implements the business interfaces!
In the prior version of EJB implementing the remote interface (which derives from javax.ejb.EJBObject)
in your bean was just not allowed. Now there is no javax.ejb.EJBObject requirement, so implementing
the business interfaces is standard practice for EJB 3.0.</p>

<h2><a name="SimpleStatelessExample-Localbusinessinterface"></a>Local business
interface</h2>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">@Local
<span class="code-keyword">public</span> <span class="code-keyword">interface</span>
CalculatorLocal {
	
	<span class="code-keyword">public</span> <span class="code-object">int</span>
sum(<span class="code-object">int</span> add1, <span class="code-object">int</span>
add2);
	
	<span class="code-keyword">public</span> <span class="code-object">int</span>
multiply(<span class="code-object">int</span> mul1, <span class="code-object">int</span>
mul2);
}
</pre>
</div></div>

<p>Local interfaces in EJB are <b>pass-by-reference</b> interfaces.  Meaning
that <b>normal java semantics</b> are used for passing arguments, return values
and exceptions.  A business local interface can be any plain java interface.  There are no
restrictions on the method arguments, return types, or throws clauses.  </p>

<p>Unless specified otherwise, every interface your bean implements (and it's parent
class implements and so on) is considered to be a local business interface.  You can use the
<b>@Local</b> annotation to explicitly state that an interface is a local interface,
but this is not required.</p>

<p>You'll notice that in EJB 3.0 the Local Business Interface of a stateless session
bean does not need to extend from javax.ejb.EJBLocalObject and does not need a javax.ejb.EJBLocalHome
interface as they did in EJB 2.x and prior.  Per the vocabulary of the EJB spec, interfaces
that implement javax.ejb.EJBLocalObject or javax.ejb.EJBLocalHome are considered Component
Interfaces and the plain java interface above is considered a Business Interface.</p>

<h2><a name="SimpleStatelessExample-Remotebusinessinterface"></a>Remote
business interface</h2>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">@Remote
<span class="code-keyword">public</span> <span class="code-keyword">interface</span>
CalculatorRemote {
	
	<span class="code-keyword">public</span> <span class="code-object">int</span>
sum(<span class="code-object">int</span> add1, <span class="code-object">int</span>
add2);
	
	<span class="code-keyword">public</span> <span class="code-object">int</span>
multiply(<span class="code-object">int</span> mul1, <span class="code-object">int</span>
mul2);

}
</pre>
</div></div>

<p>Remote interfaces are <b>pass-by-value</b> interfaces.  Meaning that
all method parameters, return values, and exceptions are <b>serialized</b> on
every call.  The result is that you get a copy of the original object and not the original
object.  The advantage is of course that Remote interfaces can be used to invoke an EJB across
a network in a client-server fashion.  There are no restrictions on the Remote interface itself,
but there are on the data passed in and out of the remote interface.  The <b>values</b>
passed into a method or returned from a method of a Remote interface <b>must be serializable</b>.
 It is fine for the method signature to be, for example, "public Object myMethod(Object myParam)"
as long as the <b>value</b> passed in and returned implements <b>java.io.Serializable</b>.</p>

<p>As stated above, the Remote Business Interface of a bean can be any plain old interface.
 It does not need to extend javax.ejb.EJBObject, it does not need a javax.ejb.EJBHome, the
methods do not need to throw javax.rmi.RemoteException, and the bean class <b>can</b>
implement it!</p>

<p>At minimum the interface must be annotated with <b>@Remote</b> either
in the interface itself or in the bean class, or the interface must be declared via &lt;business-remote&gt;
in the ejb-jar.xml.</p>

<h1><a name="SimpleStatelessExample-Writingaunittestfortheexample"></a>Writing
a unit test for the example</h1>

<p>Writing an unit test for the stateless session EJb is quite simple. We need just
to write a setup method to create and initialize the InitialContext, and then write our test
methods</p>

<h2><a name="SimpleStatelessExample-setUp"></a>setUp</h2>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">private</span> InitialContext
initialContext;

   <span class="code-keyword">protected</span> void setUp() <span class="code-keyword">throws</span>
Exception {
       Properties properties = <span class="code-keyword">new</span> Properties();
       properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, <span class="code-quote">"org.apache.openejb.client.LocalInitialContextFactory"</span>);

       initialContext = <span class="code-keyword">new</span> InitialContext(properties);
   }
</pre>
</div></div>

<h2><a name="SimpleStatelessExample-Testthelocalbusinessinterface"></a>Test
the local business interface</h2>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">  <span class="code-keyword">public</span> void testCalculatorViaLocalInterface()
<span class="code-keyword">throws</span> Exception {
      <span class="code-object">Object</span> object = initialContext.lookup(<span
class="code-quote">"CalculatorImplLocal"</span>);

assertNotNull(object);
assertTrue(object <span class="code-keyword">instanceof</span> CalculatorLocal);
CalculatorLocal calc = (CalculatorLocal) object;
assertEquals(10, calc.sum(4,6));
assertEquals(12, calc.multiply(3,4));
  }
</pre>
</div></div>

<h2><a name="SimpleStatelessExample-Testtheremotebusinessinterface"></a>Test
the remote business interface</h2>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">  <span class="code-keyword">public</span> void testCalculatorViaRemoteInterface()
<span class="code-keyword">throws</span> Exception {
      <span class="code-object">Object</span> object = initialContext.lookup(<span
class="code-quote">"CalculatorImplRemote"</span>);

assertNotNull(object);
assertTrue(object <span class="code-keyword">instanceof</span> CalculatorRemote);
CalculatorRemote calc = (CalculatorRemote) object;
assertEquals(10, calc.sum(4,6));
assertEquals(12, calc.multiply(3,4));
  }
</pre>
</div></div>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>JNDI Names</b><br
/>Note that JNDI names for Java SE clients are not standardized by the EJB spec.  This
is unfortunate and something being addressed in EJB 3.1.  The default schema that OpenEJB
uses is ejbName + interfaceType (i.e. Local, Remote, LocalHome, RemoteHome), so in our example
"CalculatorImpl" + "Local" and "CalculatorImpl" + "Remote".  You can in fact change this default
to be absolutely <a href="/confluence/display/OPENEJBx30/JNDI+Names" title="JNDI Names">anything
you want</a> including interface class name, ejb class name, and more.</td></tr></table></div>


<h1><a name="SimpleStatelessExample-Running"></a>Running</h1>

<p>Running the example is fairly simple.  In the "simple-stateless" directory of the
<a href="/confluence/display/OPENEJB/Download" title="Download">examples zip</a>,
just run:</p>

<p>$ mvn clean install</p>

<p>Which should create output like the following.</p>

<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.superbiz.calculator.CalculatorTest
Apache OpenEJB 3.0    build: 20080408-04:13
http://openejb.apache.org/
INFO - openejb.home = /Users/dblevins/work/openejb-3.0/examples/simple-stateless
INFO - openejb.base = /Users/dblevins/work/openejb-3.0/examples/simple-stateless
INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default
Security Service)
INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default
Transaction Manager)
INFO - Configuring Service(id=Default JDK 1.3 ProxyFactory, type=ProxyFactory, provider-id=Default
JDK 1.3 ProxyFactory)
INFO - Found EjbModule in classpath: /Users/dblevins/work/openejb-3.0/examples/simple-stateless/target/classes
INFO - Configuring app: /Users/dblevins/work/openejb-3.0/examples/simple-stateless/target/classes
INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default
Stateless Container)
INFO - Auto-creating a container for bean CalculatorImpl: Container(type=STATELESS, id=Default
Stateless Container)
INFO - Loaded Module: /Users/dblevins/work/openejb-3.0/examples/simple-stateless/target/classes
INFO - Assembling app: /Users/dblevins/work/openejb-3.0/examples/simple-stateless/target/classes
INFO - Jndi(name=CalculatorImplLocal) --&gt; Ejb(deployment-id=CalculatorImpl)
INFO - Jndi(name=CalculatorImplRemote) --&gt; Ejb(deployment-id=CalculatorImpl)
INFO - Created Ejb(deployment-id=CalculatorImpl, ejb-name=CalculatorImpl, container=Default
Stateless Container)
INFO - Deployed Application(path=/Users/dblevins/work/openejb-3.0/examples/simple-stateless/target/classes)
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.845 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
</pre>
</div></div>

    </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/Simple+Stateless+Example">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=34097&revisedVersion=20&originalVersion=19">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/OPENEJBx30/Simple+Stateless+Example?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message