消费者应用程序中的JMS ConnectionFactory NullPointerException

时间:2016-12-04 16:35:30

标签: java jms

我有一个作为服务器的应用程序,并定期将问题/答案推送到JMS队列,这部分工作正常。

我想要做的是使用在专用应用程序中发送到此队列的消息,但是当我尝试访问我的ConnectionFactory时,当我尝试调用方法时,我总是得到NullPointerException" CreateContext& #34;

    public class Consumer {

    /**
     * @param args the command line arguments
     */


    @Resource(lookup = "jms/__defaultConnectionFactory")
    private static ConnectionFactory connectionFactory;
    @Resource(lookup = "jms/QuizQueue")
    private static Queue queue;
    private static Map<String, Map<String, Boolean>> map;
    private static Quiz quiz = new Quiz();
    //private static JMSContext ctx;

    public static void main(String[] args) {
        boolean first = false;
        int qid = 0;      
        map = new HashMap();
        try(JMSContext ctx = connectionFactory.createContext();){
        JMSConsumer m = ctx.createConsumer(queue);
        while(true) {
            Message msg = m.receive(100000);
... Do  something with the msg ...

例外:

Exception occurred: java.lang.NullPointerException
java.lang.NullPointerException
    at com.sun.enterprise.resource.pool.PoolManagerImpl.getResourceFromPool(PoolManagerImpl.java:245)
    at com.sun.enterprise.resource.pool.PoolManagerImpl.getResource(PoolManagerImpl.java:170)
    at com.sun.enterprise.connectors.ConnectionManagerImpl.getResource(ConnectionManagerImpl.java:354)
    at com.sun.enterprise.connectors.ConnectionManagerImpl.internalGetConnection(ConnectionManagerImpl.java:307)
    at com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:242)
    at com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:171)
    at com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:166)
    at com.sun.messaging.jms.ra.ConnectionFactoryAdapter._allocateConnection(ConnectionFactoryAdapter.java:204)
    at com.sun.messaging.jms.ra.ConnectionFactoryAdapter.createConnection(ConnectionFactoryAdapter.java:162)
    at com.sun.messaging.jms.ra.ConnectionFactoryAdapter.createConnection(ConnectionFactoryAdapter.java:144)
    at com.sun.messaging.jmq.jmsclient.JMSContextImpl.<init>(JMSContextImpl.java:163)
    at com.sun.messaging.jms.ra.ConnectionFactoryAdapter.createContext(ConnectionFactoryAdapter.java:167)
    at quizconsumer.Consumer.main(Consumer.java:48)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.glassfish.appclient.client.acc.AppClientContainer.launch(AppClientContainer.java:446)
    at org.glassfish.appclient.client.AppClientFacade.main(AppClientFacade.java:166)

我不知道是什么导致了这个异常,看起来查找确实有效,并且&#34; connectionFactory&#34; object不为null,但在调用createContext方法时出错。

1 个答案:

答案 0 :(得分:0)

如果您尝试在容器外运行应用程序,则可注入属性将不起作用。相反,你必须手动JNDI查找ConnectionFactory和Queue。

例如:

PWM chip