blob: 41e0639a47467984d9ca5730eadbd3e1396203b0 [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 - Simple Stateless Example</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">
</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="../OPENEJB/index.html" title="Index">Home</A> | <A href="../OPENEJB/download.html" title="Download">Download</A> | <A href="../OPENEJB/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="../OPENEJB/index.html" title="Index">Home</A></LI>
<LI><A href="../OPENEJB/news.html" title="News">News</A></LI>
<LI><A href="../OPENEJB/faq.html" title="FAQ">FAQ</A></LI>
<LI><A href="../OPENEJB/download.html" title="Download">Download</A></LI>
<LI><A href="index.html" title="Index">Documentation</A></LI>
<LI><A href="../OPENEJB/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="../OPENEJB/mailing-lists.html" title="Mailing Lists">Mailing Lists</A></LI>
<LI><A href="../OPENEJB/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="../OPENEJB/local-server.html" title="Local Server">Local</A></LI>
<LI><A href="../OPENEJB/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="tomcat.html" title="Tomcat">Tomcat</A></LI>
<LI><A href="../OPENEJB/geronimo.html" title="Geronimo">Geronimo</A></LI>
<LI><A href="../OPENEJB/webobjects.html" title="WebObjects">WebObjects</A></LI>
</UL>
<H3><A name="Navigation-Community"></A>Community</H3>
<UL class="alternate" type="square">
<LI><A href="../OPENEJB/team.html" title="Team">Team</A></LI>
<LI><A href="../OPENEJB/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="../OPENEJB/space-index.html" title="Space Index">Site Index</A></LI>
<LI><A href="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 -->
Simple Stateless Example
</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">
<H1><A name="SimpleStatelessExample-Overview"></A>Overview</H1>
<P><SPAN style="float: right; margin-left: 20px;">
<OBJECT width="400" height="250"><PARAM name="movie" value="http://www.youtube.com/v/aLx2jta96xU?fs=1&hl=en_US"></PARAM><PARAM name="allowFullScreen" value="true"></PARAM><PARAM name="allowscriptaccess" value="always"></PARAM><EMBED src="http://www.youtube.com/v/aLx2jta96xU?fs=1&hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="400" height="250"></EMBED></OBJECT>
</SPAN></P>
<DIV>
<P>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;,</P>
<DIV class="panel" style="border-width: 1px;"><DIV class="panelContent">
<P>&quot;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.&quot;</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 &quot;simple-stateless&quot; example located in the <A href="../OPENEJB/download.html" 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"><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, &quot;public Object myMethod(Object myParam)&quot; 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">&quot;org.apache.openejb.client.LocalInitialContextFactory&quot;</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">&quot;CalculatorImplLocal&quot;</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">&quot;CalculatorImplRemote&quot;</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="https://cwiki.apache.org/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 &quot;CalculatorImpl&quot; + &quot;Local&quot; and &quot;CalculatorImpl&quot; + &quot;Remote&quot;. You can in fact change this default to be absolutely <A href="jndi-names.html" 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 &quot;simple-stateless&quot; directory of the <A href="../OPENEJB/download.html" 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>
</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=OPENEJBx30&title=Simple%20Stateless%20Example">[ 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>