javajmsjndiactivemq-artemis

How to add JNDI destinations (Topics and Queues) with ActiveMQ?


I don't want to use jdni.properties file, so to add new properties to my JNDI settings, I wrote following:

    Hashtable<String, Object> jndi_env = new Hashtable<String, Object>();
    jndi_env.put(InitialContext.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
    jndi_env.put("connectionFactory.ConnectionFactory","vm://0");
    jndi_env.put("topic.example","example");

My Problem is, when I call this class:

initialContext = new InitialContext(jndi_env);

Since I pass a name parameter in the last line a URL context factory is looked up.

This makes my code looking for a tcp://localhost:61616 connection which I actually don't want.

I see that there are

QueueConnectionFactory: org.apache.activemq.ActiveMQConnectionFactory
example: org.apache.activemq.command.ActiveMQTopic
XAConnectionFactory: org.apache.activemq.ActiveMQXAConnectionFactory

which I don't want, or at least not the type they are.

If I check without passing an argument using my jndi.properties file where I don't get the issue of establishing a tcp connection, then I find just:

ConnectionFactory: org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory
queue: org.apache.activemq.artemis.jndi.ReadOnlyContext
queue/exampleQueue: org.apache.activemq.artemis.jms.client.ActiveMQQueue
dynamicTopics: org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory$2
dynamicQueues: org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory$1

So how can I change the object types of my added jndi_env.put("topic.example","example"); so it will be like this (but of course for Topics)

queue: org.apache.activemq.artemis.jndi.ReadOnlyContext
queue/exampleQueue: org.apache.activemq.artemis.jms.client.ActiveMQQueue

Solution

  • When you create your InitialContext you're passing in the wrong factory. Currently you're passing in org.apache.activemq.jndi.ActiveMQInitialContextFactory. This is the factory for ActiveMQ Classic, not ActiveMQ Artemis. You need to pass in org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory instead, e.g.:

    Hashtable<String, Object> jndi_env = new Hashtable<String, Object>();
    jndi_env.put(InitialContext.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory");
    jndi_env.put("connectionFactory.ConnectionFactory","vm://0");
    jndi_env.put("topic.example","example");