I actually use JBoss 4 and JNDI, not hard to use.
First of all, you need to know where JNDI works.
In my JBoss (conf \ jboss-service.xml) I have:
<mbean code="org.jboss.naming.NamingService" name="jboss:service=Naming" xmbean-dd="resource:xmdesc/NamingService-xmbean.xml"> ... <attribute name="Port">7099</attribute> ... </mbean>
This is important, this is the port you want to connect to.
Now you can easily connect to JNDI with this code:
Hashtable<String, String> contextProperties = new Hashtable<String, String>(); contextProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); contextProperties.put(Context.PROVIDER_URL, "jnp://localhost:7099"); InitialContext initContext = new InitialContext(contextProperties);
Now that you have the context, it is very similar to @Nick Holt, with the exception of the factory connection, you should use:
QueueConnectionFactory connFactory = (QueueConnectionFactory) initContext.lookup("ConnectionFactory");
Also, you do not need to create a queue if several
Queue queue = (Queue) initContext.lookup("queueName");
All the above codes were tested with JBoss 4.2.2 GA and JBossMQ (if I was right, JBossMQ replaced JBoss with 4.2.3 messages).
source share