ITeye博客

java RMI 基础教程

dchaoxiong · 2008-09-21 19:22 · java 基础 · Markdown

java创建远程方法调用的步骤如下:

定义一个扩展远程接口(Remote)的接口,这个接口方法将供给客户端调用,该接口中的每个方法都必须声明抛出RemoteException。

定义一个实现该接口并扩展了UnicastRemoteObject的类(服务端运行的服务类),这个类将实现给客户端调用的所用方法。

创建服务端供客户端调用的应用程序。

创建客户端RMI调用服务端的应用程序。

启动服务端,再启动客户端,测试是否调用成功。

如果客户端和服务端不在同一台电脑上,则需要对刚才定义的服务类采用rmic 编译一个客户端的框架类_stub并拷贝到客户端的类路径下

我的例子:


package com.diaoge.java.rmi.beans;

import java.io.Serializable;

public class Employees implements Serializable{

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

	private String id;
	private String name;
	
	
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getId() {
		return id;
	}
}

定义供远程调用的接口


package com.diaoge.java.rmi;

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

import com.diaoge.java.rmi.beans.Employees;

public interface RemoteInterface extends Remote{

	public String getServerInfo(int index)throws RemoteException;
	
	public Employees getEmployeesById(String id)throws RemoteException;
}

实现远程接口,定义服务类


package com.diaoge.java.rmi;

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

import com.diaoge.java.rmi.beans.Employees;

public class RemoteInterfaceImpl extends UnicastRemoteObject implements RemoteInterface{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3491749919079349917L;


	public RemoteInterfaceImpl()throws RemoteException{
		super();
	}

	@Override
	public Employees getEmployeesById(String id) throws RemoteException {
		Employees employees = new Employees();
		employees.setId(id);
		employees.setName("雕戈_"+id);
		return employees;
	}

	@Override
	public String getServerInfo(int index) throws RemoteException {
		if(index==5)
			throw new RemoteException("远程抛出的异常");
		return "远程方法调用RMI测试程序_"+index;
	}

}

创建服务端应用程序


package com.diaoge.java.rmi;

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

public class RmiServer {
	
	private static final int PORT = 9930;
	
	public static void main(String[] args) {
		try {
			RemoteInterfaceImpl impl = new RemoteInterfaceImpl();
			LocateRegistry.createRegistry(PORT);
			Naming.rebind("//localhost:"+PORT+"/rmitest", impl);
			System.out.println("RMI的服务端已经启动。。。。。");
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		
	}
}

创建客户端应用程序


package com.diaoge.java.rmi;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import com.diaoge.java.rmi.beans.Employees;

public class RmiClient {

	public static void main(String[] args) {
		try {
			final RemoteInterface client = (RemoteInterface) Naming.lookup("//192.168.1.101:9930/rmitest");
			new Thread(new Runnable(){

				@Override
				public void run() {					
					Employees emp;
					int index = 0;
					try {
						while(true){
							Thread.sleep(1000);
							emp = client.getEmployeesById(""+(index++));
							System.out.println("ID="+emp.getId());
							System.out.println("Name="+emp.getName());
						}
					} catch (RemoteException e) {
						e.printStackTrace();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				
			}).start();
			
			new Thread(new Runnable(){

				@Override
				public void run() {					
					int index = 0;
					try {
						while(true){
							Thread.sleep(5000);
							System.out.println(client.getServerInfo(index++));
						}
					} catch (RemoteException e) {
						e.printStackTrace();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				
			}).start();
			
			System.out.println("");
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
	}
}

运行服务端和客户端测试

运行命令rmic 生产总框架


在命令行运行 rmic com.diaoge.java.RemoteInterfaceImpl
然后把生产的RemoteInterfaceImpl_stub.class拷贝到客户端

评论(2)

  1. #2 楼 · dplead · 2010-09-03 10:50

    同志,谢谢了~~

  2. #1 楼 · jape198654 · 2010-03-19 11:42

    太感谢刁哥了!!!收起

评论功能维护中,当前仅展示历史评论。