Aitor
Aitor

Reputation: 1

how to read a jms queue from a non hosted java application?

I'm trying to read messages from a jms queue created in "Sun App Server" from a non-hosted application (console app) but I get the following error:

NoInitialContextException
Cannot instantiate class: javax.jms.TopicConnectionFactory

with this code:

Properties env = new Properties( );
env.put(Context.INITIAL_CONTEXT_FACTORY, "javax.jms.TopicConnectionFactory");
InitialContext jndi = new InitialContext(env);

and I have referenced the j2ee.jar library that contains the class but certainly, the class is an interface.

Can I access the queue from a non-hosted application??

Upvotes: 0

Views: 838

Answers (1)

Nicholas
Nicholas

Reputation: 16056

Aitor;

When you say "Sun App Server", I'm not sure what that means, but I will assume it is Glassfish.

There are 2 separate steps to acquiring remote JMS resources.

  1. You need to create a remote JNDI connection which requires a valid InitialContextFactory class name.
  2. Once you have a the connection, you can look up the TopicConnectionFactory.

For item #1, this link demonstrates how to make a remote JNDI connection. For item #2, once you have a JNDI context, you will also need to know the JNDI name of the TopicConnectionFactory which will look something like:

TopicConnectionFactory tcf = (TopicConnectionFactory) jndi.lookup("jms/TopicConnectionFactory");

One aspect you need to keep in mind is that the j2ee.jar library contains the generic Java EE interfaces for the JMS classes, but you will also need a library in your classpath that contain the JMS implementation concrete classes. This also goes for the JNDI connection. This tutorial provides a concise list as:

Applicationserver JNDI Lookup

  • /lib/appserv-rt.jar
  • /lib/appserv-admin.jar
  • /lib/javaee.jar /lib/j2ee.jar

Client Lib

  • /imq/lib/jms.jar
  • /imq/lib/imq.jar
  • /imq/lib/imqutil.jar
  • /lib/install/applications/jmsra/jmsra.jar

Upvotes: 1

Related Questions