2009-01-12 64 views
6

我已经通过在activemq.xml(ActiveMQ版本5.2.0)中配置它,如documentation中所述设置队列。ActiveMQ:问题与队列查找

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

我receveing下面的错误,虽然队列中JConsole的可见( Tree/org.apache.activemq/Queue):

javax.naming.NameNotFoundException: DUMMY 

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

回答

8

首先你不必explicitly create any queues in the broker虽然它没有伤害。

此外,代理中可用的目标不会自动奇迹般地映射到您使用某种JNDI名称的JNDI上下文中。您可以这样做explicitly as described here。如果你想JNDI的自动神奇人口然后使用dynamicQueues JNDI命名惯例/假人为你查找(如Dynamically creating destinations描述)的JNDI名称

+0

感谢您解释创建主题/队列并将其映射到JNDI上下文之间存在差异。我怀疑我们试图解决的队列/主题连接问题的根源在于创建== JNDI注册的假设。 – 2009-11-17 21:11:45

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

并确保您的处理程序实现MessageListener。

+0

谢谢伯尼。问题在于你具有ActiveMQ特定的代码,我上面列出的代码与其他JMS实现(即OpenJms)完美兼容。 – MrG 2009-01-13 08:43:48