vendredi 3 juillet 2015

Inside EJB lookup: constructor, @PostConstruct

I'm studying the insight of EJB lookup and trying to understand how the container and the pool works. I created some test application, each of one I'm going to explain.

First implementation: lookup inside constructor

    @Stateless
    public class EjbTest {

    protected EjbInjectedLocal      ejbInjected;

    public EjbTest() {
            InitialContext ic = new InitialContext();
            ejbInjected = (EjbInjectedLocal)ic.lookup("java:comp/env/ejb/EjbInjected");
            ic.close();
    }

    public void test() {
        ejbInjected.callSomeMethod();
        return;
    }

}

Injection is performed inside the class constructor (bad practice), but everything works. The constructor retrieve a Proxy for the EJB. When I call the method test(), it's executed correctly.

Second implementation: lookup inside @PostConstruct

@Stateless
public class EjbTest {

    protected EjbInjectedLocal      ejbInjected;

    public EjbTest() {
    }

    @PostConstruct
    public start() {
            InitialContext ic = new InitialContext();
            ejbInjected = (EjbInjectedLocal)ic.lookup("java:comp/env/ejb/EjbInjected");
            ic.close();
    }

    public void test() {
        ejbInjected.callSomeMethod();
        return;
    }

}

As the prior example, the lookup works fine as well the method test.

Third implementation: lookup inside constructor and function execution

@Stateless
public class EjbTest {

    protected EjbInjectedLocal      ejbInjected;

    public EjbTest() {
            InitialContext ic = new InitialContext();
            ejbInjected = (EjbInjectedLocal)ic.lookup("java:comp/env/ejb/EjbInjected");
            ejbInjected.callSomeMethod();
            ic.close();
    }

}

With this implementation, the lookup works fine, but the function halt/freeze the thread, as the container is not yet ready to return NOT the proxy implementation, but the whole EJB and the function cannot be performed.

When the constructor is called, the bean is not yet initialized and no dependencies are injected? Only the proxy is returned but it's not yet available and cannot be retrieved the whole EJB from the pool?

Aucun commentaire:

Enregistrer un commentaire