tags:

views:

1436

answers:

2

I've set up a queue by configuring it in activemq.xml (ActiveMQ version 5.2.0) as described in the documentation.

<destinations>
    <queue physicalName="FOO.BAR" />
    <queue physicalName="DUMMY" />
</destinations>

I'm trying to access it from java (on the same host) with the following code:

Hashtable properties = new Hashtable();
properties.put(Context.INITIAL_CONTEXT_FACTORY, 
    "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
properties.put(Context.PROVIDER_URL, "tcp://localhost:61616");

context = new InitialContext(properties);

factory = (ConnectionFactory) context.lookup("ConnectionFactory");
connection = factory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
queueName = "DUMMY"; // which can be either FOO.BAR or DUMMY
dest = (Destination) context.lookup(queueName);

I'm receveing the following error, although the queue is visible in jconsole (Tree / org.apache.activemq / Queue):

javax.naming.NameNotFoundException: DUMMY

Please tell me what I'm doing wrong. Many, many thanks!

+3  A: 

Firstly you don't have to explicitly create any queues in the broker though it does no harm.

Also the destinations available in the broker are not auto-magically mapped into a JNDI context for you using some kind of JNDI name.

You can do this explicitly as described here. If you want auto-magical population of JNDI then use the JNDI naming convention of dynamicQueues/DUMMY as the JNDI name you lookup (as described in the Dynamically creating destinations)

James Strachan
Thanks for explaining that there is a difference between creating a topic/queue, and mapping it into a JNDI context. I suspect a queue/topic connection problem we are trying to solve has its roots in the assumption that creation == JNDI registration.
Stephen Harmon
A: 

Hmm.. well when I want to listen to a Queue I usually do something like this. (Imports from javax.jms)

Queue queue;

    // Connect to ActiveMQ
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(messageBrokerURL);
    connection = factory.createConnection();
    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

    // List to Dummy Queue
    queue = session.createQueue("DUMMY");
    messageConsumer = session.createConsumer(queue);
    messageConsumer.setMessageListener(queueHandler);

    // Start the connection
    connection.start();

And make sure that your Handler implements MessageListener.

Bernie Perez
Thank you Bernie. The problem with that is that you have ActiveMQ specific code, the ones I listed above works perfectly fine with other JMS implementations too, i.e. with OpenJms.
MrG