`

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;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface RmiSample extends Remote{
	public int sum(int a, int b) throws RemoteException;
	
	public void save(Student s) throws RemoteException;
	
	public Student get() throws RemoteException;
}
 

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

 

package com.server;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class RmiSampleImpl extends UnicastRemoteObject implements RmiSample{

	/**
	 * 
	 */
	private static final long serialVersionUID = -7851182277085789517L;

	protected RmiSampleImpl() throws RemoteException {
		super();
	}
	
	@Override
	public int sum(int a, int b) throws RemoteException {
		return a + b;
	}

	@Override
	public void save(Student s) throws RemoteException {
		System.out.println("student id is :"+s.getId());
		System.out.println("student name is :"+s.getName());
	}

	@Override
	public Student get() throws RemoteException {
		Student s = new Student();
		s.setId(2);
		s.setName("学生2");
		return s;
	}

}

3)      服务器端启动RMI注册服务,将远程对象进行注册
package com.server;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

public class RmiSampleServer {
	 public static void main(String[] args) {   
	        try{   
	            LocateRegistry.createRegistry(8808);   
	            //LocateRegistry.createRegistry(1099);   
	            RmiSampleImpl server= new RmiSampleImpl();   
	            Naming.rebind("//localhost:8808/SAMPLE-SERVER" , server);
	            //Naming.rebind("server" , server);
	        }catch (MalformedURLException me){   
	            System.out.println("Malformed URL: " + me.toString());   
	        }catch(RemoteException re){   
	            System.out.println("Remote Exception: "+re.toString());   
	        }   
	    } 
}

4)      客户端查找远程对象并调用远程方法
package com.client;

import java.rmi.RemoteException;
import java.rmi.Naming;   

import com.server.RmiSample;
import com.server.Student;
  

public class RmiSampleClient {
	 public static void main(String[] args) {   
	        try {   
	            String url = "//localhost:8808/SAMPLE-SERVER";   
	            //RmiSample RmiObject = (RmiSample) Naming.lookup("server");
	        	RmiSample RmiObject = (RmiSample) Naming.lookup(url);
	            System.out.println(" 3 + 2 = " + RmiObject.sum(3, 2));
	            
	            Student s = new Student();
	            s.setId(1);
	            s.setName("学生1");
	            RmiObject.save(s);
	            
	            Student student = RmiObject.get();
	            System.out.println("student id is :"+student.getId());
	    		System.out.println("student name is :"+student.getName());
	        } catch (RemoteException rex) {   
	            System.out.println("Error in lookup: " + rex.toString());   
	        } catch (java.net.MalformedURLException me) {   
	            System.out.println("Malformed URL: " + me.toString());   
	        } catch (java.rmi.NotBoundException ne) {   
	            System.out.println("NotBound: " + ne.toString());   
	        }
	  
	    }   
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics