2011-08-26 78 views
0

我找到了一些同名的文章。不幸的是,我找不到任何解決方案來解決我的錯誤。javax.ejb.EJBException:javax.ejb.CreateException:無法創建無狀態的EJB

我是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/[email protected]@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/[email protected]@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 

回答

1

的JMS目的地jms/NotificationQueue通常需要被創建。指南的確沒有提到它。

也許Netbeans嚮導自動爲您創建它,並且您還沒有通過嚮導創建MDB?

編輯:

的指南並提到它:

  • In the New Message-Driven Bean dialog, specify the EJB Name as NotificationBean and the package as "com.customerapp.mdb", click on the "Add..." button of the Project Destination option
  • In the Add Message Destination dialog, specify the Destination Name as NotificationQueue and keep the Destination Type as Queue and click OK

這讓嚮導創建一個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實現都有不同的語法)。

+0

謝謝你回答我, 嗯..我使用NetBeans嚮導創建NotificationBean(按照Netbeans指南)。 我在管理控制檯中檢查了Glassfish,創建了JMS目標jms/NotificationQueue。 – hainp

+0

您可能想要嘗試刪除嚮導在EJB模塊中創建的不必要的xml文件:轉到Files視圖,然後轉到CustomerApp-ejb - > src - > conf並刪除清單和glassfish-ejb.xml文件(只有peristence.xml應該保留)。特別是glassfish-ejb.xml是完全沒有必要的(因爲EJB已經註釋了),在這種情況下實際上可能是有害的。 – akira

+0

p.s.我也很快跟上了指南,而且這裏工作起來。 – akira

1

與GlassFish ejb-jar.xml中和清潔&構建的刪除幫助我的情況下(Netbeans的7.1.1,3.1.2的GlassFish)