Java RMI技术

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 {}
	@Override
	public void setString(String s) throws RemoteException {
		// TODO Auto-generated method stub
		stringState = s;
	}
	@Override
	public String getString() throws RemoteException {
		// TODO Auto-generated method stub
		return 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 stub
		LocateRegistry.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 stub
		try {
			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 block
			e.printStackTrace();
		}
		
	}

}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值