Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveMQInitialContextFactory vs. NamingContextFactory

I'm creating a java enterprise application which uses activemq to send messages to a standalone queue broker.

I currently maintain access to long lived resources through jndi lookup which works quite nicely. I'd like to continue this pattern with the ActiveMQ connection factories and queue connection factories, however in the amq documentation it specifies that my jndi.properties should have:

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

while the default jndi.properties (which works with my simple object and facade lookups has:

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory

Can I use one with the other? Can I have two jndi.properties files or two initial contexts somehow?

This question shows how to configure ONLY activemq through jndi. I want them to play nicely together.

like image 500
jpredham Avatar asked Feb 14 '26 21:02

jpredham


1 Answers

You can create any number of InitialContext objects you want. You just have to pass environment to its constructor to properly initialize it.

So you can still safely use jndi.properties and initialize initial context for activemq with the code which may look like this:

public void setUpActiveMQResources() throws IOException, NamingException {
    InitialContext context = createInitialContext("/activemq.jndi.properties");
    // do what you want
}

public InitialContext createInitialContext(String resource) throws IOException, NamingException {
    InputStream is = getClass().getResourceAsStream(resource);
    Properties props = new Properties();
    try {
        props.load(is);
    } finally {
        is.close();
    }
    return new InitialContext(props);
}

activemq.jndi.properties in that case is the classpath resource with content like here

like image 99
szhem Avatar answered Feb 17 '26 12:02

szhem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!