How to use the PropertiesFileInitialContextFactory
This ContextFactory uses a java properties formatted file to setup initial values.
JNDI Property setup
By setting the JNDI Initial Context Factory and URL as below it is possible to load any File from the locally mounted file system to use for JNDI purposes. The format of the file is described in the next section.
By simply setting these two system properties you can jump straight to the InitialContext creation in your code.
Example properties file
This is the example properties file.
The property file allows a number of queues to be defined that can then be discovered via JNDI. There are four properties used by the PFICFactory.
connectionfactory.<jndiname> this is the Connection URL that the connection factory will use to perform connections.
queue.<jndiname> this defines a jms queue or in amqp a amq.direct exchange
topic.<jndiname> this defines a jms topic or in amqp a amq.topic exchange
destination.<jndiname> this takes a Binding URL and so can be used for defining all amq destinations, queues, topics and header matching.
In all of these properties the <jndiname> is the string value that would be given when performing a lookup.
NOTE: This does not create the queue on the broker. You should ensure that you have created the queue before publishing to it. Queues can be declared in the virtualhosts.xml file so that they are created on broker startup, or created dynamically by consuming clients. Topics and other destinations that use temporary queues cannot be created in this way, so a consumer must be created first before publishing messages with mandatory routing.
Example lookup code
The bindingValue is the String that would be placed in <jndiname> above.
final String INITIAL_CONTEXT_FACTORY = "org.apache.qpid.jndi.PropertiesFileInitialContextFactory";
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
System.setProperty(Context.PROVIDER_URL, _JNDIFile);
Context ctx = new InitialContext();
object = ctx.lookup(bindingValue);
ctx.close();
final String INITIAL_CONTEXT_FACTORY = "org.apache.qpid.jndi.PropertiesFileInitialContextFactory";
final String CONNECTION_JNDI_NAME = "local";
final String CONNECTION_NAME = "amqp:;
final String QUEUE_JNDI_NAME = "queue";
final String QUEUE_NAME = "example.MyQueue";
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
properties.put("connectionfactory."+CONNECTION_JNDI_NAME , CONNECTION_NAME);
properties.put("queue."+QUEUE_JNDI_NAME , QUEUE_NAME);
Context ctx = new InitialContext(properties);
ConnectionFactory factory = (ConnectionFactory)ctx.lookup(CONNECTION_JNDI_NAME);
Queue queue = (Queue)ctx.lookup(QUEUE_JNDI_NAME);
ctx.close();
Using Qpid with other JNDI Providers