I am trying to write a app that will send messages to a message queue. RIght now, I am sending the messages through a servlet. It works, but I have no idea how the @Resource injection works.
My code is
I created the jms/testQueue and jms/connectionFactory in my Glassfish Admin console. The code works fine. I am trying to understand how it works.
I understand that the @Resource annotation is injecting the code that will look up and object of the name jms/connectionFactory in the JNDI. But, I don;t understand how it gets the JNDI context? If I want to write a standalone app that does the same thing, where do I put the reference to the JNDI context? Do I have to put it into a property file?
I am probably asking a stupid question, and am missing something that should be very obvious
I don't think it is practical to answer the questions you pose in a forum post. I would suggest looking up a decent reference for EJB 3/Java EE 5.
In short, the container looks into the application server's default JNDI context for DI. You can change that by specifying a different jndi.properties file in the classpath. As to standalone applications, you can still use DI via an application client container or an embeddded EJB 3 container. Otherwise, you can get a handle to a JNDI context using the JNDI API.
Hope this helps,
Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1