Java 通过JNDI使用ActiveMQ
我正在尝试使用JNDI创建与ActiveMQ的简单连接Java 通过JNDI使用ActiveMQ,java,jndi,activemq,Java,Jndi,Activemq,我正在尝试使用JNDI创建与ActiveMQ的简单连接 我有 名为“example.A”的队列 根据,如果我想通过JNDI使用连接工厂和队列(主题),我必须将JNDI.properties文件放在我的类路径上。据我所知,activeMQ类路径默认为%activeMQ%/conf目录。我没有改变它。 因此,我的队列具有以下属性: queue.MyQueue=example.A 我已经为ActiveMQ创建了java客户端类,它使用JNDI,如下所示: Properties jndiPara
我有
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");
我的错误在哪里?问题是您正在显式创建属性并将它们传递到InitialContext构造函数中。这意味着不会读取类路径上的jndi.properties 您的代码应该类似于:
Context context = new InitialContext();
ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("ConnectionFactory");
Queue queue = (Queue) context.lookup("MyQueue");
您可以设置静态属性,也可以从以下文件中检索静态属性:
InputStream is = getClass().getResourceAsStream("/my.jndi.properties");
Properties jndiParameters = new Properties();
jndiParameters.load(is);
jndiParameters.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
jndiParameters.put(Context.PROVIDER_URL, "tcp://localhost:61616");
Context ctx = new InitialContext(jndiParameters);
...
只要在加载资源后设置静态道具,这种方法就可以工作。例如,如果您从其他地方加载提供程序url,这将非常有用。我有一个类似的问题,我可以配置jndi url和连接工厂,但无法提供其他属性。这样看来,队列端点似乎没有定义。如果我使用dynamicQueue/Test,它可以工作,但是是否也可以寻址静态命名的目的地?