RMI

转载 2015年11月19日 09:28:14

The RMI (Remote Method Invocation) is an API that provides a mechanism to create distributed application in java. The RMI allows an object to invoke methods on an object running in another JVM.

The RMI provides remote communication between the applications using two objects stub and skeleton.

stub

The stub is an object, acts as a gateway for the client side. All the outgoing requests are routed through it. It resides at the client side and represents the remote object. When the caller invokes method on the stub object, it does the following tasks:
相当于一个大门,所有外部的请求都要经过这里,STUB定居在CLIENT SIDE,代表远程物体。当被叫醒时候:

It initiates a connection with remote Virtual Machine (JVM),
与远程虚拟机建立联系
It writes and transmits (marshals) the parameters to the remote Virtual Machine (JVM),
写和传输参数到远程的虚拟机
It waits for the result 等待结果
It reads (unmarshals) the return value or exception, and
It finally, returns the value to the caller. 返回结果

skeleton

The skeleton is an object, acts as a gateway for the server side object. All the incoming requests are routed through it. When the skeleton receives the incoming request, it does the following tasks:

It reads the parameter for the remote method
It invokes the method on the actual remote object, and
It writes and transmits (marshals) the result to the caller.

Understanding requirements for the distributed applications

If any application performs these tasks, it can be distributed application.
The application need to locate the remote method
It need to provide the communication with the remote objects, and
The application need to load the class definitions for the objects.
The RMI application have all these features, so it is called the distributed application.

Steps to write the RMI program

The is given the 6 steps to write the RMI program.

Create the remote interface 建立一个远程的接口
Provide the implementation of the remote interface提供远程接口实施
Compile the implementation class and create the stub and skeleton objects using the rmic tool
编译实施的类,用rmic建立一个STUB和框架物体。
Start the registry service by rmiregistry tool
开始注册服务用rmiregistry
Create and start the remote application
建立和开始远程应用
Create and start the client application
建立和开始远程应用

RMI Example

In this example, we have followed all the 6 steps to create and run the rmi application.

The client application need only two files, remote interface and client application.

In the rmi application, both client and server interacts with the remote interface.

The client application invokes methods on the proxy代理 object, RMI sends the request to the remote JVM. The return value is sent back to the proxy object and then to the client application.

1) create the remote interface

For creating the remote interface, extend the Remote interface and declare the RemoteException with all the methods of the remote interface.

Here, we are creating a remote interface that extends the Remote interface. There is only one method named add() and it declares RemoteException.

import java.rmi.*;  
public interface Adder extends Remote{  
public int add(int x,int y)throws RemoteException;  
}  

2) Provide the implementation of the remote interface

Now provide the implementation of the remote interface. For providing the implementation of the Remote interface, we need to

Either extend the UnicastRemoteObject class,
or use the exportObject() method of the UnicastRemoteObject class

In case, you extend the UnicastRemoteObject class, you must define a constructor that declares RemoteException.

import java.rmi.*;  
import java.rmi.server.*;  
public class AdderRemote extends UnicastRemoteObject implements Adder{  
AdderRemote()throws RemoteException{  
    super();  
}  
public int add(int x,int y){return x+y;}  
}  

3) create the stub and skeleton objects using the rmic tool.
Next step is to create stub and skeleton objects using the rmi compiler. The rmic tool invokes the RMI compiler and creates stub and skeleton objects.

rmic AdderRemote  

4) Start the registry service by the rmiregistry tool

Now start the registry service by using the rmiregistry tool. If you don’t specify the port number, it uses a default port number. In this example, we are using the port number 5000.

rmiregistry 5000  

5) Create and run the server application

Now rmi services need to be hosted in a server process. The Naming class provides methods to get and store the remote object. The Naming class provides 5 methods.

public static java.rmi.Remote lookup(java.lang.String) throws java.rmi.NotBoundException, java.net.MalformedURLException, java.rmi.RemoteException;
it returns the reference of the remote object.

public static void bind(java.lang.String, java.rmi.Remote) throws java.rmi.AlreadyBoundException, java.net.MalformedURLException, java.rmi.RemoteException; it binds the remote object with the given name.
public static void unbind(java.lang.String) throws java.rmi.RemoteException, java.rmi.NotBoundException, java.net.MalformedURLException; it destroys the remote object which is bound with the given name.
public static void rebind(java.lang.String, java.rmi.Remote) throws java.rmi.RemoteException, java.net.MalformedURLException; it binds the remote object to the new name.
public static java.lang.String[] list(java.lang.String) throws java.rmi.RemoteException, java.net.MalformedURLException; it returns an array of the names of the remote objects bound in the registry.

import java.rmi.*;  
import java.rmi.registry.*;  
public class MyServer{  
    public static void main(String args[]){  
    try{  
        Adder stub=new AdderRemote();  
        Naming.rebind("rmi://localhost:5000/sonoo",stub);  
    }catch(Exception e){System.out.println(e);}  
}  
}  

6) Create and run the client application

At the client we are getting the stub object by the lookup() method of the Naming class and invoking the method on this object. In this example, we are running the server and client applications, in the same machine so we are using localhost. If you want to access the remote object from another machine, change the localhost to the host name (or IP address) where the remote object is located.

import java.rmi.*;  
public class MyClient{  
public static void main(String args[]){  
try{  
Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/sonoo");  
System.out.println(stub.add(34,4));  
}catch(Exception e){}  
}  
}  

dubbo相关知识(六)-- java RMI框架

1、概述 在 Java 世界里,有一种技术可以实现“跨虚拟机”的调用,它就是 RMI(Remote Method Invocation,远程方法调用),通过RMI技术,某一个本地的JVM可以...
  • guchuanyun111
  • guchuanyun111
  • 2016年08月01日 16:35
  • 3147

java RMI原理详解

定义 RMI(Remote Method Invocation)为远程方法调用,是允许运行在一个Java虚拟机的对象调用运行在另一个Java虚拟机上的对象的方法。 这两个虚拟机可以是运行在相同计算机上...
  • xinghun_4
  • xinghun_4
  • 2015年05月17日 19:16
  • 7804

RMI介绍与使用

今天在这边介绍一下Java基础中的rmi使用。其实rmi有什么样的使用场景呢?它跟webservice有什么区别呢?其实webservice主要是可以跨语言实现项目间的方法调用,而rmi只是java内...
  • u014001866
  • u014001866
  • 2016年03月20日 13:38
  • 3077

java远程调用rmi入门实例

RMI是Java的一组拥护开发分布式应用程序的API。RMI使用Java语言接口定义了远程对象,它集合了Java序列化和Java远程方法协议(Java Remote Method Protocol...
  • h348592532
  • h348592532
  • 2014年10月17日 13:24
  • 2124

J2EE规范——RMI与EJB

偶然在网上看到了EJB和RMI的关系,和大家分享一下其中的奥秘 一、RMI         RMI是Java的一组拥护开发分布式应用程序的API。RMI使用Java语言接口定义了远...
  • tr1912
  • tr1912
  • 2017年01月14日 21:11
  • 574

RMI在外网不能通信的解决

前段时间写了一个全文检索服务,用的是RMI远程调用。在本机上写好后,测试ok.但一部署到服务器上就报找不到服务。在网上找了一大通: 原因如下:     RMI除了注册端口外,其通信端口是服务器...
  • wjrong_1
  • wjrong_1
  • 2015年12月12日 01:01
  • 1440

架构设计:系统间通信(8)——通信管理与RMI 上篇

在概述了数据描述格式的基本知识、IO通信模型的基本知识后。我们终于可以进入这个系列博文的重点:系统间通信管理。在这个章节我将通过对RMI的详细介绍,引出一个重要的系统间通信的管理规范RPC,并且继续讨...
  • yinwenjie
  • yinwenjie
  • 2015年10月22日 20:52
  • 10720

Java RMI 简介及其优劣势总结

今天,帮别人看UDAS配置问题时,看到采用的是RMI方式的调用,如:rmi://10.20.134.140:2299/DataServer,而且这个端口是被直接硬编码在UDAS的代码中的。既然看到了,...
  • mingtianhaiyouwo
  • mingtianhaiyouwo
  • 2016年01月13日 22:07
  • 1929

Eclipse RMI 插件

 Eclipse RMI插件下载地址是http://www.genady.net/rmi/v20/downloads.html下载及安装Eclipse 3.3 (Europa): net.genady...
  • tgeh23
  • tgeh23
  • 2008年03月14日 22:21
  • 2072

浅谈RMI - 搭建一个简单的RMI程序

RMI是Remote Method Invoke的缩写,是JDK提供的一个完善的、简单易用的远程调用框架,它要求客户端和服务器端都是Java程序。下面简述RMI的基本原理:如下图所示,RMI采用代理来...
  • xiaoyaoyulinger
  • xiaoyaoyulinger
  • 2016年05月19日 11:50
  • 2537
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:RMI
举报原因:
原因补充:

(最多只允许输入30个字)