本人刚接触有关rmi,jndi的知识,在学习的工程中遇到了一些疑问。
在测试rmi的时候我用两种方法都能得到远程方法的调用,方法一:
服务器端:
String serverName = "//localhost:8089/Remote_Hello";
int port = 8089;
Registry registry = LocateRegistry.createRegistry(port) ;
//生成远程对象
RemoteHello hello = new RemoteHello();
//绑定对象
Naming.rebind(serverName,hello);
System.out.println("服务启动1,正等待客户端调用...");
客户端:
String remoteName = "//localhost:8089/Remote_Hello";
Registry registry = LocateRegistry.getRegistry(8089) ;
//获得远程对象
IRemoteHello hello = (IRemoteHello)Naming.lookup(remoteName);
System.out.println(hello.getDate());
方法二:
服务器端:
String serverName = "Remote_Hello";
int port = 8089;
Registry registry = LocateRegistry.createRegistry(port) ;
//生成远程对象
RemoteHello hello = new RemoteHello();
registry.bind(serverName,hello);
System.out.println("服务启动21,正等待客户端调用...");
客户端:
String remoteName = "Remote_Hello";
Registry registry = LocateRegistry.getRegistry(8089) ;
//获得远程对象
IRemoteHello hello = (IRemoteHello)registry.lookup(remoteName);
//获得远程调用结果
System.out.println("二1:"+hello.getDate());疑问:为什么在服务器端既可以使用Naming来绑定对象也可以用registry 来绑定对象? 他们之间的区别和关联在什么地方?还有就是我在学习的时候总觉得rmi和jndi视乎是混杂在一起的,不能很好的将他们界定开来。jndi可以将设备或者对象用一个名字来命名,然后我们可以使用这个名字来找到相应的设备或者对象,那么这个时候我们不就可以操作对象了吗? 然后在这个rmi中的一些操作感觉就像和jndi一样似的。就像上面对象的注册或者绑定,以及客户端使用的lookup方法,似乎也是一种关联和查找的操作。 希望大家能帮帮小弟解决下我心中的困惑。谢谢!!
在测试rmi的时候我用两种方法都能得到远程方法的调用,方法一:
服务器端:
String serverName = "//localhost:8089/Remote_Hello";
int port = 8089;
Registry registry = LocateRegistry.createRegistry(port) ;
//生成远程对象
RemoteHello hello = new RemoteHello();
//绑定对象
Naming.rebind(serverName,hello);
System.out.println("服务启动1,正等待客户端调用...");
客户端:
String remoteName = "//localhost:8089/Remote_Hello";
Registry registry = LocateRegistry.getRegistry(8089) ;
//获得远程对象
IRemoteHello hello = (IRemoteHello)Naming.lookup(remoteName);
System.out.println(hello.getDate());
方法二:
服务器端:
String serverName = "Remote_Hello";
int port = 8089;
Registry registry = LocateRegistry.createRegistry(port) ;
//生成远程对象
RemoteHello hello = new RemoteHello();
registry.bind(serverName,hello);
System.out.println("服务启动21,正等待客户端调用...");
客户端:
String remoteName = "Remote_Hello";
Registry registry = LocateRegistry.getRegistry(8089) ;
//获得远程对象
IRemoteHello hello = (IRemoteHello)registry.lookup(remoteName);
//获得远程调用结果
System.out.println("二1:"+hello.getDate());疑问:为什么在服务器端既可以使用Naming来绑定对象也可以用registry 来绑定对象? 他们之间的区别和关联在什么地方?还有就是我在学习的时候总觉得rmi和jndi视乎是混杂在一起的,不能很好的将他们界定开来。jndi可以将设备或者对象用一个名字来命名,然后我们可以使用这个名字来找到相应的设备或者对象,那么这个时候我们不就可以操作对象了吗? 然后在这个rmi中的一些操作感觉就像和jndi一样似的。就像上面对象的注册或者绑定,以及客户端使用的lookup方法,似乎也是一种关联和查找的操作。 希望大家能帮帮小弟解决下我心中的困惑。谢谢!!
Naming绑定和registry绑定没有本质的区别,你使用Naming.bind(...)的时候,底层还是调用registry进行绑定的。关于这一点,建议LZ看jdk源码,一目瞭然。>总觉得rmi和jndi似乎是混杂在一起的
有这样的感觉,纯属正常。特别是使用EJB的时候,感觉到JNDI和RMI同在...
不过,只需要你仔细研究JDK文档中关于JNDI的说明,你就会恍然大悟。
JNDI通常分为API和SPI,API不言而喻就是一般场景使用的客户端API;而SPI指的是JNDI服务提供商需要实现的,SPI的实现可以基于LDAP,DNS,CORBA,RMI等,其实就是说客户端使用API连接到JNDI服务器,他也许并不关心你JNDI服务提供者怎么存储和管理(服务提供商可以把对象存储在LDAP,CORBA服务器上,但是客户端可以不关心这个问题,只要他能lookup着就可以了)。上面可以看出来,JNDI的SPI可以选择使用RMI的registry。以下是JDK文档中关于【RMI Registry Service Provider for the Java Naming and Directory InterfaceTM (JNDI)】的介绍原文,你仔细阅读一下.
---
The RMI registry service provider allows JNDI applications to access remote objects registered with the RMI registry. Given the location of a registry, the provider will create a naming context with bindings for the objects registered there. Such a context may be bound into another JNDI-accessible namespace (such as LDAP, for example). References to individual remote objects may likewise be bound into another namespace.
---再者,你使用JNDI的时候,如果对方的SPI是基于RMI的,那么你JNDI客户端一定会有如下
For example:
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.rmi.registry.RegistryContextFactory");
env.put(Context.PROVIDER_URL, "rmi://server:1099");
如果说JNDI客户端连接的JNDI服务器是基于LDAP的,那么相应的Context.INITIAL_CONTEXT_FACTORY就需要编程LDAP的,PROVIDER_URL相应也要变为ldap://......不知道有否打开你的疑问。