<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    EJB3.1 Singleton Beans in AS 6.0.0 M3
</h3>
<span style="margin-bottom: 10px;">
    modified by <a href="http://community.jboss.org/people/jaikiran">jaikiran pai</a> in <i>EJB 3.0</i> - <a href="http://community.jboss.org/docs/DOC-15105">View the full document</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><h5><span>Overview</span></h5><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>JBoss AS 6.0.0.M3 which was released on May 5th 2010, introduces support for EJB3.1 Singleton beans.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h5><span>What to download and how to use</span></h5><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>You can download JBoss AS 6.0.0.M3 from <a class="jive-link-external-small" href="http://www.jboss.org/jbossas/downloads.html">here</a>. After downloading the AS, start and stop it once to make sure it boots without any issues.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>The next step would be to deploy a EJB3.1 Singleton bean into this server.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h5><span>EJB3.1 Singleton bean:</span></h5><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Note that the examples shown below are just for the sake of illustrating the usage of singleton beans and as such don't hold much meaning.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Let's deploy a very simple singleton bean which exposes a remote view:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java"><font color="navy"><b>package</b></font> org.jboss.ejb3.singleton.example;
&#160;
&#160;
<font color="navy"><b>public</b></font> <font color="navy"><b>interface</b></font> Counter
<font color="navy">{</font>
&#160;&#160;&#160; <font color="navy"><b>void</b></font> incrementCount();
&#160;&#160;&#160; 
&#160;&#160;&#160; <font color="navy"><b>void</b></font> decrementCount();
&#160;&#160;&#160; 
&#160;&#160;&#160; <font color="navy"><b>int</b></font> getCount();
<font color="navy">}</font>
&#160;
&#160;
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java"><font color="navy"><b>package</b></font> org.jboss.ejb3.singleton.example;
&#160;
&#160;
<font color="navy"><b>import</b></font> javax.ejb.EJB;
<font color="navy"><b>import</b></font> javax.ejb.Remote;
<font color="navy"><b>import</b></font> javax.ejb.Singleton;
&#160;
&#160;
<font color="navy"><b>import</b></font> org.jboss.ejb3.annotation.RemoteBinding;
&#160;
&#160;
@Singleton
@Remote(Counter.class)
@RemoteBinding (jndiBinding = <font color="red">"SimpleCounterRemoteJNDIName"</font>)
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> SimpleCounter <font color="navy"><b>implements</b></font> Counter
<font color="navy">{</font>
&#160;
&#160;
&#160;&#160;&#160; <font color="darkgreen">// we use a calculator to increment/decrement the count</font>
&#160;&#160; @EJB
&#160;&#160; <font color="navy"><b>private</b></font> Calculator calculator;
&#160;
&#160;
&#160;&#160;&#160; <font color="darkgreen">// Maintains the current count</font>
&#160;&#160; <font color="navy"><b>private</b></font> <font color="navy"><b>int</b></font> count;
&#160;
&#160;
&#160;&#160; @Override
&#160;&#160; <font color="navy"><b>public</b></font> <font color="navy"><b>int</b></font> getCount()
&#160;&#160; <font color="navy">{</font>
&#160;&#160;&#160;&#160;&#160; <font color="navy"><b>return</b></font> this.count;
&#160;&#160; <font color="navy">}</font>
&#160;
&#160;
&#160;&#160; @Override
&#160;&#160; <font color="navy"><b>public</b></font> <font color="navy"><b>void</b></font> incrementCount()
&#160;&#160; <font color="navy">{</font>
&#160;&#160;&#160;&#160;&#160; this.count = this.calculator.add(this.count, 1);
&#160;&#160;&#160;&#160;&#160; 
&#160;&#160; <font color="navy">}</font>
&#160;
&#160;
&#160;&#160; @Override
&#160;&#160; <font color="navy"><b>public</b></font> <font color="navy"><b>void</b></font> decrementCount()
&#160;&#160; <font color="navy">{</font>
&#160;&#160;&#160;&#160;&#160; this.count = this.calculator.subtract(this.count, 1);
&#160;&#160;&#160;&#160;&#160; 
&#160;&#160; <font color="navy">}</font>
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java"><font color="navy"><b>package</b></font> org.jboss.ejb3.singleton.example;
&#160;
&#160;
<font color="navy"><b>import</b></font> javax.ejb.Singleton;
&#160;
&#160;
@Singleton
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> Calculator
<font color="navy">{</font>
&#160;&#160; <font color="navy"><b>public</b></font> <font color="navy"><b>int</b></font> add(<font color="navy"><b>int</b></font> a, <font color="navy"><b>int</b></font> b)
&#160;&#160; <font color="navy">{</font>
&#160;&#160;&#160;&#160;&#160; <font color="navy"><b>return</b></font> a + b;
&#160;&#160; <font color="navy">}</font>
&#160;&#160; 
&#160;&#160; <font color="navy"><b>public</b></font> <font color="navy"><b>int</b></font> subtract(<font color="navy"><b>int</b></font> a, <font color="navy"><b>int</b></font> b)
&#160;&#160; <font color="navy">{</font>
&#160;&#160;&#160;&#160;&#160; <font color="navy"><b>return</b></font> a - b;
&#160;&#160; <font color="navy">}</font>
<font color="navy">}</font>
&#160;
&#160;
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>So in the example above, we have a Counter interface which is exposed as the remote view for the SimpleCounter Singleton bean. Notice that the SimpleCounter class is marked with @Singleton annotation:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java">@Singleton
@Remote(Counter.class)
@RemoteBinding (jndiBinding = <font color="red">"SimpleCounterRemoteJNDIName"</font>)
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> SimpleCounter <font color="navy"><b>implements</b></font> Counter
<font color="navy">{</font>
...
&#160;
&#160;
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>The SimpleCounter internally uses a Calculator to do the increment and decrement operations. As you can see, the Calculator is being injected into the SimpleCounter by using a @EJB annotation:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java">&#160;&#160;&#160; <font color="darkgreen">// we use a calculator to increment/decrement the count</font>
&#160;&#160; @EJB
&#160;&#160; <font color="navy"><b>private</b></font> Calculator calculator;
&#160;
&#160;
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>If you look at the Calculator class, you will notice that it is a singleton bean with a no-interface view:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java">@Singleton
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> Calculator
<font color="navy">{</font>
...
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>So effectively, in this example, we have 2 singleton beans. One is exposing a remote view and the other is exposing a no-interface view.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Now package all these classes into a .jar and deploy it to the server which you downloaded earlier. That's it! You now have the beans deployed on the server. The next step is to write a simple client which access the Counter to perform the operations. In this example, let's use a standalone java class which through its main() method, looks up the remote view of the SimpleCounter and invokes the operations. Here's the client:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java"><font color="navy"><b>package</b></font> org.jboss.ejb3.singleton.example.client;
 
<font color="navy"><b>import</b></font> javax.naming.Context;
<font color="navy"><b>import</b></font> javax.naming.InitialContext;
<font color="navy"><b>import</b></font> javax.naming.NamingException;
<font color="navy"><b>import</b></font> org.jboss.ejb3.singleton.example.Counter;
 
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> Client
<font color="navy">{</font>
&#160;&#160; <font color="navy"><b>public</b></font> <font color="navy"><b>static</b></font> <font color="navy"><b>void</b></font> main(String args[]) <font color="navy"><b>throws</b></font> Exception
&#160;&#160; <font color="navy">{</font>
&#160;&#160;&#160;&#160;&#160; Context ctx = <font color="navy"><b>new</b></font> InitialContext();
&#160;&#160;&#160;&#160;&#160; <font color="darkgreen">// lookup the counter bean</font>
&#160;&#160;&#160;&#160;&#160; Counter counter = (Counter) ctx.lookup(<font color="red">"SimpleCounterRemoteJNDIName"</font>);
&#160;&#160;&#160;&#160;&#160; 
&#160;&#160;&#160;&#160;&#160; System.out.println(<font color="red">"Initial count is "</font> + counter.getCount());
&#160;&#160;&#160;&#160;&#160; 
&#160;&#160;&#160;&#160;&#160; <font color="darkgreen">// let's increment once</font>
&#160;&#160;&#160;&#160;&#160; counter.incrementCount();
&#160;&#160;&#160;&#160;&#160; 
&#160;&#160;&#160;&#160;&#160; System.out.println(<font color="red">"Count after increment is "</font> + counter.getCount());
&#160;&#160;&#160;&#160;&#160; 
&#160;&#160;&#160;&#160;&#160; <font color="darkgreen">// now let's just look up the counter once more</font>
&#160;&#160;&#160;&#160;&#160; <font color="darkgreen">// and see whether the incremented state is still maintained (it should be!)</font>
&#160;&#160;&#160;&#160;&#160; Counter oneMoreCounter = (Counter) ctx.lookup(<font color="red">"SimpleCounterRemoteJNDIName"</font>);
&#160;&#160;&#160;&#160;&#160; 
&#160;&#160;&#160;&#160;&#160; <font color="darkgreen">// should be 1</font>
&#160;&#160;&#160;&#160;&#160; System.out.println(<font color="red">"Count from another counter is "</font> + oneMoreCounter.getCount());
&#160;&#160;&#160;&#160;&#160; 
&#160;&#160;&#160;&#160;&#160; <font color="darkgreen">// now let's decrement</font>
&#160;&#160;&#160;&#160;&#160; oneMoreCounter.decrementCount();
&#160;&#160;&#160;&#160;&#160; 
&#160;&#160;&#160;&#160;&#160; <font color="darkgreen">// should be 0</font>
&#160;&#160;&#160;&#160;&#160; System.out.println(<font color="red">"Count after decrementing is "</font> + counter.getCount());
&#160;&#160; <font color="navy">}</font>
 
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>In the client code, we first lookup the SimpleCounter singleton bean and try out a couple of operations through it. We then again lookup the same singleton bean and try out some more operations on it. Remember that since it's a singleton bean, all these operations will finally end up being called on the same singleton bean instance. So effectively, you will have a single state irrespective of the proxy instance on which you are invoking the operations.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h5><span>What should I try next?</span></h5><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>The above example was just to get you started with EJB3.1 Singleton beans. Try out the singleton beans within your own applications and let us know if you run into any issues. Feel free to start a discussion about any issues around this, in our <a class="jive-link-external-small" href="http://community.jboss.org/community/ejb3">EJB3 user forum</a> or ping us on <a class="jive-link-external-small" href="http://www.jboss.org/ejb3/chat.html">IRC</a>. The more issues you find now, the better - because we can get some of them fixed before AS 6.0.0.M3 is released.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> &#160;</p><h5><span>I have some tutorial for singleton beans, Can I contribute?</span></h5><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Similar to our other EJB3 tutorials, we are going to include a tutorial for singleton beans. Infact, the example that is posted here in the wiki, can perhaps be just added as a tutorial in SVN. If anyone of you wants to contribute a different tutorial and a chapter in our guide, then feel free to let us know - either through the forums or IRC.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h5><span>Known issues:</span></h5><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Currently there are a couple of known-issues in the singleton bean implementation:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>1) PostConstruct/PreDestroy are not invoked in a transactional context <a class="jive-link-external-small" href="https://jira.jboss.org/jira/browse/EJBTHREE-2070">EJBTHREE-2070</a></p><p>2) The @DependsOn for a Singleton bean is not yet implemented</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> &#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Comment by <a href="http://community.jboss.org/docs/DOC-15105">going to Community</a></p>

        <p style="margin: 0;">Create a new document in EJB 3.0 at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=102&containerType=14&container=2029">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>