通过JNDI使用ActiveMQ [英] Using ActiveMQ via JNDI

查看:284
本文介绍了通过JNDI使用ActiveMQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JNDI创建与ActiveMQ的简单连接。



我有

I'm trying to create simply connect with ActiveMQ using JNDI.


I have


  1. 名为'example.A'的队列。

  1. 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆