RMI程序设计教程
HelloWorld.java
package cn.itcareers.helloworld.interfaces ;
//导入远程接口类
import java.rmi.Remote ;
import java.rmi.RemoteException ;
/*
这个结构需要考虑三件事情:
1、把该接口放进后缀为“interfaces”的包中
2、该接口扩展了java.rmi.Remote
3、helloWorld必须抛出一个java.rmi.RemoteException异常
*/
public interface HelloWorld extends Remote //标记这个接口是一个远程接口
{
public static String NAME = "helloworld" ;
public String helloWorld(String name) throws RemoteException;
}
HelloWorldImpl.java
package cn.itcareers.helloworld.server ;
import java.rmi.RemoteException ;
import java.rmi.server.RemoteServer ;
import java.rmi.server.UnicastRemoteObject ;
import cn.itcareers.helloworld.interfaces.HelloWorld ;
/*
类HelloWorldImpl扩展了java.rmi.server.RemoteServer。通过继承RemoteServer,可以获得equals和hashCode方法实现。
这些方法在分布式应用中非常有用
*/
public class HelloWorldImpl extends RemoteServer implements HelloWorld
{
//因为导出的是一个可能失败的过程,所以必须从自己的够函数抛出java.rmi.RemoteException异常
public HelloWorldImpl() throws RemoteException
{
//RMI实现创建了一个绑定到端口1234的监听器套接字
/*
通过继承RemoteServer,可以获得equals和hashCode方法的实现,这些方法在分布式应用中非常有用,
更具体地说,它们使用分配给远程对象的唯一对象标识,该标识是在执行这些方法的导出过程中分配的。
*/
UnicastRemoteObject.exportObject(this,1234);
/*
如果扩展了java.rmi.server.UnicastRemoteObject而不是java.rmi.server.RemoteServer,
那么UnicastRemoteObject的缺省构造函数将亲自执行这一导出操作
即:
public HelloWorldImpl() throws RemoteException{
super() ;
}
*/
}
//实现远程接口
public String helloWorld(String name)
{
return "Hello "+ name + "!!" ;
}
}
Main.java
package cn.itcareers.helloworld.server ;
import java.io.IOException ;
import java.net.MalformedURLException ;
import java.rmi.Naming ;
import java.rmi.AlreadyBoundException ;
import java.rmi.RemoteException ;
//该类的任务是执行这些操作并管理该远程对象
/*
该管理器类与HellloWorld远程对象类位于相同的包中,因为它本身是本有程序中调用服务器的一部分,
随后导入想要使用的类和异常。
*/
import cn.itcareers.helloworld.interfaces.HelloWorld ;
public class Main
{
public static void main(String[] args)
{
//初始化该类的一个实例
new Main() ;
}
public Main()
{
try
{
//1、创建一个远程对象
HelloWorld server = new HelloWorldImpl() ;
//2、将远程对象绑定到RMI注册
Naming.rebind(HelloWorldImpl.NAME,server) ;
System.out.println("服务已经开始并注册");
}
catch(MalformedURLException e)
{
System.out.println("这个服务名称不正确");
e.printStackTrace() ;
}
catch(RemoteException e)
{
System.out.println("这个对象不能被创建") ;
e.printStackTrace();
}
}
}
MainClient.java
package cn.itcareers.helloworld.client ;
import java.io.IOException ;
import java.net.MalformedURLException ;
import java.rmi.Naming ;
import java.rmi.NotBoundException ;
import java.rmi.RemoteException ;
import java.util.Properties ;
import cn.itcareers.helloworld.interfaces.HelloWorld ;
//在本类中,把客户声明为Runnable并提供了一个main方法,该方法在启动应用时被调用
public class MainClient implements Runnable
{
public static void main(String[] args) throws Exception
{
new MainClient().run();
}
public void run()
{
try
{
//本地远程对象
HelloWorld server = (HelloWorld)Naming.lookup("rmi://localhost/"+HelloWorld.NAME);
//呼叫helloWorld方法
String result = server.helloWorld("World");
System.out.println(result) ;
}
catch(NotBoundException e)
{
System.out.println("这个服务没有被发现");
e.printStackTrace();
}
catch(MalformedURLException e)
{
System.out.println("这个服务的名称不正确");
e.printStackTrace();
}
catch(Exception e)
{
System.out.println("这个对象不能被创建");
e.printStackTrace();
}
}
}
实验步骤:
1、首先对所有文件进行编辑 javac -d . *.java
2、之后运行rmic cn.itcareers.helloworld.server.HelloWorldImpl,产生stub和skel
3、启动RMI注册器 rmiregistry
4、重新打开一个命令行窗口 运行RMI的服务器端程序
java cn.itcareers.helloworld.server.Main
5、再重新打开一个命令行窗口 运行RMI的客户端程序
java cn.itcareers.helloworld.client.MainClient
HelloWorld.java
package cn.itcareers.helloworld.interfaces ;
//导入远程接口类
import java.rmi.Remote ;
import java.rmi.RemoteException ;
/*
这个结构需要考虑三件事情:
1、把该接口放进后缀为“interfaces”的包中
2、该接口扩展了java.rmi.Remote
3、helloWorld必须抛出一个java.rmi.RemoteException异常
*/
public interface HelloWorld extends Remote //标记这个接口是一个远程接口
{
public static String NAME = "helloworld" ;
public String helloWorld(String name) throws RemoteException;
}
HelloWorldImpl.java
package cn.itcareers.helloworld.server ;
import java.rmi.RemoteException ;
import java.rmi.server.RemoteServer ;
import java.rmi.server.UnicastRemoteObject ;
import cn.itcareers.helloworld.interfaces.HelloWorld ;
/*
类HelloWorldImpl扩展了java.rmi.server.RemoteServer。通过继承RemoteServer,可以获得equals和hashCode方法实现。
这些方法在分布式应用中非常有用
*/
public class HelloWorldImpl extends RemoteServer implements HelloWorld
{
//因为导出的是一个可能失败的过程,所以必须从自己的够函数抛出java.rmi.RemoteException异常
public HelloWorldImpl() throws RemoteException
{
//RMI实现创建了一个绑定到端口1234的监听器套接字
/*
通过继承RemoteServer,可以获得equals和hashCode方法的实现,这些方法在分布式应用中非常有用,
更具体地说,它们使用分配给远程对象的唯一对象标识,该标识是在执行这些方法的导出过程中分配的。
*/
UnicastRemoteObject.exportObject(this,1234);
/*
如果扩展了java.rmi.server.UnicastRemoteObject而不是java.rmi.server.RemoteServer,
那么UnicastRemoteObject的缺省构造函数将亲自执行这一导出操作
即:
public HelloWorldImpl() throws RemoteException{
super() ;
}
*/
}
//实现远程接口
public String helloWorld(String name)
{
return "Hello "+ name + "!!" ;
}
}
Main.java
package cn.itcareers.helloworld.server ;
import java.io.IOException ;
import java.net.MalformedURLException ;
import java.rmi.Naming ;
import java.rmi.AlreadyBoundException ;
import java.rmi.RemoteException ;
//该类的任务是执行这些操作并管理该远程对象
/*
该管理器类与HellloWorld远程对象类位于相同的包中,因为它本身是本有程序中调用服务器的一部分,
随后导入想要使用的类和异常。
*/
import cn.itcareers.helloworld.interfaces.HelloWorld ;
public class Main
{
public static void main(String[] args)
{
//初始化该类的一个实例
new Main() ;
}
public Main()
{
try
{
//1、创建一个远程对象
HelloWorld server = new HelloWorldImpl() ;
//2、将远程对象绑定到RMI注册
Naming.rebind(HelloWorldImpl.NAME,server) ;
System.out.println("服务已经开始并注册");
}
catch(MalformedURLException e)
{
System.out.println("这个服务名称不正确");
e.printStackTrace() ;
}
catch(RemoteException e)
{
System.out.println("这个对象不能被创建") ;
e.printStackTrace();
}
}
}
MainClient.java
package cn.itcareers.helloworld.client ;
import java.io.IOException ;
import java.net.MalformedURLException ;
import java.rmi.Naming ;
import java.rmi.NotBoundException ;
import java.rmi.RemoteException ;
import java.util.Properties ;
import cn.itcareers.helloworld.interfaces.HelloWorld ;
//在本类中,把客户声明为Runnable并提供了一个main方法,该方法在启动应用时被调用
public class MainClient implements Runnable
{
public static void main(String[] args) throws Exception
{
new MainClient().run();
}
public void run()
{
try
{
//本地远程对象
HelloWorld server = (HelloWorld)Naming.lookup("rmi://localhost/"+HelloWorld.NAME);
//呼叫helloWorld方法
String result = server.helloWorld("World");
System.out.println(result) ;
}
catch(NotBoundException e)
{
System.out.println("这个服务没有被发现");
e.printStackTrace();
}
catch(MalformedURLException e)
{
System.out.println("这个服务的名称不正确");
e.printStackTrace();
}
catch(Exception e)
{
System.out.println("这个对象不能被创建");
e.printStackTrace();
}
}
}
实验步骤:
1、首先对所有文件进行编辑 javac -d . *.java
2、之后运行rmic cn.itcareers.helloworld.server.HelloWorldImpl,产生stub和skel
3、启动RMI注册器 rmiregistry
4、重新打开一个命令行窗口 运行RMI的服务器端程序
java cn.itcareers.helloworld.server.Main
5、再重新打开一个命令行窗口 运行RMI的客户端程序
java cn.itcareers.helloworld.client.MainClient