1、创建
-
创建接口
public interface MyRemote extends Remote{
public String say() throws RemoteException
}
String必须实现序列化Serializataion接口
方法必须抛出RemoteException -
实现接口
public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote{
//实现MyRemote
try{
MyRemote service =new MyRemoteImpl();//创建MyRemote对象
Naming.rebind(“Remote Hello”,service);//将MyRemote对象放入rmiregistry,此时只是代码,就算构建也没放入,因为还没启动rmiregistry,所以其实还没有放入
}
必须继承UnicastRemoteObject或者其他的与远程有关的功能 -
获取stub与skeletion(.class)文件
调用jdk中的rmic对MyRemoteImpl进行加工,产生两个辅助类
MyRemoteImpl_Stub.class与MyRemoteImpl_Skeleton.class -
启动rmiregistry
-
初始化远程服务
构建MyRemoteImpl,将MyRemote对象放入自己电脑的rmiregistry,以供别的电脑的调用。
2、调用
-
客户端应有MyRemote.class,MyRemoteImpl_Stub.class,服务端应有MyRemote.class,MyRemoteImpl.class,MyRemoteImpl_Stub.class与MyRemoteImpl_Skeleton.class两个辅助类,
-
客户端调用代码
MyRemote service=(MyRemote)Naming.lookup("rmi://ip/注册名"); String str=service.say();
-
实际运行过程中,客户端通过注册名查询,返回一个stub对象,调用stub对象的say,stub对象远程调用服务端的skeleton,skeleton调用服务端被放入registry的service对象。