标签:java远程调用rmi
RMI是个典型的为java定制的远程通信协议,我们都知道,在single vm中,我们可以通过直接调用java object instance来实现通信,那么在远程通信时,如果也能按照这种方式当然是最好了,这种远程通信的机制成为RPC(Remote Procedure Call),RMI正是朝着这个目标而诞生的。
来看下基于RMI的一次完整的远程通信过程的原理:
PersonService.java
package rmi.service; import java.rmi.Remote; import java.rmi.RemoteException; //此为远程对象调用的接口,必须继承Remote类 public interface PersonService extends Remote { public String getPersonName(int id) throws RemoteException; }
package rmi.serviceImpl; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import rmi.service.PersonService; public class PersonServiceImpl extends UnicastRemoteObject implements PersonService { public PersonServiceImpl() throws RemoteException { super(); } @Override public String getPersonName(int id) throws RemoteException { String returnName; if (0 == id) { returnName = "张三"; } else if (1 == id) { returnName = "李四"; } else if (2 == id) { returnName = "王五"; } else { returnName = "不存在"; } System.out.println("------------getPersonName(" + id + ")------------"); return returnName; } }
package rmi.remote; import java.rmi.Naming; import java.rmi.registry.LocateRegistry; import rmi.service.PersonService; import rmi.serviceImpl.PersonServiceImpl; public class ReremoServer { public static void main(String[] args) { try { PersonService personService = new PersonServiceImpl(); // 注册通讯端口 LocateRegistry.createRegistry(6600); // 注册通讯路径 Naming.rebind("rmi://localhost:6600/PersonService", personService); System.out.println("------服务启动成功------"); } catch (Exception e) { e.printStackTrace(); } } }
package rmi.remote; import java.rmi.Naming; import rmi.service.PersonService; public class ReremoClient { public static void main(String[] args) { try { // 调用远程对象,注意RMI路径与接口必须与服务器配置一致 PersonService personService = (PersonService) Naming.lookup("rmi://localhost:6600/PersonService"); for (int i = 0; i < 5; i++) { String name = personService.getPersonName(i); System.out.println("name:" + name); } } catch (Exception ex) { ex.printStackTrace(); } } }
标签:java远程调用rmi
原文地址:http://blog.csdn.net/adam_wzs/article/details/42193587