Question

I am new to EJB and i'm trying to get a simple Stateless Session Bean working.

I'm using glassfish for this.

What i've done:

I've created a jar file containing the interface:

@Local
public interface SimpleStatelessBeanLocal {
    public String getHello();
}

I've then created a war file for my EJB containing the following class (with a dependency to the jar with the interface):

@Stateless
public class SimpleStatelessSessionBean implements SimpleStatelessBeanLocal {
    public String getHello() {
        return "Hello from stateless session bean";
    }
}

I then created a web application with a single servlet and a dependency to the jar with the interface.

@WebServlet("/SimpleServlet")
public class SimpleServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        InitialContext ctx;
        try {
            ctx = new InitialContext();
            Object object = ctx.lookup("java:global/simple-stateless-session-bean/SimpleStatelessSessionBean");
            response.getWriter().println(object);
            Class c = object.getClass();
            for (Class i : c.getInterfaces()) {
                response.getWriter().println(i.getName());
            }
            response.getWriter().println(object instanceof SimpleStatelessBeanLocal);
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }      
    }
}

This has the following output:

be.sdutry.ejb.tests.stateless.simple.SimpleStatelessBeanLocal_253329002
com.sun.enterprise.container.common.spi.util.IndirectlySerializable
be.sdutry.ejb.tests.stateless.simple.SimpleStatelessBeanLocal
false

so basicaly it finds the Bean, it implements that interface, but it's not an instance of that interface from the current classloader, which means i can't cast it.

Is there anything i'm doing wrong here? I'm pretty sure there has to be a way around this other than reflection?

What i already tried: A post i found suggested to put the jar with the interface on provided, but then i'm getting a ClassNotFoundException instead.

using:

  • GlassFish4
  • EJB 3.1
Was it helpful?

Solution

I've managed to get it working. The main problem was that the SLSB needed to be Remote because the code using it wasn't located in the same EAR-file.

The way i did it:

Jar containing only interfaces:

public interface SimpleStatelessBeanCommon {
    public String getHello();
}

@Local
public interface SimpleStatelessBeanLocal extends SimpleStatelessBeanCommon{
}

@Remote
public interface SimpleStatelessBeanRemote extends SimpleStatelessBeanCommon{
}

War containing Bean implementation (with dependency to jar with interfaces)

@Stateless
public class SimpleStatelessSessionBean implements SimpleStatelessBeanLocal, SimpleStatelessBeanRemote {
    public String getHello() {
        return "Hello from stateless session bean";
    }
}

War using the Bean (with dependency to jar with interfaces)

@WebServlet("/SimpleServlet")
public class SimpleServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @EJB
    SimpleStatelessBeanRemote simpleStatelessSessionBean;

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

I'll still take a look at what fvu posted.

Any optimization suggestions or remarks are still appreciated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top