对于 JBoss,您的代码应如下所示:
Properties properties = new Properties();
properties.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
properties.put("java.naming.factory.url.pkgs","=org.jboss.naming:org.jnp.interfaces");
properties.put("java.naming.provider.url","localhost:1099");
Context context = new InitialContext(properties);
(EchoBeanRemote) c.lookup("EchoBean/remote");
如果您愿意,可以将 JNDI 环境设置放在一个jndi.properties
文件中(该文件需要位于类路径中):
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=jnp://localhost:1099
并使用非参数InitialContext
构造函数:
Context context = new InitialContext();
(EchoBeanRemote) c.lookup("EchoBean/remote");
这显然更便携。
在这两种情况下,您都需要jbossall-client.jar
在客户端的类路径上。
PS:您可以在基于 Web 的 JMX 控制台的JNDI 视图中检查您的 bean 注册的全局 JNDI 名称 (如果它仍然存在)。