AdminObjectFinder.java and jndi.properties example for ActiveMQ Artemis

Configuring JNDI for ActiveMQ Artemis requires and AdminObjectFinder.java and a jndi.properties file. The jndi.properties file specifies the attributes to create a connection factory.

Use the following code to create an AdminObjectFinder.java file for ActiveMQ Artemis:

package jmsfromABL;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.TopicConnectionFactory;
import javax.jms.QueueConnectionFactory;
import javax.jms.Topic;
import javax.jms.Queue;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
import java.util.Hashtable;
import java.io.File;
 
public class AdminObjectFinder{
 
    public Context context = null;
    public AdminObjectFinder() throws Exception{
        context = new InitialContext();
    }
 
    public TopicConnectionFactory getTopicConnectionFactory(String name)
    throws Exception {
        TopicConnectionFactory factory = null;
        factory = (javax.jms.TopicConnectionFactory)context.lookup(name);
        return factory;
    }
 
    public QueueConnectionFactory getQueueConnectionFactory(String name)
    throws Exception {
        QueueConnectionFactory factory = null;
        factory = (javax.jms.QueueConnectionFactory)context.lookup(name);
        return factory;
    }
 
    public ConnectionFactory getConnectionFactory(String name)
    throws NamingException {
        return (ConnectionFactory) context.lookup(name);
    }
 
    public Topic getTopic(String name) throws Exception {
        Topic topic = null;
        Object object = null;
        object = context.lookup(name);
        if (object != null) {
            topic = (javax.jms.Topic) object;
        }
        return topic;
    }
 
    public Queue getQueue(String name) throws Exception {
        Queue queue = null;
        Object object = null;
        object = context.lookup(name);
        if (object != null) {
            queue = (javax.jms.Queue) object;
        }
        return queue;
    }
 
    public Destination getDestination(String name)
    throws NamingException {
        return (Destination) context.lookup(name);
    }
}

Use the following code to create a jndi.properties file for ActiveMQ Artemis:

# START SNIPPET: jndi

java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory

# use the following property to configure the default connector
java.naming.provider.url=failover:(tcp://localhost:61617,tcp://localhost:61618)?initialReconnectDelay=100
#java.naming.provider.url=tcp://localhost:61617

# use the following property to specify the JNDI name the connection factory
# should appear as. 
connectionFactoryNames = ConnectionFactory

# register some queues in JNDI using the form
# queue.[jndiName] = [physicalName]
queue.MyQueue = example.MyQueue


# register some topics in JNDI using the form
# topic.[jndiName] = [physicalName]
topic.MyTopic = example.MyTopic

# END SNIPPET: jndi
Note: Replace the hostname and port in the above example code with your failover broker's location.