java远程方法调用(RMI)

Java远程方法调用(Java Remote Method Invocation,简称RMI)是实现RPC的一种机制。Java RMI实现过程可分为以下几个步骤:

1.       创建远程接口及声明远程方法;

2.       创建远程对象及实现远程方法;

3.       服务器端启动RMI注册服务,注册远程对象;

4.       客户端查找远程对象并调用远程方法。

远程接口具有如下特点: 

1. 远程接口必须为public属性。如果不这样,除非客户端与远程接口在同一个包内,否则 当试图装入实现该远程接口的远程对象时,调用会得到错误结果。 

2. 远程接口必须扩展接口java.rmi.Remote。 

 

  3. 除与应用程序本身特定的例外之外,远程接口中的每个方法都必须在自己的throws从句中 声明java.rmi.RemoteException。(或 RemoteException 的父类)。 

4. 作为参数或返回值传递的一个远程对象(不管是直接,还是本地对象中嵌入)必须声明为远 程接口,而不应声明为实施类。

  5. 参数或返回值若为对象,该对象必须实行序列号接口 Serializable

 

其代码如下:

1)     创建远程接口及声明远程方法

 

package com.server;

Java代码   收藏代码
  1. import java.rmi.Remote;  
  2. import java.rmi.RemoteException;  
  3.   
  4. public interface RmiSample extends Remote{  
  5.     public int sum(int a, int b) throws RemoteException;  
  6.       
  7.     public void save(Student s) throws RemoteException;  
  8.       
  9.     public Student get() throws RemoteException;  
  10. }  
 

2)      创建远程对象及实现远程接口方法

 

Java代码   收藏代码
  1. package com.server;  
  2.   
  3. import java.rmi.RemoteException;  
  4. import java.rmi.server.UnicastRemoteObject;  
  5.   
  6. public class RmiSampleImpl extends UnicastRemoteObject implements RmiSample{  
  7.   
  8.     /** 
  9.      *  
  10.      */  
  11.     private static final long serialVersionUID = -7851182277085789517L;  
  12.   
  13.     protected RmiSampleImpl() throws RemoteException {  
  14.         super();  
  15.     }  
  16.       
  17.     @Override  
  18.     public int sum(int a, int b) throws RemoteException {  
  19.         return a + b;  
  20.     }  
  21.   
  22.     @Override  
  23.     public void save(Student s) throws RemoteException {  
  24.         System.out.println("student id is :"+s.getId());  
  25.         System.out.println("student name is :"+s.getName());  
  26.     }  
  27.   
  28.     @Override  
  29.     public Student get() throws RemoteException {  
  30.         Student s = new Student();  
  31.         s.setId(2);  
  32.         s.setName("学生2");  
  33.         return s;  
  34.     }  
  35.   
  36. }  

3)       服务器端启动RMI注册服务,将远程对象进行注册
Java代码   收藏代码
  1. package com.server;  
  2.   
  3. import java.net.MalformedURLException;  
  4. import java.rmi.Naming;  
  5. import java.rmi.RemoteException;  
  6. import java.rmi.registry.LocateRegistry;  
  7.   
  8. public class RmiSampleServer {  
  9.      public static void main(String[] args) {     
  10.             try{     
  11.                 LocateRegistry.createRegistry(8808);     
  12.                 //LocateRegistry.createRegistry(1099);     
  13.                 RmiSampleImpl server= new RmiSampleImpl();     
  14.                 Naming.rebind("//localhost:8808/SAMPLE-SERVER" , server);  
  15.                 //Naming.rebind("server" , server);  
  16.             }catch (MalformedURLException me){     
  17.                 System.out.println("Malformed URL: " + me.toString());     
  18.             }catch(RemoteException re){     
  19.                 System.out.println("Remote Exception: "+re.toString());     
  20.             }     
  21.         }   
  22. }  

4)       客户端查找远程对象并调用远程方法
Java代码   收藏代码
  1. package com.client;  
  2.   
  3. import java.rmi.RemoteException;  
  4. import java.rmi.Naming;     
  5.   
  6. import com.server.RmiSample;  
  7. import com.server.Student;  
  8.     
  9.   
  10. public class RmiSampleClient {  
  11.      public static void main(String[] args) {     
  12.             try {     
  13.                 String url = "//localhost:8808/SAMPLE-SERVER";     
  14.                 //RmiSample RmiObject = (RmiSample) Naming.lookup("server");  
  15.                 RmiSample RmiObject = (RmiSample) Naming.lookup(url);  
  16.                 System.out.println(" 3 + 2 = " + RmiObject.sum(3, 2));  
  17.                   
  18.                 Student s = new Student();  
  19.                 s.setId(1);  
  20.                 s.setName("学生1");  
  21.                 RmiObject.save(s);  
  22.                   
  23.                 Student student = RmiObject.get();  
  24.                 System.out.println("student id is :"+student.getId());  
  25.                 System.out.println("student name is :"+student.getName());  
  26.             } catch (RemoteException rex) {     
  27.                 System.out.println("Error in lookup: " + rex.toString());     
  28.             } catch (java.net.MalformedURLException me) {     
  29.                 System.out.println("Malformed URL: " + me.toString());     
  30.             } catch (java.rmi.NotBoundException ne) {     
  31.                 System.out.println("NotBound: " + ne.toString());     
  32.             }  
  33.         
  34.         }     


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值