第1种方式:
1. 使用UnicastRemoteObject类的静态方法exportObject将一个Remote对象(服务提供者)export给RMI runtime,这样之后,才可以接受远程调用,并返回一个用于传送给client的stub对象
Server obj = new Server();
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
注:Server对象是Remote的,但不是Serializable的
2. For a caller (client, peer, or applet) to be able to invoke a method on a remote object, that caller must first obtain a stub for the remote object. For bootstrapping, Java RMI provides a registry API for applications to bind a name to a remote object's stub and for clients to look up remote objects by name in order to obtain their stubs.
Caller要调用一个远程对象的方法,就先要有该对象的引用,该引用可以是方法调用的参数,可以是一个返回值,比如在RMI中使用工厂模式时就是使用了方法的返回值。
Registry registry = LocateRegistry.getRegistry();
registry.bind("Hello", stub);
注:LocateRegistry.getRegistry()只是返回一个stub,并不检查rmiregistry是否在运行,如果没有运行就会抛出RemoteException
LocateRegistry.getRegistry()返回一个实现了java.rmi.registry.Registry 的stub,并向本机rmiregistry监听的默认端口1099发出调用请求。
第2种方式
1. 让服务提供者继承UnicastRemoteObject ,然后在lauch code中使用如下函数
Naming.rebind
代码如下:
Hello h = new HelloImpl();
Naming.rebind("hello",h);
其中HelloImpl的声明如下:
public class HelloImpl extends UnicastRemoteObject implements Hello
问题:这两种实现方式的原理各是什么?有什么差异?