Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ActiveMQ via JNDI

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


I have
  1. Queue named 'example.A'.

  2. 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

  3. 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");
    

but it cannot find my queue, it throws exception: javax.naming.NameNotFoundException: MyQueue

Where are my misstakes?

like image 201
user56228 Avatar asked Jan 17 '09 16:01

user56228


People also ask

What is the use of JNDI in JMS?

The Java™ Naming and Directory Interface (JNDI) API enables JMS clients to look up configured JMS objects. By delegating all the provider-specific work to administrative tasks for creating and configuring these objects, the clients can be completely portable between environments.

Which is the connection factory of queue accessed by JNDI?

The JNDI name of topic connection factory. You can use this option in inbound or outbound scenarios. The JNDI name of queue connection factory. You can use this option in inbound or outbound scenarios.


2 Answers

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.

Your code should be something like:

Context context = new InitialContext();
ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("ConnectionFactory");
Queue queue = (Queue) context.lookup("MyQueue");
like image 120
BenM Avatar answered Oct 23 '22 02:10

BenM


You can set static properties as well as retrieve them from a file as such:

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

This works as long as you set the static props after you load the resource. Helpful if you're loading the provider url from somewhere else for instance.

like image 43
jpredham Avatar answered Oct 23 '22 03:10

jpredham