How to specify the initial context detail for JMS connection factory lookup

14,511

The initial context is a reference to a JNDI namespace where objects like JMS Queues can be looked up. I wrote this tutorial some time ago, which you might find helpful.

For a remote jboss server, there are 3 basic this should be (using the default port):

  • java.naming.factory.initial: org.jnp.interfaces.NamingContextFactory
  • java.naming.factory.url.pkgs: org.jboss.naming:org.jnp.interfaces
  • java.naming.provider.url: <hostname>:1099

The code would look something like this:

import javax.naming.*;
import javax.jms.*;
import java.util.*;
.....
Properties jndiProps = new Properties();
jndiProps.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
jndiProps.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
jndiProps.put("java.naming.provider.url", "localhost:1099");
Context ctx = new InitialContext(jndiProps);
Queue jmsQueue = (Queue)ctx.lookup("jndi-name-of-queue");

If your code is running inside the jboss server, you don't need those properties since they are implicit.

import javax.naming.*;
import javax.jms.*;
.....
Context ctx = new InitialContext(); // no properties needed
Queue jmsQueue = (Queue)ctx.lookup("jndi-name-of-queue");
Share:
14,511

Related videos on Youtube

user2098047
Author by

user2098047

Updated on September 16, 2022

Comments

  • user2098047
    user2098047 over 1 year

    I faced a situation at work where I need to specify the initial context name to an underlying architecture component so that it can help me post message to a JMS Queue.

    How do I specify the exact context factory name? I presume this is probably the string to be used "org.jnp.interfaces.namingcontextfactory" based on google results. I would like to understand what is the authoritative method to arrive at this string taking perhaps the jboss server configuration as the starting point?

    Thanks Cinish

  • user1053510
    user1053510 about 6 years
    Link to your tutorial leads to HTTP 404
  • Nicholas
    Nicholas about 6 years
    Sorry about that. It was quite old and it got unlisted.
  • Khaled Alanezi
    Khaled Alanezi about 5 years
    @Nicholas I'm following this tutorial here. It describes how to communicate between two clients using jms with glassfish server. The tutorial uses iiop://localhost:3700 as the provider url. However, the code is failing to connect with the reason: "Could not obtain connection to any of these urls: iiop://localhost:3700" .... "Failed to connect to server iiop:1099". Any idea what is wrong?
  • Khaled Alanezi
    Khaled Alanezi about 5 years
    Note: when I try use an empty constructor for the context as suggested for local server situation, I'm also receiving an error: javax.naming.NoInitialContextException: "Need to specify class name in environment or system property, or in an application resource file: java.naming.factory.initial"