javax.ejb.EJBException:javax.ejb.CreateException:无法创建无状态EJB

时间:2011-08-26 11:56:41

标签: java-ee jsf-2 ejb-3.1

我找到了一些同名文章。不幸的是,我无法找到任何解决方案。

我是JEE,JSF,EJB的新手。我正在使用Netbeans 7.0.1。我正在遵循本指南:http://wiki.netbeans.org/DevelopJavaEE6App

这是:  CustomerSessionBean:

@Stateless
@LocalBean
public class CustomerSessionBean {

@PersistenceContext
private EntityManager em;
@Resource(name = "jms/NotificationQueue")
private Queue notificationQueue;
@Resource(name = "jms/NotificationQueueFactory")
private ConnectionFactory notificationQueueFactory;

public List<Customer> retrieve() {
    Query query = em.createNamedQuery("Customer.findAll");
    return query.getResultList();
}

public Customer update(Customer customer) {
    Customer updated = em.merge(customer);
    try {
        sendJMSMessageToNotificationQueue(updated);
    } catch (JMSException ex) {
        Logger.getLogger(CustomerSessionBean.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println("Customer updated in CustomerSessionBean!");
    return updated;
}

private Message createJMSMessageForjmsNotificationQueue(Session session, Object messageData) throws JMSException {

    ObjectMessage tm = session.createObjectMessage();
    tm.setObject((Serializable) messageData);
    return tm;
}

private void sendJMSMessageToNotificationQueue(Object messageData) throws JMSException {
    Connection connection = null;
    Session session = null;
    try {
        connection = notificationQueueFactory.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer messageProducer = session.createProducer(notificationQueue);
        messageProducer.send(createJMSMessageForjmsNotificationQueue(session, messageData));
    } finally {
        if (session != null) {
            try {
                session.close();
            } catch (JMSException e) {
                Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot close session", e);
            }
        }
        if (connection != null) {
            connection.close();
        }
    }
}

public List<DiscountCode> getDiscountCodes() {
    Query q = em.createNamedQuery("DiscountCode.findAll");
    return q.getResultList();
}
}

我收到此错误:“javax.ejb.EJBException:javax.ejb.CreateException:无法创建无状态EJB”每次访问jsf页面时(在指南中:CustomerList.jsp; CustomerDetails.jsp)如果我不知道不要评论“@Resource(name =”jms / NotificationQueue“)”。如果我发表评论,将显示jsf页面。

编辑:这是堆栈跟踪:

javax.ejb.EJBException: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB
at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:454)
at com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:2528)
at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1895)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
at $Proxy226.retrieve(Unknown Source)
at com.customerapp.ejb.__EJB31_Generated__CustomerSessionBean__Intf____Bean__.retrieve(Unknown Source)
at com.customerapp.web.CustomerMBean.getCustomers(CustomerMBean.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:302)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
at com.sun.el.parser.AstValue.getValue(AstValue.java:116)
at com.sun.el.parser.AstValue.getValue(AstValue.java:163)
at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219)
at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:55)
at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:55)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
at javax.faces.component.UIData.getValue(UIData.java:731)
at javax.faces.component.UIData.getDataModel(UIData.java:1798)
at javax.faces.component.UIData.setRowIndexWithoutRowStatePreserved(UIData.java:484)
at javax.faces.component.UIData.setRowIndex(UIData.java:473)
at com.sun.faces.renderkit.html_basic.TableRenderer.encodeBegin(TableRenderer.java:81)
at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:820)
at javax.faces.component.UIData.encodeBegin(UIData.java:1118)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1754)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1756)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1759)
at com.sun.faces.application.view.JspViewHandlingStrategy.doRenderView(JspViewHandlingStrategy.java:432)
at com.sun.faces.application.view.JspViewHandlingStrategy.renderView(JspViewHandlingStrategy.java:233)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)
Caused by: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB
at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:726)
at com.sun.ejb.containers.util.pool.NonBlockingPool.getObject(NonBlockingPool.java:247)
at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:449)
... 70 more
Caused by: javax.ejb.CreateException: Could not create stateless EJB
at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:534)
at com.sun.ejb.containers.StatelessSessionContainer.access$000(StatelessSessionContainer.java:95)
at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:724)
... 72 more
Caused by: java.lang.IllegalStateException: Exception attempting to inject Unresolved Message-Destination-Ref jms/NotificationQueue@java.lang.String@null into class com.customerapp.ejb.CustomerSessionBean: Lookup failed for 'java:comp/env/jms/NotificationQueue' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming}
at org.glassfish.weld.services.InjectionServicesImpl.aroundInject(InjectionServicesImpl.java:137)
at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:50)
at org.jboss.weld.bean.SessionBean$1.inject(SessionBean.java:190)
at org.glassfish.weld.services.JCDIServiceImpl.injectEJBInstance(JCDIServiceImpl.java:223)
at com.sun.ejb.containers.BaseContainer.injectEjbInstance(BaseContainer.java:1675)
at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:494)
... 74 more
Caused by: com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Unresolved Message-Destination-Ref jms/NotificationQueue@java.lang.String@null into class com.customerapp.ejb.CustomerSessionBean: Lookup failed for 'java:comp/env/jms/NotificationQueue' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming}
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:703)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.inject(InjectionManagerImpl.java:470)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.injectInstance(InjectionManagerImpl.java:171)
at org.glassfish.weld.services.InjectionServicesImpl.aroundInject(InjectionServicesImpl.java:130)
... 79 more
Caused by: javax.naming.NamingException: Lookup failed for 'java:comp/env/jms/NotificationQueue' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: No object bound for java:comp/env/jms/NotificationQueue [Root exception is java.lang.NullPointerException]]
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:599)
... 82 more
Caused by: javax.naming.NameNotFoundException: No object bound for java:comp/env/jms/NotificationQueue [Root exception is java.lang.NullPointerException]
at com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.java:242)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:498)
... 86 more
Caused by: java.lang.NullPointerException
at com.sun.enterprise.naming.impl.WrappedSerialContext.getURLScheme(WrappedSerialContext.java:146)
at com.sun.enterprise.naming.impl.WrappedSerialContext.getURLOrDefaultInitCtx(WrappedSerialContext.java:103)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at com.sun.enterprise.naming.util.JndiNamingObjectFactory.create(JndiNamingObjectFactory.java:82)
at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:776)
at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:744)
at com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.java:172)
... 87 more

2 个答案:

答案 0 :(得分:1)

通常需要创建JMS目标jms/NotificationQueue。指南确实没有提到它。

也许Netbeans向导会自动为您创建它并且您还没有通过向导创建MDB?

编辑:

指南确实提到了它:

  
      
  • 在New Message-Driven Bean对话框中,将EJB Name指定为   将NotificationBean和包作为“com.customerapp.mdb”,单击   “项目目的地”选项的“添加...”按钮
  •   
  • 在“添加消息目标”对话框中,指定“目标名称”   作为NotificationQueue并将目标类型保留为队列,然后单击确定
  •   

这使向导可以创建一个glassfish-resources.xml文件,用于定义队列:

<resources>
    <admin-object-resource enabled="true" jndi-name="jms/NotificationQueue"  res-type="javax.jms.Queue"  res-adapter="jmsra">
        <property name="Name" value="PhysicalQueue"/>
    </admin-object-resource>
    <connector-connection-pool name="jms/NotificationQueueFactoryPool"  connection-definition-name="javax.jms.QueueConnectionFactory"  resource-adapter-name="jmsra"/>
    <connector-resource enabled="true" jndi-name="jms/NotificationQueueFactory" pool-name="jms/NotificationQueueFactoryPool"  />
</resources>

如果您没有遵循信函指南和/或未使用Glassfish,您必须自己定义(注意每个Java EE实现都有不同的语法)。

答案 1 :(得分:1)

在我的案例中删除 glassfish-ejb-jar.xml 和Clean&amp; Build帮助(Netbeans 7.1.1,glassfish 3.1.2)