天天看点

学习笔记:JAVA RMI远程方法调用简单实例

RMI的概念

  RMI的开发步骤

  先创建远程接口及声明远程方法,注意这是实现双方通讯的接口,需要继承Remote

  开发一个类来实现远程接口及远程方法,值得注意的是实现类需要继承UnicastRemoteObject

  通过javac命令编译文件,通过java -server 命令注册服务,启动远程对象

  最后客户端查找远程对象,并调用远程方法

  简单实例

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

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;

  创建远程接口PersonService,注意远程接口需要继承Remote

package rmi.service;

import java.rmi.Remote;

import java.rmi.RemoteException;

import java.util.List;

import rmi.model.*;

//此为远程对象调用的接口,必须继承Remote类

public interface PersonService extends Remote {

public List<PersonEntity> GetList() throws RemoteException;

  建立PersonServiceImpl实现远程接口,注意此为远程对象实现类,需要继承UnicastRemoteObject

package rmi.serviceImpl;

import java.rmi.server.UnicastRemoteObject;

import java.util.LinkedList;

import rmi.model.PersonEntity;

import rmi.service.*;

//此为远程对象的实现类,须继承UnicastRemoteObject

public class PersonServiceImpl extends UnicastRemoteObject implements PersonService {

public PersonServiceImpl() throws RemoteException {

super();

// TODO Auto-generated constructor stub

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.serviceImpl.*;

public class Program{

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) {

// TODO Auto-generated catch block

e.printStackTrace();

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

package rmi.remotingclient;

public class Program {

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 ex){

ex.printStackTrace();

  常见错误

  在命令提示符调用java命令时,显示并无此命令。这是因为未在“环境变量”中绑定JAVA的JDK命令造成的,你首先单击“计算机右键”->“属性”->“高级”->“环境变量”。在系统变量Path设置中加载为JDK的路径  .;D:\Program Files\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\bin。然后在ClassPath加载服务器端的Program.class地址 .;D:\\RMI\RemotingService\bin

  在调用javac命令时出现“javac 找不到文件 ..... ”此错误,可能是因为阁下输入的文件路径出现错误造成,注意不要把D:\\RMI\RemotingService\src>javac rmi/remotingservice/Program.java写错为D:\\RMI\RemotingService\src>javac rmi.remotingservice.Program.java

  在调用D:\\RMI\RemotingService\bin>java rmi/remotingservice/Program命令时出现“Exception in thread 'main' java.lang.NoClassEdfoundError”错误,第一这可能是阁下把Program错写为Program.class,注意java命令不需要加后缀名。第二可能是阁下把“java rmi/remotingservice/Program”错写为“java rmi\remotingservice\Program"。   

最新内容请见作者的GitHub页:http://qaseven.github.io/