重新部署EJB后的javax.ejb.NoSuchEJBException

时间:2011-01-12 09:59:41

标签: java java-ee glassfish ejb-3.1 cdi

使用Glassfish 3.0.1 ...如果我有一个Web应用程序远程访问另一个应用程序中的EJB,并且重新部署了包含EJB的远程应用程序,我会得到一个javax.ejb.NoSuchEJBException(参见下面的stacktrace)。

不应该这样吗?我可以看到使用完全相同的JNDI名称成功部署了有问题的EJB。

除了重启Web应用程序之外,还有其他方法可以解决此问题吗?应该注意的是,在这个堆栈跟踪来自的特定示例中,我正在访问一个用CDI注入bean的servlet:

public class StatusServlet extends HttpServlet {

    @Inject
    private StatusService statusService;

    @Override
    public void doGet(final HttpServletRequest req, final HttpServletResponse res) throws IOException {
        res.getWriter().write(statusService.getStatus());
    }
}

使用以下生产者完成注入以获得正确的EJB:

public class StatusServiceProducer extends AbstractServiceProducer {

 @EJB(name = "StatusService")
 private StatusService service;

 @Produces
 public StatusService getService(final InjectionPoint ip) {
  return service;
 }

}

生成器用于使服务更容易包装在代理中,并且可以更容易地更改EJB的查找方式。

StatusService接口和实现如下:

@Stateless(name = "StatusService")
public class StatusServiceImpl implements StatusService {
    private static final String OK = "OK";

    public String getStatus() {
        // Some code
        return OK;
    }
}
public interface StatusService {
 String getStatus();
}

修改:提供了我自己的solution。还有更好的吗?


完整的堆栈跟踪:

[#|2011-01-12T10:45:28.273+0100|WARNING|glassfish3.0.1|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=50;_ThreadName=http-thread-pool-8080-(1);|StandardWrapperValve[Load
Balancer status servlet]: PWC1406: Servlet.service() for servlet Load
Balancer status servlet threw exception
javax.ejb.NoSuchEJBException
       at org.example.service._StatusService_Wrapper.getStatus(org/example/service/_StatusService_Wrapper.java)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:597)
       at no.evote.service.cache.ServiceInvocationHandler.invoke(ServiceInvocationHandler.java:34)
       at $Proxy760.getStatus(Unknown Source)
       at no.evote.presentation.StatusServlet.doGet(StatusServlet.java:25)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
       at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
       at net.balusc.http.multipart.MultipartFilter.doFilter(MultipartFilter.java:78)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:277)
       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
       at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
       at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
       at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
       at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
       at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
       at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
       at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
       at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
       at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
       at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
       at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
       at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
       at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
       at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
       at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
       at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
       at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
       at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
       at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
       at java.lang.Thread.run(Thread.java:662)
Caused by: java.rmi.NoSuchObjectException: CORBA OBJECT_NOT_EXIST
1330446338 No; nested exception is:
       org.omg.CORBA.OBJECT_NOT_EXIST: ----------BEGIN server-side stack
trace----------
org.omg.CORBA.OBJECT_NOT_EXIST:   vmcid: OMG  minor code: 2  completed: No
       at com.sun.corba.ee.impl.logging.OMGSystemException.noObjectAdaptor(OMGSystemException.java:3457)
       at com.sun.corba.ee.impl.logging.OMGSystemException.noObjectAdaptor(OMGSystemException.java:3475)
       at com.sun.corba.ee.impl.oa.poa.POAFactory.find(POAFactory.java:222)
       at com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.findObjectAdapter(CorbaServerRequestDispatcherImpl.java:450)
       at com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatch(CorbaServerRequestDispatcherImpl.java:209)
       at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequestRequest(CorbaMessageMediatorImpl.java:1841)
       at com.sun.corba.ee.impl.protocol.SharedCDRClientRequestDispatcherImpl.marshalingComplete(SharedCDRClientRequestDispatcherImpl.java:119)
       at com.sun.corba.ee.impl.protocol.CorbaClientDelegateImpl.invoke(CorbaClientDelegateImpl.java:235)
       at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.privateInvoke(StubInvocationHandlerImpl.java:187)
       at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.invoke(StubInvocationHandlerImpl.java:147)
       at com.sun.corba.ee.impl.presentation.rmi.codegen.CodegenStubBase.invoke(CodegenStubBase.java:225)
       at no.evote.service.__StatusService_Remote_DynamicStub.getStatus(no/evote/service/__StatusService_Remote_DynamicStub.java)
       at no.evote.service._StatusService_Wrapper.getStatus(no/evote/service/_StatusService_Wrapper.java)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:597)
       at no.evote.service.cache.ServiceInvocationHandler.invoke(ServiceInvocationHandler.java:34)
       at $Proxy760.getStatus(Unknown Source)
       at no.evote.presentation.StatusServlet.doGet(StatusServlet.java:25)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
       at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
       at net.balusc.http.multipart.MultipartFilter.doFilter(MultipartFilter.java:78)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:277)
       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
       at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
       at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
       at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
       at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
       at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
       at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
       at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
       at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
       at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
       at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
       at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
       at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
       at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
       at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
       at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
       at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
       at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
       at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
       at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
       at java.lang.Thread.run(Thread.java:662)
Caused by: org.omg.PortableServer.POAPackage.AdapterNonExistent:
IDL:omg.org/PortableServer/POA/AdapterNonExistent:1.0
       at com.sun.corba.ee.impl.oa.poa.POAImpl.find_POA(POAImpl.java:1057)
       at com.sun.corba.ee.impl.oa.poa.POAFactory.find(POAFactory.java:218)
       ... 48 more

----------END server-side stack trace----------  vmcid: OMG  minor
code: 2  completed: No
       at com.sun.corba.ee.impl.javax.rmi.CORBA.Util.mapSystemException(Util.java:280)
       at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.privateInvoke(StubInvocationHandlerImpl.java:200)
       at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.invoke(StubInvocationHandlerImpl.java:147)
       at com.sun.corba.ee.impl.presentation.rmi.codegen.CodegenStubBase.invoke(CodegenStubBase.java:225)
       at no.evote.service.__StatusService_Remote_DynamicStub.getStatus(no/evote/service/__StatusService_Remote_DynamicStub.java)
       ... 39 more
Caused by: org.omg.CORBA.OBJECT_NOT_EXIST: ----------BEGIN server-side
stack trace----------
org.omg.CORBA.OBJECT_NOT_EXIST:   vmcid: OMG  minor code: 2  completed: No
       at com.sun.corba.ee.impl.logging.OMGSystemException.noObjectAdaptor(OMGSystemException.java:3457)
       at com.sun.corba.ee.impl.logging.OMGSystemException.noObjectAdaptor(OMGSystemException.java:3475)
       at com.sun.corba.ee.impl.oa.poa.POAFactory.find(POAFactory.java:222)
       at com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.findObjectAdapter(CorbaServerRequestDispatcherImpl.java:450)
       at com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatch(CorbaServerRequestDispatcherImpl.java:209)
       at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequestRequest(CorbaMessageMediatorImpl.java:1841)
       at com.sun.corba.ee.impl.protocol.SharedCDRClientRequestDispatcherImpl.marshalingComplete(SharedCDRClientRequestDispatcherImpl.java:119)
       at com.sun.corba.ee.impl.protocol.CorbaClientDelegateImpl.invoke(CorbaClientDelegateImpl.java:235)
       at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.privateInvoke(StubInvocationHandlerImpl.java:187)
       at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.invoke(StubInvocationHandlerImpl.java:147)
       at com.sun.corba.ee.impl.presentation.rmi.codegen.CodegenStubBase.invoke(CodegenStubBase.java:225)
       at no.evote.service.__StatusService_Remote_DynamicStub.getStatus(no/evote/service/__StatusService_Remote_DynamicStub.java)
       at no.evote.service._StatusService_Wrapper.getStatus(no/evote/service/_StatusService_Wrapper.java)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:597)
       at no.evote.service.cache.ServiceInvocationHandler.invoke(ServiceInvocationHandler.java:34)
       at $Proxy760.getStatus(Unknown Source)
       at no.evote.presentation.StatusServlet.doGet(StatusServlet.java:25)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
       at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
       at net.balusc.http.multipart.MultipartFilter.doFilter(MultipartFilter.java:78)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:277)
       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
       at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
       at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
       at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
       at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
       at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
       at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
       at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
       at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
       at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
       at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
       at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
       at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
       at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
       at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
       at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
       at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
       at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
       at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
       at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
       at java.lang.Thread.run(Thread.java:662)
Caused by: org.omg.PortableServer.POAPackage.AdapterNonExistent:
IDL:omg.org/PortableServer/POA/AdapterNonExistent:1.0
       at com.sun.corba.ee.impl.oa.poa.POAImpl.find_POA(POAImpl.java:1057)
       at com.sun.corba.ee.impl.oa.poa.POAFactory.find(POAFactory.java:218)
       ... 48 more

----------END server-side stack trace----------  vmcid: OMG  minor
code: 2  completed: No
       at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
       at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
       at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
       at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
       at com.sun.corba.ee.impl.protocol.giopmsgheaders.MessageBase.getSystemException(MessageBase.java:913)
       at com.sun.corba.ee.impl.protocol.giopmsgheaders.ReplyMessage_1_2.getSystemException(ReplyMessage_1_2.java:129)
       at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.getSystemExceptionReply(CorbaMessageMediatorImpl.java:681)
       at com.sun.corba.ee.impl.protocol.CorbaClientRequestDispatcherImpl.processResponse(CorbaClientRequestDispatcherImpl.java:510)
       at com.sun.corba.ee.impl.protocol.SharedCDRClientRequestDispatcherImpl.marshalingComplete(SharedCDRClientRequestDispatcherImpl.java:153)
       at com.sun.corba.ee.impl.protocol.CorbaClientDelegateImpl.invoke(CorbaClientDelegateImpl.java:235)
       at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.privateInvoke(StubInvocationHandlerImpl.java:187)
       ... 42 more
|#]

3 个答案:

答案 0 :(得分:2)

唯一的解决方案是不使用@EJB注入作为servlet中的一个字段(它在类加载时被注入,因此应用程序启动或首次使用servlet,而是为每个请求请求一个新实例。 遗憾的是,这确实意味着您无法使用@EJB注入注释,并且必须使用JNDI查找等以旧方式处理EJB。

答案 1 :(得分:1)

来自Glassfish论坛的discussion

  

只有当EJB容器创建注入的新实例时,才会更新注入的引用。因此,如果您一直查找它,您将获得具有更新引用的实例,但如果您的代码保留在原始实例上,或者如果它被合并,则注入的引用仍然是陈旧的。

所以根本问题是对EJB的引用是陈旧的。我找到了两个解决这个问题的方法:

  1. 在每个请求上查找EJB(不要使用CDI)
  2. 在代理中包装EJB,如果引用是陈旧的,则查找EJB
  3. 使用第二种解决方案,仍然可以使用CDI。您需要一个制作者和一个InvocationHandler。下面的代码是我在我的项目中使用的代码的简化版本,它可能无法正确编译,但它将显示用于实现此工作的想法和概念。 YMMV。

    首先,在使用EJB的代码中,使用@Inject,如下所示:

    public class Foo {
        @Inject
        MyEjb obj;
    }
    

    其次,您需要一个只返回EJB的包装版本的生产者:

    public class MyEjbProducer {
        @EJB(name = "MyEjb")
        private MyEjb obj;
    
        @Produces
        public MyEjb getEjb(final InjectionPoint ip) {
            InvocationHandler handler = new MyInvocationHandler(obj, MyEjb.class);
            return (MyEjb)Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), handler);
        }
    }
    

    第三,调用处理程序需要测试NoSuchEjbException并再次查找bean:

    public class MyInvocationHandler implements InvocationHandler {
        private MyEjb obj;
        private Class lookupClass;
    
        public MyInvocationHandler(MyEjb obj, Class lookupClass) {
            this.obj = obj;
            this.lookupClass = lookupClass;
        }
    
        public Object invoke (...) {
            try {
                return method.invoke(impl, args);
            } catch (final InvocationTargetException e) {
                if (e.getTargetException() instanceof NoSuchEJBException) {
                    // 1. look up EJB again
                    // 2. try calling invoke again                    
                }
            }
        }
    }
    

    此代码绝不完整。一些代码,例如查找EJB并再次调用该方法,已被省略。

    这样做会起作用。另一种方法是做类似的事情,但是向EJB添加一个单独的方法来检查陈旧性,而不是使用用户试图调用的方法。

    你怎么看?有更好的解决方案吗?

答案 2 :(得分:1)

此处已更正:

https://wikis.oracle.com/display/GlassFish/GlassFishv3.1EJB

保留独特的应用。重新部署的id / ejb id MS5 Mahesh EJB-6-1由MS3完成

因此,Glassfish 3.1应该可以正常使用。