Question

I am writing a small utility to send JMS messages to a remote server, but I am failing to configure correctly the InitialContext (or so it seems)

code to init the Context:

      Properties p = new Properties();
      p.put(Context.INITIAL_CONTEXT_FACTORY, 
            "org.jnp.interfaces.NamingContextFactory");
      p.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
      p.put(Context.PROVIDER_URL, "jnp://10.10.10.10:1099/");
      Context context = new InitialContext(p);

But when I run it I get an exception:

javax.naming.CommunicationException 
[Root exception is java.rmi.ConnectException: 
Connection refused to host: 127.0.0.1; 
nested exception is: 
java.net.ConnectException: Connection refused: connect]

So what is baffling me is that is complaining about 127.0.0.1 event though I am configuring it for 10.10.10.10, which is alive, running jboss, no firewall, I can get a telnet session to port 1099, so it seems to be ok

Any pointers ? or helpers ?

Was it helpful?

Solution

This is because JNDI, and dependent protocols, are connect-back in nature, and subsequent requests will go to the IP that the server 'believes' it should be listening on. If you telnet to 10.10.10.10:1099, and look at the output, you'll see something like this:

telnet 10.10.10.10 1099
[Connection message]
[Garbage]
127.0.0.1....

The reason is that your server is 'serving' on 127.0.0.1, and will reply with that address as the JNDI location. Because you are (most likely) on a remote machine, when your remote machine tries to connect to the IP that the Jboss server informed it to connect to (127.0.0.1), it tries to connect to itself, not to the JBoss server.

You need to either start jboss with: ./run.sh -b 0.0.0.0 (all IP bind), or change your localhost entry on the machine to 10.10.10.10

(Incidentally, I remember struggling with this one for ages, and know how frustrating it is)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top