1.RMI的概念
RMI(Remote Method Invocation)远程方法调用是一种计算机之间利用远程对象互相调用实现双方通讯的一种通讯机制。
使用这种机制,某一台计算机上的对象可以调用另外一台计算机上的对象来获取远程数据。
RMI是Enterprise JavaBeans的支柱,是建立分布式Java应用程序的方便途径。
在过去,TCP/IP套接字通讯是远程通讯的主要手段,但此开发方式没有使用面向对象的方式实现开发,在开发一个如此的通讯机制时往往令程序员感觉到乏味,对此RPC(Remote Procedure Call)应运而生,它使程序员更容易地调用远程程序,但在面对复杂的信息传讯时,RPC依然未能很好的支持,而且RPC未能做到面向对象调用的开发模式。
针对RPC服务遗留的问题,RMI出现在世人面前,它被设计成一种面向对象的通讯方式,允许程序员使用远程对象来实现通信,并且支持多线程的服务,这是一次远程通讯的革命,为远程通信开辟新的里程碑。
2.使用步骤
3. 示例
1. modal层
package com.love; //modal 必须加入序列化接口 而且使用 public class PersonEntity implements java.io.Serializable { private static final long serialVersionUID = -2789846215850392966L; privateint id; private String name; privateint age; publicvoid setId(int id) { this.id = id; } publicint getId() { return id; } publicvoid setName(String name) { this.name = name; } public String getName() { return name; } publicvoid setAge(int age) { this.age = age; } publicint getAge() { return age; } }
2. service 接口
package com.love; import java.rmi.Remote; import java.rmi.RemoteException; import java.util.List; //此为远程对象调用的接口,必须继承Remote类 publicinterface PersonService extends Remote { public List<PersonEntity> GetList() throws RemoteException; }
package com.love; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import java.util.LinkedList; import java.util.List; //此为远程对象的实现类,须继承UnicastRemoteObject public class PersonServiceImpl extends UnicastRemoteObject implements PersonService { //可实例化对象 public PersonServiceImpl() throws RemoteException { super(); } @Override public List<PersonEntity> GetList() throws RemoteException { System.out.println("开始获取List"); List<PersonEntity> personList=new LinkedList<PersonEntity>(); PersonEntity person1=new PersonEntity(); person1.setAge(25); person1.setId(0); person1.setName("张三"); personList.add(person1); PersonEntity person2=new PersonEntity(); person2.setAge(25); person2.setId(1); person2.setName("李四"); personList.add(person2); return personList; } }
package com.love; import java.rmi.Naming; import java.rmi.registry.LocateRegistry; public class Server{ public static void main(String[] args) { try { PersonService personService=new PersonServiceImpl(); //注册通讯端口 LocateRegistry.createRegistry(6600); //注册通讯路径 Naming.rebind("rmi://127.0.0.1:6600/PersonService", personService); System.out.println("Service Start!"); } catch (Exception e) { e.printStackTrace(); } } }
package com.love.test; import java.rmi.Naming; import java.util.List; import com.love.PersonEntity;//在远程的另一端要用到该modal中的 PersonEntity import com.love.PersonService; //在远程的另一端要用到该modal中的 PersonService public class Client { public static void main(String[] args){ try{ //调用远程对象,注意RMI路径与接口必须与服务器配置一致 PersonService personService=(PersonService)Naming.lookup("rmi://127.0.0.1:6600/PersonService"); List<PersonEntity> personList=personService.GetList(); for(PersonEntity person:personList){ System.out.println("ID:"+person.getId()+" Age:"+person.getAge()+" Name:"+person.getName()); } }catch(Exception e){ e.printStackTrace(); } } }
这里在Client的一端,按说是没有PersonEntity和PersonService这个类和接口的,所以我们就要把Server端的 PersonEntity和PersonService 打包为jar 然后添加到Client端来实现对应类和接口的实例化
原创:http://blog.csdn.net/qilin001cs