使用RMI实现远程方法调用

 

题目如下:

开发一个称为“城市信息”服务器的应用程序,这个城市信息服务器的客户程序通过提供一个城市名,获得这个城市的相关信息。在此应用程序中,服务器程序只提供两个方法:一个获取人口;另一个获取温度。

而且必须用RMI去实现。

 

(1)定义一个远程接口MyRmi

文件名:MyRmi.java

  1.  import java.rmi.*; 
  2.  public interface MyRmi extends Remote { 
  3.      public int sum(String a) throws RemoteException;
  4.      public int tem(String b) throws RemoteException;
  5.  }

(2)实现远程接口和服务器;

文件名:RmiImpl.java

  1.   import java.rmi.*; 
  2.   import java.rmi.server.*; 
  3.   public class RmiImpl extends UnicastRemoteObject 
  4.                            implements MyRmi { 
  5.       RmiImpl() throws RemoteException { 
  6.          super(); 
  7.       } 
  8.       public int sum(String a) throws RemoteException {
  9.         int s=0;
  10.         if(a.equals("保定"))          
  11.                 s=1000000;
  12.         if(a.equals("北京"))              
  13.                 s=2000000;
  14.         if(a.equals("石家庄"))             
  15.                 s=1500000;                  
  16.         
  17.         return s;
  18.       }
  19.       public int tem(String b) throws RemoteException {
  20.         int s=0;
  21.         if(b.equals("保定"))              
  22.                 s=26;
  23.         if(b.equals("北京"))              
  24.                 s=28;
  25.         if(b.equals("石家庄"))             
  26.                 s=29;                   
  27.         
  28.         return s; 
  29.       } 
  30.   } 

文件名:RmiServer.java

  1.  import java.rmi.*;
  2.  import java.rmi.registry.*;
  3.  public class RmiServer{
  4.      public static void main(String args[]) {
  5.          try { 
  6.          LocateRegistry.createRegistry(8808) ;
  7.          RmiImpl Server = new RmiImpl();
  8.          // 将该对象实例与名称“SAMPLE-SERVER”捆绑 
  9.          Naming.rebind("//localhost:8808/SAMPLE-SERVER" , Server);
  10.          } catch (java.net.MalformedURLException me) {
  11.             System.out.println("Malformed URL: " + me.toString());
  12.          } catch (RemoteException re) { 
  13.             System.out.println("Remote exception: " + re.toString());
  14.          }
  15.      }
  16.   }

(3)使用远程接口开发一个客户程序;

文件名:RmiClient.java

  1.  import java.rmi.*;
  2.  import java.rmi.server.*;
  3.  public class RmiClient {
  4.      public static void main(String[] args)
  5.      { 
  6.          try {
  7.             String url = "//localhost:8808/SAMPLE-SERVER";
  8.             MyRmi RmiObject = (MyRmi)Naming.lookup(url);
  9.             System.out.println(" 北京市人口是 " + RmiObject.sum("北京") );
  10.             System.out.println(" 北京市温度是 " + RmiObject.tem("北京") );
  11.          } catch (RemoteException exc) {
  12.              System.out.println("Error in lookup: " + exc.toString());
  13.          } catch (java.net.MalformedURLException exc) { 
  14.              System.out.println("Malformed URL: " + exc.toString());
  15.          } catch (java.rmi.NotBoundException exc) {
  16.              System.out.println("NotBound: " + exc.toString());
  17.          }
  18.       }
  19.    } 

(4)产生桩和构架。

编写一个批处理文件Build.bat,方便操作。写入下面内容.

javac MyRmi.java RmiImpl.java RmiServer.java RmiClient.java

rmic -v1.2 Rmilmpl

其中rmic -v1.2 Rmilmpl会在目录下产生RmiImpl_Stub.class

(5)启动RMI注册表。

rmiregistry

这条命令用于启动RMI服务器。可以添加到上面的批处理后面。

(6)运行服务器和客户程序。

写两个批处理文件,运行服务端和客户端

java RmiServer

java RmiClient

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值