Search code examples
javaejbejb-3.0openejb

How to access stateful session bean through remote interface proxy?


I have a testcase which makes use of OpenEjb's @LocalClient annotation (embedded container) and injects EJBs using the @EJB annotation:

@LocalClient
public class MyTestCase {

    @EJB
    private BoxDao boxDao;
    
    ...   
    
}

BoxDao is a remote EJB interface. Now, for testing, I need to access some internal state of BoxDao's implementation BoxDaoBean, which is a stateful session bean. I created a protected method in BoxDaoBean, which exposes the needed internal state, but I found not yet a way to access it in my test case, since the injected BoxDao is a remote interface proxy (an cannot be cast to BoxDaoBean).

Is there a way to access the stateful session bean behind the remote interface BoxDao in the test case? Would not matter if solution is OpenEjb specific.

Update: We can't use EJB 3.1 specific solutions unfortunately, as we have several EJB 3.0 projects running. Using Proxy.getInvocationHandler(boxDao), I can get access to the OpenEjb container, via StatefulEjbObjectHandler. Is it possible to access the stateful bean this way?


Solution

  • You could try having BoxDaoBean also expose an @LocalBean interface. A single EJB can expose a near unlimited number of views from @WebService, @Local, @Remote, JAX-RS and more.

    Just update your bean like so:

    @Stateful
    @LocalBean
    public class BoxDaoBean implements BoxDao {
       //...
    }
    

    Then add another field to your test:

    @LocalClient
    public class MyTestCase {
    
        @EJB
        private BoxDao boxDao;
    
        @EJB
        private BoxDaoBean boxDaoBean;
    
        ...   
    
    }