RMI参考


RMI远程调用技术是基于servlet容器基础之上的

1,定义本地提供的远程调用接口,对外接口要继承java.rmi.Remote
2,接口实现类在实现对外接口的时候要继承java.rmi.server.UnicastRemoteObject

System.setSecurityManager(new java.rmi.RMISecurityManager());
在程序入口定义rmi安全管理器

java.rmi.Naming.rebind("/Customer", cust);//将应用程序绑定到了RMI名字空间,这样对象就可以被外界了解

编写好服务端之后 要通过rmic命令编译rmi服务端代码生成代码存根(stub)和框架(skeleton)
xx_stub.class //客户端,用来发送请求数据
xx_skel.class //服务端框架,用来解析请求数据

rmic命令: rmic -d 服务端根目录

有点类似webservice

客户端调用
  ·远程对象使用Naming.lookup()服务来解析。我们给出了远程对象的外部名称,然后在目标主机的rmiregistry中查找它。
  ·所有对远程方法的调用都可能会产生一个远程异常。
String host = "";
    CustomerInterface cust = (CustomerInterface)java.rmi.Naming.lookup(host + "Customer");

     // We have an instance of the remote object. Get the
     // customer information
     CustomerData data = cust.getCustomerData(id);

RMI部署:
(1)定义远程接口(继承Remote接口即可);
(2)定义应用程序服务执行类,该类必须执行远程定义接口且继承UnicastRemoteObject;
(3)定义应用程序服务端程序入口(main函数),绑定远程对象
(4)使用rmic工具生成执行类的存根,程序员并不直接使用该类
(5)启动RMI注册表工具(rmiregistry)
(6)启动应用程序服务器
(7)检查服务注册
(8)将Server目录中远程定义接口和存根复制到Client目录
(9)定义客户端程序入口(main函数),检索远程接口,实现接口调用

接口定义和客户端程序必须拷贝到客户端

posted on 2013-07-04 23:14  java疯子  阅读(216)  评论(0编辑  收藏  举报

导航