问题
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