简体   繁体   English

远程JMS连接仍使用localhost

[英]Remote JMS connection still using localhost

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. 我在远程glassfish服务器上创建了一个JMS连接工厂,并希望从我本地计算机上的Java客户端应用程序中使用该服务器。 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. 这似乎工作,当我从glassfish服务器删除ConnectionFactory时,我得到一个异常,表明无法按预期找到jms / MyConnectionFactory。

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). 但是,当我随后使用我的topicConnection获取主题时,它尝试连接到localhost:7676(由于我没有在本地运行glassfish,因此失败)。

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. 并且glassfish服务器没有在本地运行我得到了相同的连接拒绝但是,如果我启动我的本地glassfish服务器,主题是在本地创建的,我可以在glassfish管理控制台中看到它们。

In case you ask I do not have jms/MyConnectionFactory on my local glassfish instance, it is only available on the remote server. 如果你问我在我的本地glassfish实例上没有jms / MyConnectionFactory,它只能在远程服务器上使用。

I can't see what I am doing wrong here and why it is trying to use localhost at all. 我无法看到我在这里做错了什么以及它为什么要尝试使用localhost。

Any ideas? 有任何想法吗?

Cheers, 干杯,

James 詹姆士

I have run across a similar problem while trying to do a remote JNDI lookup for EJBs. 我在尝试为EJB进行远程JNDI查找时遇到了类似的问题。 The issue was that in my /etc/hosts file the machine name was mapped to local host something like: 问题是在我的/ etc / hosts文件中,机器名被映射到本地主机,例如:

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. 第二个使用此IP进行查找。 In your case the first connect is returning localhost (127.0.0.1). 在您的情况下,第一个连接是返回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. 您的应用程序尝试连接到localhost进行查找,但不能,因为没有本地运行的glassfish。 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 . 你可以在这里找到更多。

You have to distinguish between obtaining the ConnectionFactory object and the connections that it opens. 您必须区分获取ConnectionFactory对象及其打开的连接。 Obtaining ConnectionFactory object is handled by JNDI (backed by CORBA in this case). 获取ConnectionFactory对象由JNDI处理(在本例中由CORBA支持)。 Once ConnectionFactory object is obtained, it uses its imqAddressList parameter to open the connection to a JMS broker(s). 获得ConnectionFactory对象后,它将使用其imqAddressList参数打开与JMS代理的连接。 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. 默认情况下,它配置为localhost:7676,如果要打开与远程代理的连接,则必须配置正确的值。

You have to check how is this parameter configured for that TopicConnectionFactory bound to jms/MyConnectionFactory. 您必须检查如何为绑定到jms / MyConnectionFactory的TopicConnectionFactory配置此参数。 See Administering JMS Connection Factories and Destinations for more details about how to configure the connection factories within Glassfish. 有关如何在Glassfish中配置连接工厂的更多详细信息,请参阅管理JMS连接工厂和目标

I had a similar issue with obtaining JMS resources from a different machine then the machine running GlassFish (v3.0.1 in my case). 我有一个类似的问题,从另一台机器获取JMS资源,然后运行GlassFish的机器(在我的情况下为v3.0.1)。

In order to get it to work I did the following: 为了让它工作,我做了以下事情:

  • Ensured that the JMS host was set to the hostname of the server machine (default is localhost). 确保将JMS主机设置为服务器计算机的主机名(默认为localhost)。 [Configuration->Java Message Service->JMS Hosts] [配置 - > Java消息服务 - > JMS主机]
  • As Matej suggested, make sure the imqAddressList property uses the same hostname. 正如Matej建议的那样,确保imqAddressList属性使用相同的主机名。 [Resources->Connectors->Connector Connection Pool->yourJmsConnectionFactory] [Resources-> Connectors-> Connector Connection Pool-> yourJmsConnectionFactory]
  • Rather then configuring the properties in a Properties object and supply that object to the InitialContext, I set them as properties of the JVM. 而不是在Properties对象中配置属性并将该对象提供给InitialContext,我将它们设置为JVM的属性。 Such properties can be supplied when starting your application ( java -Dorg.omg.CORBA.ORBInitialHost=myServerHost ...) or through code ( System.setProperty("org.omg.CORBA.ORBInitialHost","myServerHost"); ) 启动应用程序时可以提供这些属性( java -Dorg.omg.CORBA.ORBInitialHost=myServerHost ...)或通过代码( System.setProperty("org.omg.CORBA.ORBInitialHost","myServerHost");

The last measure for me was needed because while loading the JMS resources, some internal class (com.sun.enterprise.naming.factory.AdministeredObjectFactory and maybe others) created a new InitialContext without taking the properties I supplied into account. 我需要的最后一个措施是因为在加载JMS资源时,一些内部类(com.sun.enterprise.naming.factory.AdministeredObjectFactory和其他人)创建了一个新的InitialContext而没有考虑我提供的属性。 That newly created InitialContext was still referring to localhost rather then myServerHost. 新创建的InitialContext仍然是指localhost而不是myServerHost。

Setting the properties as JVM properties works because InitialContext by default takes into account the relevant JVM properties set. 将属性设置为JVM属性是有效的,因为默认情况下InitialContext会考虑相关的JVM属性集。 Any (internal) component that creates a new InitialContext, will automatically use the correct settings. 创建新InitialContext的任何(内部)组件将自动使用正确的设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM