您的位置:首页 > 编程语言 > Java开发

JAVA RMI远程方法调用简单实例

2015-10-19 13:28 591 查看
RMI的概念

RMI(Remote Method Invocation)远程方法调用是一种计算机之间利用远程对象互相调用实现双方通讯的一种通讯机制。使用这种机制,某一台计算机上的对象可以调用另外一台计算机上的对象来获取远程数据。RMI是Enterprise JavaBeans的支柱,是建立分布式Java应用程序的方便途径。在过去,TCP/IP套接字通讯是远程通讯的主要手段,但此开发方式没有使用面向对象的方式实现开发,在开发一个如此的通讯机制时往往令程序员感觉到乏味,对此RPC(Remote
Procedure Call)应运而生,它使程序员更容易地调用远程程序,但在面对复杂的信息传讯时,RPC依然未能很好的支持,而且RPC未能做到面向对象调用的开发模式。针对RPC服务遗留的问题,RMI出现在世人面前,它被设计成一种面向对象的通讯方式,允许程序员使用远程对象来实现通信,并且支持多线程的服务,这是一次远程通讯的革命,为远程通信开辟新的里程碑。

RMI的开发步骤

先创建远程接口及声明远程方法,注意这是实现双方通讯的接口,需要继承Remote
开发一个类来实现远程接口及远程方法,值得注意的是实现类需要继承UnicastRemoteObject
通过javac命令编译文件,通过java -server 命令注册服务,启动远程对象
最后客户端查找远程对象,并调用远程方法

简单实例

首先为服务建立一个Model层,注意因为此对象需要现实进行远程传输,所以必须继承Serializable

<pre name="code" class="java">代码

package rmi.model;

import java.io.Serializable;

//注意对象必须继承Serializable
publicclass PersonEntity implements Serializable {
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;
}
}


</pre>
创建远程接口PersonService,注意远程接口需要继承Remote

package rmi.service;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.List;
import rmi.model.*;

//此为远程对象调用的接口,必须继承Remote类
publicinterface PersonService extends Remote {
public List<PersonEntity> GetList() throws RemoteException;
}
建立PersonServiceImpl实现远程接口,注意此为远程对象实现类,需要继承UnicastRemoteObject

代码

package rmi.serviceImpl;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.LinkedList;
import java.util.List;

import rmi.model.PersonEntity;
import rmi.service.*;

//此为远程对象的实现类,须继承UnicastRemoteObject
publicclass PersonServiceImpl extends UnicastRemoteObject implements PersonService {

public PersonServiceImpl() throws RemoteException {
super();
// TODO Auto-generated constructor stub
}

@Override
public List<PersonEntity> GetList() throws RemoteException {
// TODO Auto-generated method stub
System.out.println("Get Person Start!");
List<PersonEntity> personList=new LinkedList<PersonEntity>();

PersonEntity person1=new PersonEntity();
person1.setAge(25);
person1.setId(0);
person1.setName("Leslie");
personList.add(person1);

PersonEntity person2=new PersonEntity();
person2.setAge(25);
person2.setId(1);
person2.setName("Rose");
personList.add(person2);

return personList;
}
}
建立服务器端,在服务器端注册RMI通讯端口与通讯路径,然后通讯javac命令编译文件,通过java -server 命令注册服务。以下面代码为例,如果阁下将项目建立于D:\\RMI\RemotingService文件夹上时,则先输入D:\\RMI\RemotingService\src>javac
rmi/remotingservice/Program.java获取Program.class(如何阁下使用的MyEclipse等开发工具,可跳过此步,直接在*/bin文件夹中直接调用已经生成的Program.class),然后输入D:\\RMI\RemotingService\src>java rmi/remotingservice/Program启动服务。

代码

package rmi.remotingservice;

import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;

import rmi.service.*;
import rmi.serviceImpl.*;

publicclass Program{

publicstaticvoid 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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


最后建立客户端进行测试,注意客户调用的RMI路径必须服务器配置一致

代码

package rmi.remotingclient;

import java.rmi.Naming;
import java.util.List;

import rmi.model.PersonEntity;
import rmi.service.*;

publicclass Program {
publicstaticvoid 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 ex){
ex.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: