ActiveMQ:队列查找问题

时间:2009-01-12 14:51:18

标签: java jms activemq

我已按照documentation中所述在activemq.xml(ActiveMQ版本5.2.0)中配置队列。

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

我正尝试使用以下代码从java(在同一主机上)访问它:

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);

我收到了以下错误,虽然队列在jconsole中可见(Tree / org.apache.activemq / Queue):

javax.naming.NameNotFoundException: DUMMY

请告诉我我做错了什么。很多,非常感谢!

2 个答案:

答案 0 :(得分:8)

首先,你不必explicitly create any queues in the broker,但它没有任何伤害。

此外,代理中可用的目标不会使用某种JNDI名称自动神奇地映射到JNDI上下文。

您可以执行此操作explicitly as described here。如果您想要自动神奇的JNDI填充,那么使用 dynamicQueues / DUMMY 的JNDI命名约定作为您查找的JNDI名称(如Dynamically creating destinations中所述)

答案 1 :(得分:0)

嗯......好吧,当我想听一个队列时,我通常会这样做。 (从javax.jms导入)

队列队列;

    // 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();

确保您的Handler实现了MessageListener。