[jboss-user] [Beginner's Corner] - Re: Create stateless bean from stateful bean

Piotr De do-not-reply at jboss.com
Mon Jan 3 06:48:46 EST 2011

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

"Re: Create stateless bean from stateful bean"

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

Thanks for answer!
> Never instantiate EJBs (or any server managed components) via the constructor invocation. Doing so will strip off all the server provided services off those components (for example, the instances will no longer be having any transaction or security semantics).
I didn't know it, I'll take it into account in future.
> You mean same JVM? I would say it doesn't make any difference, but I haven't yet understood the question, so I might be missing something.
Yes, that's what I mean. Please take a look at the example:

Here we've got Stateless bean which is responsible for doing some computations (running on JBoss):

public class WorkerBean implements WorkerRemote {
  public void doSomeComputations(int x) {
    // executing this method takes some time

This is ordinary Java class which runs on my Desktop JVM. It runs 100 times computations, each one with different argument.
public class ClientPOJO {
  public static void main(String[] args) {
    Context jndiContext = getInitialContext();
    for(int i=0; i<100; i++) {
      WorkerRemote r = (WorkerRemote) jndiContext.lookup("WorkerBean/remote");

This code results that stateless beans are taken from the pool. So it might be 1,5,10 or even 20 WorkerBean objects (it depends on JBoss).
Now I want to do the same what do ClientPOJO#main but using Stateful bean running on JBoss. 
The question is: how to obtain references to WorkerRemote interface appropriately?

public class Manager implements ManagerRemote {
  public void orderComputations() {
    for(int i=0; i<100; i++) {
      WorkerRemote r = // how to obtain reference? Should I use JNDI as ClientPOJO or @EJB annotation?

(This is not real logic - actually method orderComputations() will be called X times, but I think that this code clearly describes my question.)

Reply to this message by going to Community

Start a new discussion in Beginner's Corner at Community

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20110103/48c96154/attachment.html 

More information about the jboss-user mailing list