Search code examples
jakarta-eeannotationsjndilookupejb-3.1

EJB lookup issue


I have a web project with a couple of EJBs in a different package. However I have an issue looking up for the EJBs.

My Directory Structure: have two packages index.job and index.ejb.

-- package index.job has a POJO index.java

-- package index.ejb has an ejb defined by @Stateless(name = "indexEJB", mappedName = "indexEJB") and @LocalBean

-- package index.ejb also has a local ejb interface defined @Local

My Local Bean

package index.ejb;

import java.util.ArrayList;
import javax.ejb.Local;

@Local
public interface IndexEJBLocal {
    public ArrayList getLatestVersions(String year, int start, int end);
}

My EJB

package index.ejb;

import javax.ejb.Stateless;
import javax.annotation.Resource;
import javax.ejb.LocalBean;

@Stateless(name = "indexEJB", mappedName = "indexEJB")
@LocalBean
public class IndexEJB implements IndexEJBLocal {

    @Resource(lookup = "jdbc/cap")
    private DataSource ds;

    @Override
    public ArrayList getLatestVersions(String year, int start, int end) {
        return null;
    }
}

My POJO

//ADDED @ManagedBean ANNOTATION SO SERVER IDENTIFIES THIS OBJ AS A RESOURCE
//Also cannot use @PostConstruct as the execute method is overridden from quartz job interface

package index.job;

import javax.annotation.ManagedBean;
import javax.ejb.EJB;
import javax.inject.Inject;

@ManagedBean
public class IndexJob implements Job {

    @EJB
    IndexEJB billIndexEJB1;

    @EJB
    IndexEJBLocal billIndexEJB2;

    @Inject
    IndexEJB billIndexEJB3;

    @Inject
    IndexEJBLocal billIndexEJB4;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        System.out.println("1::"+billIndexEJB1); //ALWAYS NULL
        System.out.println("2::"+billIndexEJB2); //ALWAYS NULL
        System.out.println("3::"+billIndexEJB3); //ALWAYS NULL
        System.out.println("4::"+billIndexEJB4); //ALWAYS NULL


        try {
            Context ctx = new InitialContext();
            IndexEJBLocal billIndex = (IndexEJBLocal) 
                ctx.lookup("java:global.MY-PROJECT-NAME.IndexEJB!index.ejb.IndexEJBLocal");

            //ABOVE LOOKUP ALWAYS FAILS WITH NameNotFoundException

            System.out.println("billIndex::" + billIndex);
        } catch (Exception e) {
        e.printStackTrace();
        }
    }
}

Any hints on how this could be resolved would be highly appreciated.

My project is running on: NetBeans 8.2; JDK 1.8b45; Java EE 7 Web; WebLogic 12.2.1.1


Solution

  • Whenever you annotate a bean as @LocalBean, making the bean expose a no-interface view, the reference returned by the lookup will be a reference to the EJB class and not the interface. The @LocalBean annotation exposes all of the methods within the EJB implementation and not only the methods defined in the interface.

    The exception you are getting is probably telling you that there is no bean with an IndexEJBLocal view.

    Your simplest solution is to remove the @LocalBean annotation which, I believe, is what you want.