3 Replies Latest reply on Jul 3, 2007 8:07 AM by winterer

    Accessing stateful session beans from stateless session bean

    winterer

      Is it possible (and a good idea) in EJB 3.0 to access stateful session beans via a stateless session facade (instead of a JNDI-lookup)? This would enable a new possibility to implement a variation of the value-list-handler pattern.

      Example:

      // get a stateless session facade
      StatelessSessionRemote statelessSession = ctx.lookup("...");
      
      // execute a query and return the result in form of a
      // stateful session bean that holds the result internally
      StatefulResultRemote result = statelessSession.findData(criteria);
      // remote-iterate over the result
      while (result.hasNext()) {
       Data[] data = result.fetchNext(100); // fetch next 100 rows
      }
      // we are done (done is annotated with @Remove)
      result.done();
      


      If this is possible, it would be nice if the stateful session bean (that implements both, the local and the remote interface) could be initialized from within the stateless session bean using the local interface and is then returned to the client (using the remote interface):
      @Stateless
      public class StatelessSessionBean implements StatelessSessionRemote {
       ...
       public StatefulResultRemote findData(String criteria) {
       // create query using the criteria
       Query query = entityManager.createQuery("...");
       // lookup local interface of StatefulResult
       StatefulResultLocal result = ctx.lookup("...");
       // initialize the result with the query
       result.setQuery(query);
      
       // return the remote(!)-result to the client
       return (StatefulResultRemote)result;
       }
      }
      

      But I think this is not possible because the cast (in the return statement) doesn't work - does it?

        • 1. Re: Accessing stateful session beans from stateless session
          keyurva

          Casting a local interface to a remote one might not work... May be you can define your local interface like this:

          @Local
          public interface StatefulResultLocal {
           StatefulResultRemote getRemote();
           ....
          }
          

          and in the EJB return it as such:
          @Stateful
          public class StatefulResultBean implements StatefulResultLocal, StatefulResultRemote {
           public StatefulResultRemote getRemote() {
           return (StatefulResultRemote)ctx.getEJBObject(); //@NOTE
           }
           ...
          }
          

          @NOTE: But as per my other post (http://jboss.com/index.html?module=bb&op=viewtopic&t=62006) this method does not work for EJB 3.0... Is there another way to be able to do this?

          • 2. Re: Accessing stateful session beans from stateless session
            winterer

            Thanks for your reply! I think this is the recommended way to do what I want - if it worked...
            I think there are two reasons why your approach does not work with EJB 3.0:

            This feature is not implemented yet but will be in further previews.
            This feature is not supported any more.

            But I don't think that EJB 3.0 does not support this feature any more. Maybe it is now possible to do a simple cast (as I did in my posting) - but as long as the spec doesn't say anything about this, we cannot be sure (even if it works with JBoss).
            I'll do some tests...

            • 3. Re: Accessing stateful session beans from stateless session
              winterer

              (At least) since JBoss 4.2.0.GA, it is possible to query and return the remote interface of an existing (stateful) session bean:

              @Stateful
              @Remote( { MyRemoteInterface.class })
              public class MyStatefulSessionBean implements MyRemoteInterface, MyLocalInterface {
              
               //... any remote/local methods ...
              
               // method defined in MyLocalInterface:
               public MyRemoteInterface getRemoteInterface() {
               Object obj = ctx.getBusinessObject(MyRemoteInterface.class);
               return (MyRemoteInterface)obj;
               }
              }