两个服务器都有尊严(@Inject导致空引用)

时间:2014-02-22 16:10:00

标签: java linux dependency-injection glassfish ejb

我对java应用程序服务器场景很陌生,所以我一直试图通过一个简单的应用程序来解决这个问题。

我已经在我的mac上安装了Glassfish 4,并编写了一个简单的应用程序,用于侦听http请求,解析查询参数,并将它们传递给jvm队列以供另一个对象处理。

在我的Mac上,代码按预期工作。我提交请求,事情得到解析和传递,一切都很顺利。如果我将我的war文件部署到我的linux盒子(使用glassfish管理控制台),事情会变得奇怪。我的mac和linux(ubuntu)机器都有glassfish 4,截至昨天都有更新。我使用一个单独的对象(注释为@stateless的类)注入到HTTPServlet对象中。在linux机器上,注入的对象为null。在Mac上,它有一个值。

以下是一些代码:

这是HttpServlet:

import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

@WebServlet(urlPatterns = "/en" )
public class ERoute extends HttpServlet {
    **@Inject
    ENotificationSender sender;**

    @Override
    public void init() throws ServletException {
        super.init();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        ENotification message=new ENotification();

        Enumeration paramNames=request.getParameterNames();
        while(paramNames.hasMoreElements()) {
                    // assemble the message to put on the queue
        }

        **sender.sendMessage(message);**

        response.setStatus(HttpServletResponse.SC_OK);
    }
}

和ENotificationSender:

import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.jms.*;

@Stateless
@JMSDestinationDefinitions(
        {@JMSDestinationDefinition(name="jms.BQueue", interfaceName="javax.jms.Queue")}
)
public class ENotificationSender {
    @Inject
    @JMSConnectionFactory("jms/BSample")
    private JMSContext context;

    @Resource(mappedName = "jms/BQueue")
    private Queue theQueue;

    public void sendMessage(ENotification message) {
        context.createProducer().send(theQueue, message);
    }
}

上面的行 sender.sendMessage(message); 导致空指针异常,但只在linux机器上。

这是错误:

[2014-02-22T10:50:33.214-0500] [glassfish 4.0] [WARNING] [] [javax.enterprise.web] [tid: _ThreadID=93 _ThreadName=http-listener-1(2)] [timeMillis: 1393084233214] [levelValue: 900] [[
  StandardWrapperValve[ERoute]: Servlet.service() for servlet ERoute threw exception
java.lang.NullPointerException
    at ERoute.doGet(ERoute.java:46)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    at RouteFilter.doFilter(RouteFilter.java:25)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
    at java.lang.Thread.run(Thread.java:744)
]]

我假设我对这些事情如何协同工作的了解还不够(我会证明这是真的),但我不确定为什么它可以在mac上运行而不是在linux机器上运行。两者都运行java 1.7,glassfish 4.0。

感谢。

0 个答案:

没有答案