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