JMSContext NullPointer Exception - wildfly 8.2.0 d

2019-09-06 09:47发布

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);
}

}

标签: jms wildfly-8
2条回答
老娘就宠你
2楼-- · 2019-09-06 10:04

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();
查看更多
3楼-- · 2019-09-06 10:26

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.

查看更多
登录 后发表回答