我正在尝试使用JNDI创建与ActiveMQ的简单连接。
我有
I'm trying to create simply connect with ActiveMQ using JNDI. I have
名为'example.A'的队列。
Queue named 'example.A'.
根据触及JNDI的ActiveMQ文档,如果我想通过JNDI使用ConectionFactories和Queues(Topics),我必须在我的类路径上放置jndi.properties文件。据我所知,默认情况下,activeMQ类路径是%activemq%/ conf目录。我没有改变它。 所以我的队列有这个属性:
According ActiveMQ documentation touching JNDI, if I want to use ConectionFactories and Queues (Topics) via JNDI, I have to place jndi.properties file on my classpath. As I have understood, activeMQ classpath is %activemq%/conf directory by default. I have not changed it. So I have this property for my queue:
queue.MyQueue = example.A
queue.MyQueue = example.A
我为ActiveMQ创建了java客户端类,它使用如下的JNDI:
I have created java client class for ActiveMQ which uses JNDI as below:
Properties jndiParameters = new Properties() ; jndiParameters.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory"); jndiParameters.put(Context.PROVIDER_URL, "tcp://localhost:61616"); Context context = new InitialContext(jndiParameters); ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("ConnectionFactory"); Queue queue = (Queue) context.lookup("MyQueue");
但它无法找到我的队列,它会抛出异常:javax.naming.NameNotFoundException:MyQueue
but it cannot find my queue, it throws exception: javax.naming.NameNotFoundException: MyQueue
我的错误在哪里?
推荐答案问题是您是显式创建属性并将它们传递给InitialContext构造函数。这意味着将不会读取类路径上的jndi.properties。
The problem is that you are explicitly creating the properties and passing them into the InitialContext constructor. This means the jndi.properties on the class path won't be read.
您的代码应该类似于:
Context context = new InitialContext(); ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("ConnectionFactory"); Queue queue = (Queue) context.lookup("MyQueue");更多推荐
通过JNDI使用ActiveMQ
发布评论