1

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

}

user3807936
  • 911
  • 8
  • 10

2 Answers2

0

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.

0

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();
Konstantin
  • 3,626
  • 2
  • 33
  • 45