[jboss-user] [Beginner's Corner] - Number of stateless beans

piotrekde do-not-reply at jboss.com
Sat Jan 22 06:28:38 EST 2011


piotrekde [http://community.jboss.org/people/piotrekde] created the discussion

"Number of stateless beans"

To view the discussion, visit: http://community.jboss.org/message/582492#582492

--------------------------------------------------------------
Hello,

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:

@Remote
public interface ComputerRemote {
    void startComputations();
}




@Stateful
public class ComputerBean implements ComputerRemote {
 
    @EJB
    private ProcessorLocal processor;
 
    public void startComputations() {
        Random r = new Random();
        while(true) {
            processor.compute(r.nextInt());
        }
    }
}
 




@Local
public interface ProcessorLocal {
    void compute(int i);
}




@Stateless
public class ProcessorBean implements ProcessorLocal {
 
    private static int COUNTER = 0;
 
    public ProcessorBean() {
        COUNTER ++;
    }
 
    public void compute(int i) {
        System.out.println("----------------> " + COUNTER);
 
        // some dummy computations - it's here only in order to take some time
        double d = 0.3;
        for (int j=0; j<100; j++) {
            d+= 0.2 % 4 + i;
        }
    }
}



So, we got stateful ComputerBean (along with its remote interface) which holds a reference to (local) stateless ProcessorBean.
ComputerBean draws some random number and passes it into processor which do some dummy computations (just to take some time).
There is also a COUNTER integer which is expected to count the number of stateless ProcessorBeans.

If I execute it as below:


        Object obj = jndiContext.lookup("app/ComputerBean/remote");
        ComputerRemote computer = (ComputerRemote) obj;
        computer.startComputations();



Computations are started, but on JBoss console there is still printed COUNTER=1.

So my questions are:

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).

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?

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).

Thanks in advance for all hints!

Piotr
--------------------------------------------------------------

Reply to this message by going to Community
[http://community.jboss.org/message/582492#582492]

Start a new discussion in Beginner's Corner at Community
[http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2075]

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20110122/17aa0838/attachment.html 


More information about the jboss-user mailing list