0

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
stefaan dutry
  • 1,096
  • 1
  • 10
  • 21
  • 1
    Not directly related, but I'm wondering why you use context lookup and not @EJB injection? Also, EJB3.1 is intended to be simple (and it is, as long as you follow the rulebook :) - what you try here should work even without interfaces, as EJB 3.1 Lite), may I suggest the most excellent [Oracle JavaEE tutorial](http://docs.oracle.com/javaee/7/tutorial/doc/javaeetutorial7.pdf) to help you get started? And – fvu Nov 14 '13 at 13:13
  • @fvu following the documentation link you posted, i was able to make a successfull working SLSB example. The downside from this example is that the SLSB and the actual code are in the same war-file. What i actualy need is the ejb to be in a seperate war, since eventualy we'll have multiple projects using the same EJBs. My next step is to see if it still works if i separate them in 2 different wars. – stefaan dutry Nov 14 '13 at 13:50
  • see chapter 5.2 of the tutorial wrt packaging, for your application I'd package and deploy the EJBs as an EJB-jar, EJB3 style - optionally wrapped together with the WAR inside an EAR. Now, v7 of the tutorial seems to focus heavily on the new interfaceless model, you'll notice that [version 5 of the same tutorial](http://docs.oracle.com/javaee/5/tutorial/doc/) shows a full example EJB that exposes explicit local and remote interfaces, it's probably easier to start with that example. – fvu Nov 14 '13 at 18:18

1 Answers1

0

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.

stefaan dutry
  • 1,096
  • 1
  • 10
  • 21