views:

438

answers:

2

I have a created a JMS Connection Factory on a remote glassfish server and want to use that server from a java client app on my local machine. I have the following configuration to get the context and connection factory:

Properties env = new Properties();
env.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
env.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
env.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
env.setProperty("org.omg.CORBA.ORBInitialHost", JMS_SERVER_NAME);
env.setProperty("org.omg.CORBA.ORBInitialPort", "3700");

initialContext = new InitialContext(env);
TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) initialContext.lookup("jms/MyConnectionFactory");
topicConnection =  topicConnectionFactory.createTopicConnection();

topicConnection.start();

This seems to work and when I delete the ConnectionFactory from the glassfish server I get a exception indicating that is can't find jms/MyConnectionFactory as expected.

However when I subsequently use my topicConnection to get a topic it tries to connect to localhost:7676 (this fails as I am not running glassfish locally).

If I dynamically create a topic:

TopicSession pubSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = pubSession.createTopic(topicName);
TopicPublisher publisher = pubSession.createPublisher(topic);
Message mapMessage = pubSession.createTextMessage(message);
publisher.publish(mapMessage);

and the glassfish server is not running locally I get the same connection refused however, if I start my local glassfish server the topics are created locally and I can see them in the glassfish admin console.

In case you ask I do not have jms/MyConnectionFactory on my local glassfish instance, it is only available on the remote server.

I can't see what I am doing wrong here and why it is trying to use localhost at all.

Any ideas?

Cheers,

James

A: 

You have to distinguish between obtaining the ConnectionFactory object and the connections that it opens. Obtaining ConnectionFactory object is handled by JNDI (backed by CORBA in this case). Once ConnectionFactory object is obtained, it uses its imqAddressList parameter to open the connection to a JMS broker(s). By default, it is configured to localhost:7676 and you have to configure a proper value if you want to open connections to remote brokers.

You have to check how is this parameter configured for that TopicConnectionFactory bound to jms/MyConnectionFactory. See Administering JMS Connection Factories and Destinations for more details about how to configure the connection factories within Glassfish.

Matej
Thanks for the help. I added AddressList on my ConnectionPoolFactory now I am getting the following error: org.omg.CORBA.COMM_FAILURE: vmcid: SUN minor code: 201 completed: No Any ideas?
James
More info, I can now see the dynamic topics on the remote glassfish server's admin pages but only if I am running a local instance of glassfish. Without the local version running I get the exception from my previous comment.
James
A: 

I have run across a similar problem while trying to do a remote JNDI lookup for EJBs. The issue was that in my /etc/hosts file the machine name was mapped to local host something like:

127.0.0.1 glassfish localhost.localdomain localhost

To look up a resource, two connections are made. The first is to find the location of the server you should look for. The second uses this IP to do the lookup. In your case the first connect is returning localhost (127.0.0.1). Your application tries to connect to localhost to do the lookup and cannot because there is no glassfish running locally. To fix this make sure your host file looks something like:

127.0.0.1 localhost

192.168.1.10 glassfish (or what ever your server name is).

You can find out more here

Robert.

Robert