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 injection works only in a managed environment, such as a Java EE application server, or a Spring container, for instance. In a standalone application JNDI is your only choice.
Annotations in general are meant to be processed by some tool/framework, and plain JVM that executes your main()
method simply does not contain such. The only annotations I know of that are processed by JVM out of the box are compile-time @Deprecated
, @Override
and @SuppressWarnings
.
Replying to your comment: I don't have access to the book, so I'll only guess that they probably describe running an application client component and not standalone application client. It's not the same — check Glassfish EJB FAQ. ACCs are normally deployed into an application server and can be executed via Java Web Start or without it, but in an AS-specific way. See Glassfish example (you didn't say what AS your EJB executes in).