Reputation: 71
I'm trying to create simply connect with ActiveMQ using JNDI.
I have:
example.A
.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
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 mistakes?
Upvotes: 6
Views: 23772
Reputation: 2199
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.
Upvotes: 1
Reputation: 4153
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");
Upvotes: 7