Resolving EJB reference in Servlet - EJB 3/WAS 7 - Websphere

This is a discussion on Resolving EJB reference in Servlet - EJB 3/WAS 7 - Websphere ; Hello everyone, I'm using WAS 7.0.0.5 and I'm trying to deploy a couple of EAR files that were not built using RAD. This application deploys and runs correctly on Sun GlassFish v2. I have a servlet called ConsoleServer that defines ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: Resolving EJB reference in Servlet - EJB 3/WAS 7

  1. Resolving EJB reference in Servlet - EJB 3/WAS 7

    Hello everyone,

    I'm using WAS 7.0.0.5 and I'm trying to deploy a couple of EAR files that were not built using RAD. This application deploys and runs correctly on Sun GlassFish v2.

    I have a servlet called ConsoleServer that defines an ejb-ref using an @EJB annotation as such:

    @EJB(name="ejb/BamGateway", beanInterface=BamGateway.class)
    public class ConsoleServer extends HttpServlet {

    The BamGateway EJB is located in the BamServer application that's installed on the same server and it's mapped to the JNDI name ejb/BamGateway. Later, in a utility class called by the ConsoleServer servlet I do a lookup of the bean:

    new InitialContext().lookup("java:comp/env/ejb/BamGateway");

    I've tried to resolve the ejb/BamGateway ejb-ref to the global JNDI name ejb/BamGateway using an ibm-web-bnd.xml file in the ConsoleServer WAR file. Here's what that looks like:







    When the server starts, I get the following messages in the SystemOut.log:

    0000000d ApplicationMg A WSVR0200I: Starting application: BamServerEar
    0000000d EJBContainerI I CNTR0167I: The server is binding the com.solcorp.bamserver.gateway.BamGateway interface of the BamGatewayBean enterprise bean in the BamServerEjb.jar module of the BamServerEar application. The binding location is: ejb/BamGateway
    ...
    0000000d ApplicationMg A WSVR0200I: Starting application: ConsoleServerEar
    0000000d serialization W EjbRefBindingImpl(anon) eResolveProxy No matching ref for name [ ejb/BamGateway ]

    When I ignore the warning about the ref name and invoke the code that does the lookup, I get the following error:

    00000017 servlet E com.ibm.ws.webcontainer.servlet.ServletWrapper service SRVE0068E: Uncaught exception created in one of the service methods of the servlet ConsoleServer in application ConsoleServerEar. Exception created : java.lang.ClassCastException: com.solcorp.bamserver.gateway._BamGateway_Stub incompatible with com.solcorp.bamserver.gateway.BamGateway
    at com.solcorp.consoleserver.controllers.BamLoggingCo nsoleController.createBamGateway(BamLoggingConsole Controller.java:68)
    at com.solcorp.consoleserver.controllers.BamLoggingCo nsoleController.executeAction(BamLoggingConsoleCon troller.java:85)
    at com.solcorp.consoleserver.ConsoleServer.processReq uest(ConsoleServer.java:180)
    at com.solcorp.consoleserver.ConsoleServer.doGet(Cons oleServer.java:91)
    ...

    I've got a couple of questions:

    1) Why can I not resolve the ejb-ref for the servlet through the administrative console? If I inject a reference to the BamGateway EJB into a class member variable of the servlet then the reference is listed in the console and can be resolved without the ibm-web-bnd.xml file. The EJB reference is not listed in the administrative console when you use the @EJB annotation on the servlet.

    2) Why does the "No matching ref for name" warning appear? Is there a special syntax for the name attribute of the ejb-ref element in the ibm-web-bnd.xml file?

    3) The com.solcorp.bamserver.gateway.BamGateway is located in a jar that's copied into both the BamServer and the ConsoleServer's EAR. Do I need to manually generate stubs with WAS or is the ClassCastException just a side-effect of the warning about the ejb-ref?

    Thanks for reading and I appreciate any help you can offer.

    Joe

  2. Re: Resolving EJB reference in Servlet - EJB 3/WAS 7

    Sorry, I made a mistake in my question 1. The resource reference can't be resolved in the admin console, even if you annotate a servlet member class variable.

    Joe

  3. Re: Resolving EJB reference in Servlet - EJB 3/WAS 7

    Well, I've determined why the @EJB annotation at the servlet level isn't being processed. The classes that make up my servlet are contained in a jar that's stored in the lib folder of the EAR. WAS 7 isn't processing the annotations in that case. If the jar is copied to the web-inf/lib folder of the WAR or the classes are extracted out to the web-inf/classes folder then the annotation is processed and the EJB reference shows up in the admin console.

    Still not sure about the ClassCastException though.

  4. Re: Resolving EJB reference in Servlet - EJB 3/WAS 7

    It turns out that I can eliminate the ClassCastException by using PortableRemoteObject.narrow() on the reference returned by Context.lookup(). The code works properly once that's done.

    The lookup() works properly on GlassFish 9.1 v2 without the narrow(). Is it true that narrow() is required when doing a lookup() on a remote interface within the same server on WAS 7 or is there something else going on here?

    Does this mean that narrow() is a requirement for portable code on all remote EJB references?

+ Reply to Thread