Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EJB interface JNDI lookup

i have an interface

@Local
public interface TestService extends Serializable
{
    public void aMethod();
}

a stateless bean that implements it

@Stateless
public class MyappServiceBean implements TestService
{
    private static final long serialVersionUID = 1L;

    @Override
    public void aMethod()
    {
        // do something...
    }
}

a managed bean

@ManagedBean
public class MyappBean implements Serializable
{
    private static final long serialVersionUID = 1L;

    @EJB
    private TestService service;

    ...
}

and this POJO

public class TestPojo implements Serializable
{
    private static final long serialVersionUID = 1L;

    private final TestService service;

    public TestPojo()
    {
        // this works
        // service = (TestService) InitialContext.doLookup("java:global/myapp/MyappServiceBean");

        // this works too
        // service = (TestService) InitialContext.doLookup("java:global/myapp/MyappServiceBean!com.example.common.ejb.TestService");

        // this works again
        // service = (TestService) InitialContext.doLookup("java:app/myapp/MyappServiceBean");

        // how to lookup ONLY by interface name/class ?
        service = (TestService) InitialContext.doLookup("???/TestService");
    }

    ...
}

everyting is packed:

myapp.war
    |   
    + WEB-INF
        |
        + lib
        |   |
        |   + common.jar
        |       |
        |       - com.example.common.ejb.TestService (@Local interface)
        |       |
        |       - com.example.common.util.TestPojo (simple pojo, must lookup)
        |
        + classes
            |
            - com.example.myapp.ejb.MyappServiceBean (@Stateless impl)
            |
            - com.example.myapp.jsf.MyappBean (jsf @ManagedBean)

since i want to use common.jar inside different applications, i want a lookup based on Testservice interface name, much like i inject @EJB TestService service; in @ManagedBean or @WebServlet

how to achieve this?

like image 658
Michele Mariotti Avatar asked Nov 01 '22 05:11

Michele Mariotti


1 Answers

a dirty solution is to declare

@Stateless(name = "TestService")
public class MyappServiceBean implements TestService

and lookup it from pojo:

service = (TestService) InitialContext.doLookup("java:module/TestService");

any better solution will be happily accepted!

like image 91
Michele Mariotti Avatar answered Nov 09 '22 08:11

Michele Mariotti