文档详情

Java网络程序设计 教学课件 房晓溪 第9章 高级RMI编程.pdf

发布:2015-11-11约8.17千字共28页下载文档
文本预览下载声明
第9章 高级RMI编程 第1页 共28页 主要内容 ►“城市信息”服务器 ►实现“工厂” ►实现回调 ►通过RMI签名对象 ►小结 第2页 共28页 9-1 “城市信息”服务器 ►定义一个远程接口 ►实现远程接口 ►开发一个客户程序 ►产生桩和构架 ►启动RMI注册表 ►运行服务器和客户程序 第3页 共28页 9-1-1 定义一个远程接口 ►City接口定义两个远程方法:getPopulation 和getTemperature。这两个方法都只有一个 字符串型(String )的参数cityName,并且 都返回一个整型(int )数。作为一个远程 接口,City接口扩展了Romote接口,并且它 定义的方法中的每一个都可以在必要时抛 出RomoteException异常。 第4页 共28页 9-1-2 实现远程接口 例9.2:CityImpl.java ► import java.rmi. * ; ► import java.rmi.server.UnicastRemoteObject; ► / * * ► * @ (#) CityImpl.java ► * / ► public class CityImpl UnicastRemoteObject implements City{ ► private String name; ► public CityImpl(String name) throws RemoteException{ ► super(); ► this.name = name; ► } 第5页 共28页 接上 ► public int getPopulation(String cityName) throws RemoteException{ ► if (cityName.equals(Toronto) ){ ► return 10; ► }else if (cityname.equals(Ottawa) ){ ► return 2; ► }else { ► returm 0; ► } ► } ► public int getTemperature(String cityName) throws RemoteException{ ► // The temperature returned is just for illustration purposes. ► return 1; ► } 第6页 共28页 接上 ►public static void main(String argv[]){ ► System.setSecurityManager(new RMISecurityManager() ); ►try{ ►CityImpl obj = new CityInpl(CrtyServer); ►Naming.rebind(// hostname/CityServer, obj); ► System.out.println(CityServer bound in registry); ► }catch (Exception e){ ►e.printS
显示全部
相似文档