Skip to content
Advertisement

Is there a way to package an EJB module with a WAR without an EAR?

I currently have the following project structure

EAR
|---myapp1.war
|---myapp2.war
|---myapp-ejb.jar

I would like to get rid of the ear and deploy myapp1 and myapp2 on their own. I tried to make myapp-ejb.jar a maven dependency of the two war and everything works fine at compile time. Nevertheless, there are a lot of jndi lookups in the code that fail at deploy time. Is there a way to make this to work?

Advertisement

Answer

I’m writing an answer to my own question, since I solved the issue. It is perfectly fine to use EJBs with a WAR only (no EAR) as stated here https://docs.oracle.com/cd/E19798-01/821-1841/gippi/index.html. As for the lookups calls, the correct way to do it is described here https://docs.oracle.com/cd/E19798-01/821-1841/girgn/index.html. The issue I was facing wasn’t really related to the lookups, as I was thinking, but it was due to the way most of the EJBs were initialized. I noticed that in most of the classes there was some nasty initialization code like the following:

@Stateless
public class FooResource
{
    FooEjb fooEjb = lookupFooEjb();
    
    private FooEjb lookupFooEjb()
    {
        javax.naming.Context c = new InitialContext();
        return (FooEjb) c.lookup("java:global/FooApplication/FooModule/FooEJB!FooInterface");
    }
}

This works fine with the EAR because the EJB module is loaded before the WAR archives, so the lookups do not fail. My guess is that when you package the EJBs with the WAR, it loads the EJBs as they are needed, computing the dependencies based on the @EJB annotation, so that kind of initialization fails since the EJB might be not loaded yet. To make it work, I just removed the lookup and added the annotation @EJB.

@Stateless
public class FooResource
{
    @EJB
    FooEjb fooEjb;
}

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement