当前位置: 移动技术网 > IT编程>开发语言>Java > Java RMI详细介绍及简单实例

Java RMI详细介绍及简单实例

2019年07月22日  | 移动技术网IT编程  | 我要评论

java rmi详解

概要:

java rmi 指的是远程方法调用 (remote method invocation)。它是一种机制,能够让在某个 java 虚拟机上的对象调用另一个 java 虚拟机中的对象上的方法。可以用此方法调用的任何对象必须实现该远程接口。 

java rmi不是什么新技术(在java1.1的时代都有了),但却是是非常重要的底层技术。

大名鼎鼎的ejb都是建立在rmi基础之上的,现在还有一些开源的远程调用组件,其底层技术也是rmi。 

在大力鼓吹web service、soa的时代,是不是每个应用都应该选用笨拙的web service组件来实现,通过对比测试后,rmi是最简单的,在一些小的应用中是最合适的。

下面通过一个简单的例子来说明rmi的原理和应用,下面这个例子是一个简单helloworld,但已涵盖rmi的核心应用与开发模式。

 
/** 
* created by intellij idea. 
* user: leizhimin 
* date: 2008-8-7 21:50:02 
* 定义一个远程接口,必须继承remote接口,其中需要远程调用的方法必须抛出remoteexception异常 
*/ 
public interface ihello extends remote { 

  /** 
   * 简单的返回“hello world!"字样 
   * @return 返回“hello world!"字样 
   * @throws java.rmi.remoteexception 
   */ 
  public string helloworld() throws remoteexception; 

  /** 
   * 一个简单的业务方法,根据传入的人名返回相应的问候语 
   * @param somebodyname 人名 
   * @return 返回相应的问候语 
   * @throws java.rmi.remoteexception 
   */ 
  public string sayhellotosomebody(string somebodyname) throws remoteexception; 
}
 

/** 
* created by intellij idea. 
* user: leizhimin 
* date: 2008-8-7 21:56:47 
* 远程的接口的实现 
*/ 
public class helloimpl extends unicastremoteobject implements ihello { 
  /** 
   * 因为unicastremoteobject的构造方法抛出了remoteexception异常,因此这里默认的构造方法必须写,必须声明抛出remoteexception异常 
   * 
   * @throws remoteexception 
   */ 
  public helloimpl() throws remoteexception { 
  } 

  /** 
   * 简单的返回“hello world!"字样 
   * 
   * @return 返回“hello world!"字样 
   * @throws java.rmi.remoteexception 
   */ 
  public string helloworld() throws remoteexception { 
    return "hello world!"; 
  } 

  /** 
   * 一个简单的业务方法,根据传入的人名返回相应的问候语 
   * 
   * @param somebodyname 人名 
   * @return 返回相应的问候语 
   * @throws java.rmi.remoteexception 
   */ 
  public string sayhellotosomebody(string somebodyname) throws remoteexception { 
    return "你好," + somebodyname + "!"; 
  } 
}

/** 
* created by intellij idea. 
* user: leizhimin 
* date: 2008-8-7 22:03:35 
* 创建rmi注册表,启动rmi服务,并将远程对象注册到rmi注册表中。 
*/ 
public class helloserver { 
  public static void main(string args[]) { 

    try { 
      //创建一个远程对象 
      ihello rhello = new helloimpl(); 
      //本地主机上的远程对象注册表registry的实例,并指定端口为8888,这一步必不可少(java默认端口是1099),必不可缺的一步,缺少注册表创建,则无法绑定对象到远程注册表上 
      locateregistry.createregistry(8888); 

      //把远程对象注册到rmi注册服务器上,并命名为rhello 
      //绑定的url标准格式为:rmi://host:port/name(其中协议名可以省略,下面两种写法都是正确的) 
      naming.bind("rmi://localhost:8888/rhello",rhello); 
//      naming.bind("//localhost:8888/rhello",rhello); 

      system.out.println(">>>>>info:远程ihello对象绑定成功!"); 
    } catch (remoteexception e) { 
      system.out.println("创建远程对象发生异常!"); 
      e.printstacktrace(); 
    } catch (alreadyboundexception e) { 
      system.out.println("发生重复绑定对象异常!"); 
      e.printstacktrace(); 
    } catch (malformedurlexception e) { 
      system.out.println("发生url畸形异常!"); 
      e.printstacktrace(); 
    } 
  } 
}

 
/** 
* created by intellij idea. 
* user: leizhimin 
* date: 2008-8-7 22:21:07 
* 客户端测试,在客户端调用远程对象上的远程方法,并返回结果。 
*/ 
public class helloclient { 
  public static void main(string args[]){ 
    try { 
      //在rmi服务注册表中查找名称为rhello的对象,并调用其上的方法 
      ihello rhello =(ihello) naming.lookup("rmi://localhost:8888/rhello"); 
      system.out.println(rhello.helloworld()); 
      system.out.println(rhello.sayhellotosomebody("熔岩")); 
    } catch (notboundexception e) { 
      e.printstacktrace(); 
    } catch (malformedurlexception e) { 
      e.printstacktrace(); 
    } catch (remoteexception e) { 
      e.printstacktrace();  
    } 
  } 
}

运行rmi服务端程序:

 运行rmi客户端程序:

 总结:

从上面的过程来看,rmi对服务器的ip地址和端口依赖很紧密,但是在开发的时候不知道将来的服务器ip和端口如何,但是客户端程序依赖这个ip和端口。

这也是rmi的局限性之一。这个问题有两种解决途径:一是通过dns来解决,二是通过封装将ip暴露到程序代码之外。
rmi的局限性之二是rmi是java语言的远程调用,两端的程序语言必须是java实现,对于不同语言间的通讯可以考虑用web service或者公用对象请求代理体系(corba)来实现。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网