Java RMI技术

来源:互联网 发布:淘宝长尾词 编辑:程序博客网 时间:2024/05/17 07:31

java rmi即java远程接口调用,实现了2台虚拟机之间的程序调用,这样,网络上的任何两台计算机就可以相互调用对方的程序(如果允许)。
    好了,下面就以经典的 Hello world作为例子讲解。开发rmi的步骤如下。
1、编写远程接口,远程接口实现类
2、编译
3、生成接口实现类存根
4、在服务器端启动rmiregistry命令
5、在服务端注册发布远程对象
6、在客户端获取远程对象

实例:
以下远程接口类为HelloRemote,实现类为HelloImpl,服务器端的发布类为RMIServer,客户端的调用类为RMIClient,为简单考虑,服务器和客户端均在同一台机器(所谓服务器和客户机,是两者运行在2个虚拟机进程下)。
一、编写类
1、远程接口类,需基础java.rmi.Remote接口,且方法抛出RMIException
public interface HelloRemote extends Remote{public void sayHello() throws RemoteException;   }

2、远程接口实现类,需继承UnicastRemoteObject

public class HelloImpl extends UnicastRemoteObject  implements HelloRemote{public HelloImpl() throws RemoteException {super();}public void sayHello() throws RemoteException {System.out.println("Hello World!");}}


3、服务端类
服务端类用于发布远程对象
public class RMIServer {public static void main(String[] args) throws RemoteException, MalformedURLException {HelloRemote hello=new HelloImpl();Naming.rebind("hello", hello);}}


4、客户端类
public class RMIClient {public static void main(String args[]) throws MalformedURLException, RemoteException, NotBoundException{                System.setSecurityManager(new   RMISecurityManager());//如果服务器和客户端不再同一台机器要加这行HelloRemote hello=(HelloRemote) Naming.lookup("hello");   hello.sayHello();}}

远程接口类需同时在服务端和客户端存在

二、编译(注意以上类都没有包,这里主要是简化操作考虑)
客户端文件夹在 E:/client(简称client),服务端在E:/server(简称server)
分别编译以上4个文件。然后将Hello.class分别放到client和server下,将Client.class放到client下,Server.class放到server下,HelloImpl.class文件放到server文件夹下
三、生成存根和骨架
进入server文件夹在命令行下输入命令 rmic HelloImpl,将生成一个HelloImpl_Stub.class文件
将此文件复制到client目录下(server与client均该文件)

四、运行注册程序
在命令行中进入server文件夹 输入命令 rmiregistry,用于启动注册,在此前需设置classpath为E:/server,输入rmiregistry命令后将在server文件夹下生成一个

五、运行服务类
java Server
六、运行客户类
java Client
你将看到执行成功。

当你执行不顺利时,请看看下面:
1、最终执行时要3个命令行窗口,一个运行rmiregistry,一个运行Server,一个运行Clinet,先执行rmiregistry命令,接着执行java Server,最后执行java Client
2、客户端和服务端均要有存根(HelloImpl_Stub.class)和远程接口的定义,且包名要一致。
3、当运行服务器时,提示找不到HelloImpl_Stub,那是你没指定类路径,那请你在执行rmiregistry前设置classpath
4、出现AscessableException访问非法时,需要修改此略文件,在jre/security目录下的java.policy文件中(如果你有多个jre,你无法确定是哪个。那就全部修改吧),在该文件中添加如下内容
grant {
           permission java.net.SocketPermission "*:1024-65535",
                "connect,accept";
           permission java.net.SocketPermission "*:80","connect";
        };

5、指定端口,默认是1099,可以在运行rmiregistry时指定 如 rmiregistry 1098,也可以在Server.java中指定,如Registry.createRegistry(1088).

6、绑定地址的设置
简单形式Naming.bind("hello"),本机地址时才可以
完全形式Naming.bind("rmi://117.45.220.11:1099/RMI_Hello")


package RMITest;import java.rmi.Remote;import java.rmi.RemoteException;public interface Example extends Remote {public void setString( String s ) throws RemoteException;  public String getString() throws RemoteException;}

package RMITest;import java.rmi.RemoteException;import java.rmi.server.UnicastRemoteObject;public class ExampleServer extends UnicastRemoteObject implements Example {private String stringState;public ExampleServer() throws RemoteException {}@Overridepublic void setString(String s) throws RemoteException {// TODO Auto-generated method stubstringState = s;}@Overridepublic String getString() throws RemoteException {// TODO Auto-generated method stubreturn stringState;}}

import java.rmi.registry.LocateRegistry;public class Server {/** * @param args * @throws MalformedURLException  * @throws RemoteException  */public static void main(String[] args) throws RemoteException, MalformedURLException {// TODO Auto-generated method stubLocateRegistry.createRegistry(8889); ExampleServer es = new ExampleServer();Naming.rebind("rmi://localhost:8889/Example", es);}}

package RMITest;import java.net.MalformedURLException;import java.rmi.Naming;import java.rmi.NotBoundException;import java.rmi.RemoteException;public class ExampleClient {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubtry {Example example = (Example) Naming.lookup("rmi://localhost:8889/Example");example.setString("success");System.out.println(example.getString());} catch (MalformedURLException | RemoteException | NotBoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}



原创粉丝点击