Search code examples
eclipseservletsjakarta-eeejbwildfly

EJB interface references from a separated Web Project


I am doing some EJB 3.1 practices on Eclipse.

I have an EJB Project and a Dynamic Web Project on Wildfly 9.

What I am trying to do is to call an EJB method (the EJB Project) from a Servlet (in the Dynamic Web Project) using a @EJB annotation on the Servlet code:

@EJB
IBeanRemote bean; //realize the bean variable is declared as the remote EJB Interfase.

protected void doGet(HttpServletRequest request, HttpServletResponse    response) throws ServletException, IOException {    
    response.getWriter().println(bean.getMethod());
}

and my EJB is pretty simple:

@Remote
@Stateless
public interfase IBeanRemote {
   public String getMethod();
}
...
public BeanRemote extends IBeanRemote
{
    @Override
    public String getMethod() { 
        return "How to call this ejb?"; 
    }
}

Both projects are being packaged into a EAR Project, but I am not able to see the IBeanRemote type reference from the Web Project (because IBeanRemote and the Servlet are in separated projects), so, the line

@EJB
IBeanRemote bean;

can not be compiled.

A common solution could be by adding the EJB project in the Web Project Build Path, but it makes no sense if I am using a Remote EJB that runs on a very remote Server and I suppose the Web Project should not know anything about the EJB implementation.

How can I get the IRemoteBean reference type into my Web Project in order to make my Servlet class compile and call the EJB method in eclipse?


Solution

  • why don't you separate your EJB project to 2 smaller projects: EJB-API which contains the interface and you can use this project for your client, and EJB-IMPL which implements the interface. Both your client and EJB-IMPL will have the reference to your EJB-API project.