I can't get javax.jms.ConnectionFactory
injected into my standalone JMS client.
I get a java.lang.NullPointerException
at connectionFactory.createConnection()
in the code below.
JmsClient.java
public class JmsClient {
@Resource(mappedName="jms/QueueConnectionFactory")
private static ConnectionFactory connectionFactory;
@Resource(mappedName="jms/ShippingRequestQueue")
private static Destination destination;
public static void main(String[] args) {
try {
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(destination);
ObjectMessage message = session.createObjectMessage();
ShippingRequestQueue shippingRequest = new ShippingRequestQueue(1, "107, Old Street");
message.setObject(shippingRequest);
producer.send(message);
session.close();
connection.close();
System.out.println("Shipping request message sent ..");
} catch (Throwable ex) {
ex.printStackTrace();
}
}
}
I have created the corresponding Connection Factory and Destination Resource at Open MQ (MoM) using Glassfish 3.1 Admin Console.
Could someone help me understand what am I missing?
@Resource
doesn't work but looking up using JNDI name does.Context jndiContext = new InitialContext(); ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext.lookup("jms/QueueConnectionFactory"); Queue destination = (Queue) jndiContext.lookup("jms/ShippingRequestQueue");
– Commensurate