2012-05-24 61 views
0

我正在嘗試在我的GlassFish項目中設置JMS。Java EE 6 Java消息傳遞服務無法執行的空指針異常

我遵循本書中的指導原則:「帶有GlassFish 3應用程序服務器的Java EE 6」。

我將連接器資源設置爲「jms/GlassFishWebDevConnectionFactory」,隊列設置爲「jms/GlassFishWebDevQueue」。

下面的代碼執行一個Singleton出來的Bean:

public class MessageSender { 
    @Resource(mappedName = "jms/GlassFishWebDevConnectionFactory") 
    private ConnectionFactory connectionFactory; 
    @Resource(mappedName = "jms/GlassFishWebDevQueue") 
    private Queue queue; 
    public void postMessages() { 
     try { 
      Connection con = connectionFactory.createConnection(); 
     }catch (JMSException ex) { 
      Logger.getLogger(MessageSender.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 
} 

應用程序的初始化在失敗 「連接CON = connectionFactory.createConnection();」帶有NullPointerException。

這是完整的堆棧跟蹤:

javax.ejb.CreateException: Initialization failed for Singleton WorldBean 
    at com.sun.ejb.containers.AbstractSingletonContainer.createSingletonEJB(AbstractSingletonContainer.java:547) 
    at com.sun.ejb.containers.AbstractSingletonContainer.access$100(AbstractSingletonContainer.java:79) 
    at com.sun.ejb.containers.AbstractSingletonContainer$SingletonContextFactory.create(AbstractSingletonContainer.java:719) 
    at com.sun.ejb.containers.AbstractSingletonContainer.instantiateSingletonInstance(AbstractSingletonContainer.java:451) 
    at org.glassfish.ejb.startup.SingletonLifeCycleManager.initializeSingleton(SingletonLifeCycleManager.java:216) 
    at org.glassfish.ejb.startup.SingletonLifeCycleManager.initializeSingleton(SingletonLifeCycleManager.java:210) 
    at org.glassfish.ejb.startup.SingletonLifeCycleManager.initializeSingleton(SingletonLifeCycleManager.java:177) 
    at org.glassfish.ejb.startup.SingletonLifeCycleManager.doStartup(SingletonLifeCycleManager.java:155) 
    at org.glassfish.ejb.startup.EjbApplication.start(EjbApplication.java:177) 
    at org.glassfish.internal.data.EngineRef.start(EngineRef.java:130) 
    at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:269) 
    at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:301) 
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:461) 
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240) 
    at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:389) 
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:348) 
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:363) 
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1085) 
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1200(CommandRunnerImpl.java:95) 
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1291) 
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1259) 
    at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:461) 
    at com.sun.enterprise.v3.admin.AdminAdapter.service(AdminAdapter.java:212) 
    at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:179) 
    at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:117) 
    at com.sun.enterprise.v3.services.impl.ContainerMapper$Hk2DispatcherCallable.call(ContainerMapper.java:354) 
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195) 
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849) 
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746) 
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045) 
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228) 
    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:636) 
Caused by: java.lang.NullPointerException 
    at tier_controller.MessageSender.postMessages(MessageSender.java:31) 
    at tier_controller.WorldBean.init(WorldBean.java:28) 
    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:616) 
    at com.sun.ejb.containers.interceptors.BeanCallbackInterceptor.intercept(InterceptorManager.java:1009) 
    at com.sun.ejb.containers.interceptors.CallbackChainImpl.invokeNext(CallbackChainImpl.java:65) 
    at com.sun.ejb.containers.interceptors.CallbackInvocationContext.proceed(CallbackInvocationContext.java:113) 
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCallback(SystemInterceptorProxy.java:138) 
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.init(SystemInterceptorProxy.java:120) 
    at sun.reflect.GeneratedMethodAccessor106.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:616) 
    at com.sun.ejb.containers.interceptors.CallbackInterceptor.intercept(InterceptorManager.java:964) 
    at com.sun.ejb.containers.interceptors.CallbackChainImpl.invokeNext(CallbackChainImpl.java:65) 
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:393) 
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:376) 
    at com.sun.ejb.containers.AbstractSingletonContainer.createSingletonEJB(AbstractSingletonContainer.java:538) 
    ... 40 more<code> 

我現在有了這個插手了很多時間和我檢查每TIPP我發現兩次。所需的資源正在運行,並且@Resource語法也應該有效。

我完全喪失了應該如何跟蹤或修復這個錯誤。

+0

你的connectionFactory爲null,它沒有被你的容器初始化。我會從那裏開始...... –

+0

嗯,當然是空的......問題是爲什麼。命名空間是正確的,資源是連接的,我遵循書中的每一步。我在研究過程中遇到了很多類似的問題,但沒有一個解釋爲什麼它可能爲空的答案。 – Sticks

+0

您的代碼是正確的。解釋如何在GlassFish管理控制檯中設置JMS和ConnectionFactory – perissf

回答

0

它要調試它以找出問題所在 - 您可以在jpda選項中設置掛起標誌,以便您可以在應用程序開始初始化之前連接到調試
另外,如果需要的話,您可以在IDE中設置NPE的斷點例外(Run-> View breakpoints-> .. in the Intellij Idea)。

+0

我最終解決了這個問題。問題在於,該servlet沒有loadOnStartup = 1註釋。我仍然不相信,爲什麼這實際上是一個問題,但它解決了這個問題。 – Sticks