JMSContext NullPointer Exception - wildfly 8.2.0 default queue

此生再无相见时 提交于 2019-12-25 04:53:23

问题


I am trying to send messages to my default queue in wildfly and when i invoke "sendMessage()" the JMSContext is giving me a null pointer exception. what can i fix ?

public class SendMsg {


@Resource(lookup = "java:/ConnectionFactory")
 ConnectionFactory connectionFactory;


JMSContext context=connectionFactory.createContext();

@Resource(mappedName="java:/jms/queue/test")
Queue queue;

public void sendMessage(String message) {
    System.out.println("fancy beans");
    context.createProducer().send(queue, message);
}

}


回答1:


I just stumbled over this same error (in my case, I was trying to inject a JMSContext into a Servlet).

It was a missing beans.xml file. In my case, it belonged in the WEB-INF folder of the war file. Apparently the Weld subsystem is only started when it is present: https://docs.jboss.org/author/display/WFLY8/Developer+Guide#DeveloperGuide-Whicharetheimplicitmoduledependencies%3F

Although I suspect that it's started in other circumstances as well - I have an EJB application which doesnt need any bean.xml for CDI to work.




回答2:


You cant call the connection factory that "early". It will not be injected during the construction of your bean. Inject a JMSContext directly is probably easier.

public class SendMsg {


//@Resource(lookup = "java:/ConnectionFactory")
//ConnectionFactory connectionFactory;

@Inject
JMSContext context;   //=connectionFactory.createContext();


来源:https://stackoverflow.com/questions/27942867/jmscontext-nullpointer-exception-wildfly-8-2-0-default-queue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!