javaejb-3.0remote-accessclasscastexception

EJB 3 RemoteInterface class cast exception during lookup


I am using WebSphere Application Server 8.0.0.1. I have two enterprise applications running in same jvm (at the moment).

I need to call an EJB-method remotely

  1. Application ("service") Interface:

    package myfirstpackage.ejb3;
    @Remote
    public interface TriggerManually {
        public boolean runTask(String taskName);
        public boolean resetTasks();
    }
    

    Implementation:

    package anotherpackage.ejb.remoteclient;
    @Stateless
    public class TriggerManuallyBean implements TriggerManually {
        @Override
        public boolean runTask(String taskName) {
            //Syso...
            return false;
        }
    
        @Override
        public boolean resetTasks() {
            //Syso...
            return false;
        }
    
    }
    
  2. Application ("client") uses the service (the interface is shared)

    public class TriggerManuallyClient {
      public void test() {
        Properties props = new Properties();
    
        props.put(Context.INITIAL_CONTEXT_FACTORY,
                "com.ibm.websphere.naming.WsnInitialContextFactory");
        props.put(javax.naming.Context.PROVIDER_URL, "iiop://localhost:2811");
    
        TriggerManually triggerManually;
        try {
            InitialContext ctx = new InitialContext(props);
            triggerManually = (TriggerManually) ctx
                    .lookup("myjdniname");
            triggerManually.resetTasks();
    
        } catch (NamingException e) {
            e.printStackTrace();
        }
      }
    }
    

But I get a Class Cast Exception during lookup:

Exception created : [java.lang.ClassCastException: myfirstpackage.ejb3._TriggerManually_Stub incompatible with anotherpackage.ejb.remoteclient.TriggerManually
    at anotherpackage.ejb.remoteclient.TriggerManuallyClient.test(TriggerManuallyClient.java:20)
    at com.ibm._jsp._status._jspService(_status.java:112)
    at com.ibm.ws.jsp.runtime.HttpJspBase.service(HttpJspBase.java:99)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1147)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:722)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:449)
    at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
    at com.ibm.wsspi.webcontainer.servlet.GenericServletWrapper.handleRequest(GenericServletWrapper.java:122)
    at com.ibm.ws.jsp.webcontainerext.AbstractJSPExtensionServletWrapper.handleRequest(AbstractJSPExtensionServletWrapper.java:205)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.invokeTarget(WebAppFilterChain.java:125)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:77)
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:919)
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1016)
    at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:87)
    at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:886)
    at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1655)
    at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:195)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)
    at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:83)
    at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
    at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
    at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
    at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
    at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
    at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
    at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1650)

I thougt in EJB 3 i dont have to create stubs. How can I solve my issue?


Solution

  • The problem is not the stub (and yes those still exist in EJB3) returned via lookup.

    Look carefully at the ClassCastException:

    myfirstpackage.ejb3._TriggerManually_Stub incompatible with anotherpackage.ejb.remoteclient.TriggerManually

    You probably have a TriggerManually class in package anotherpackage.ejb.remoteclient as well.

    This will probably fix it:

    myfirstpackage.ejb3.TriggerManually triggerManually;
    try {
        InitialContext ctx = new InitialContext(props);
        triggerManually = (myfirstpackage.ejb3.TriggerManually) ctx.lookup("myjdniname");
        triggerManually.resetTasks();
    
    } catch (NamingException e) {
        e.printStackTrace();
    }