转载 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.


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. 返回结果


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
Start the registry service by rmiregistry tool
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{  
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[]){  
        Adder stub=new AdderRemote();  
    }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[]){  
Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/sonoo");  
}catch(Exception e){}  


  • 2016年07月15日 16:01
  • 4.12MB
  • 下载


  • 2015年08月07日 17:28
  • 2KB
  • 下载

JMX之通过RMI方式连接JMX Server



  • 2017年06月22日 21:42
  • 1.19MB
  • 下载


  • 2015年12月06日 22:54
  • 5KB
  • 下载

Java RMI 简介及其优劣势总结


java RMI学习

  • 2014年11月01日 22:04
  • 232KB
  • 下载

Java RMI实例

  • 2013年08月08日 17:09
  • 369KB
  • 下载

Java RMI远程接口调用介绍与使用

相关文章: RMI(Remote Method Invocation)原理浅析 Java RMI 实现代码动态下载 stub 和 skeleton 的讲解,自己实现一个stub和skele...


  • 2013年10月10日 13:47
  • 14KB
  • 下载