<!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">JBoss 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;">
    Number of stateless beans
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="http://community.jboss.org/people/piotrekde">piotrekde</a> in <i>Beginner's Corner</i> - <a href="http://community.jboss.org/message/582492#582492">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>Hello,</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I have a couple of questions related to the number of stateless beans created by JBoss. First please take a look at the following code:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code">@Remote
public interface ComputerRemote {
&#160;&#160;&#160; void startComputations();
}
</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">@Stateful
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> ComputerBean <font color="navy"><b>implements</b></font> ComputerRemote <font color="navy">{</font>
&#160;
&#160;&#160;&#160; @EJB
&#160;&#160;&#160; <font color="navy"><b>private</b></font> ProcessorLocal processor;
&#160;
&#160;&#160;&#160; <font color="navy"><b>public</b></font> <font color="navy"><b>void</b></font> startComputations() <font color="navy">{</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160; Random r = <font color="navy"><b>new</b></font> Random();
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy"><b>while</b></font>(<font color="navy"><b>true</b></font>) <font color="navy">{</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; processor.compute(r.nextInt());
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy">}</font>
&#160;&#160;&#160; <font color="navy">}</font>
<font color="navy">}</font>
&#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 style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java">@Local
<font color="navy"><b>public</b></font> <font color="navy"><b>interface</b></font> ProcessorLocal <font color="navy">{</font>
&#160;&#160;&#160; <font color="navy"><b>void</b></font> compute(<font color="navy"><b>int</b></font> i);
<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">@Stateless
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> ProcessorBean <font color="navy"><b>implements</b></font> ProcessorLocal <font color="navy">{</font>
&#160;
&#160;&#160;&#160; <font color="navy"><b>private</b></font> <font color="navy"><b>static</b></font> <font color="navy"><b>int</b></font> COUNTER = 0;
&#160;
&#160;&#160;&#160; <font color="navy"><b>public</b></font> ProcessorBean() <font color="navy">{</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160; COUNTER ++;
&#160;&#160;&#160; <font color="navy">}</font>
&#160;
&#160;&#160;&#160; <font color="navy"><b>public</b></font> <font color="navy"><b>void</b></font> compute(<font color="navy"><b>int</b></font> i) <font color="navy">{</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160; System.out.println(<font color="red">"----------------&gt; "</font> + COUNTER);
&#160;
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="darkgreen">// some dummy computations - it's here only in order to take some time</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy"><b>double</b></font> d = 0.3;
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy"><b>for</b></font> (<font color="navy"><b>int</b></font> j=0; j&lt;100; j++) <font color="navy">{</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; d+= 0.2 % 4 + i;
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy">}</font>
&#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>So, we got stateful ComputerBean (along with its remote interface) which holds a reference to (local) stateless ProcessorBean.</p><p>ComputerBean draws some random number and passes it into processor which do some dummy computations (just to take some time).</p><p>There is also a COUNTER integer which is expected to count the number of stateless ProcessorBeans.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>If I execute it as below:</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;&#160;&#160;&#160;&#160; Object obj = jndiContext.lookup(<font color="red">"app/ComputerBean/remote"</font>);
&#160;&#160;&#160;&#160;&#160;&#160;&#160; ComputerRemote computer = (ComputerRemote) obj;
&#160;&#160;&#160;&#160;&#160;&#160;&#160; computer.startComputations();
</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>Computations are started, but on JBoss console there is still printed COUNTER=1.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>So my questions are:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>1. Why COUNTER is equal 1 all the time? Shouldn't it be more stateless beans? (I know that server manages them, but I thought that if stateless bean do some work for a some amount of time, and another call comes in, then server would create another instance of thise bean and processing both of them would take place simultaneously).</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>2. Is COUNTER=1 all the time because ComputerBean waits for ProcessorBean to end its current computations, and then call another one passing the next random number? I mean - it is blocking operation, running in one thread? Is there any way to run this computations in multiple threads and do not wait for the end of the first one in order to call second?</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>3. (this question is probably related to 2nd question) Is there any way to run ComputerBean in a "daemon" mode? I mean, if I execute computer.starComputations() by JNDI from the remote client it stops and waits for the return from startComputations method. It would never happen, because it runs in while(true) loop. But I'd like to call this method and process next client's instructions. E.g. ProcessorBean was a kind of CRON service / FTP server (running in background all the time, waiting for incoming requests, not blocking the rest of execution).</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Thanks in advance for all hints!</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Piotr</p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Reply to this message by <a href="http://community.jboss.org/message/582492#582492">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in Beginner's Corner at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2075">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


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

</div>

</body>
</html>