JNDI without a J2EE Container (with JNP? Maybe some other provider?)

By : Benson
Source: Stackoverflow.com
Question!

I need to run a JNDI provider without the overhead of a J2EE container. I've tried to follow the directions in this article, which describes (on page 3) exactly what I want to do. Unfortunately, these directions fail. I had to add the jboss-common.jar to my classpath too. Once I did that, I get a stack trace:

$ java org.jnp.server.Main
0    [main] DEBUG
org.jboss.naming.Naming  - Creating
NamingServer stub, theServer=null,rmiPort=0,clientSocketFactory=null,[email protected]d093076[bindAddress=null]
Exception in thread "main"
java.lang.NullPointerException
     at org.jnp.server.Main.getNamingInstance(Main.java:301)
     at org.jnp.server.Main.initJnpInvoker(Main.java:354)
     at org.jnp.server.Main.start(Main.java:316)
     at org.jnp.server.Main.main(Main.java:104)

I'm hoping to make this work, but I would also be open to other lightweight standalone JNDI providers. All of this is to make ActiveMQ work, and if somebody can suggest another lightweight JMS provider that works well outside of the vm the clients are in without a full blown app server that would work too.

By : Benson


Answers

Use a jndi.properties file like this:

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

# use the following property to configure the default connector
java.naming.provider.url=tcp://jmshost:61616

# use the following property to specify the JNDI name the connection factory
# should appear as. 
#connectionFactoryNames = connectionFactory, queueConnectionFactory, topicConnectionFactry

# 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 = MY.TOPIC

Make sure that this file is in your classpath. Then you can lookup the topic/queue like this (minus appropriate try/catches):

context = new InitialContext(properties);

context = (Context) context.lookup("java:comp/env/jms");

topicConnectionFactory = (TopicConnectionFactory) context.lookup("ConnectionFactory");

topic = (Topic) context.lookup("myTopic");
By : KC Baltz


Apache ActiveMQ already comes with an integrated lightweight JNDI provider. See these instructions on using it.

Basically you just add the jndi.properties file to the classpath and you're done.

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:61616

# use the following property to specify the JNDI name the connection factory
# should appear as. 
#connectionFactoryNames = connectionFactory, queueConnectionFactory, topicConnectionFactry

# 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


I think OregonGhost's answer is probably the way you want to go. Just to elaborate on it, he's suggesting doing something like this:

public string GetPath(string basefolder, string[] extraFolders)
{
    string version = Versioner.GetBuildAndDotNetVersions();
    string callingModule = StackCrawler.GetCallingModuleName();

    List<string> parameters = new List<string>(extraFolders.Length + 3);
    parameters.Add(basefolder);
    parameters.Add(version);
    parameters.Add(callingModule);
    parameters.AddRange(extraFolders);
    return AppendFolders(parameters.ToArray());
}

And I don't mean that as a lesson on how to use Lists, just as a little clarification for anybody who may come along looking for the solution in the future.



This video can help you solving your question :)
By: admin