RMI

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){}  
}  
}  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ehcache RMI 提供了一种自动化的远程方法调用(RMI)功能,它能够将 Ehcache 缓存的数据通过远程方法调用进行访问和操作。 Ehcache 是一个流行的 Java 缓存框架,用于提升应用程序的性能和响应速度。它能够在内存中缓存数据,减少对数据库或其他外部资源的频繁访问。然而,当多个应用程序运行在不同的服务器上,且它们之间需要共享和访问缓存数据时,就需要一种能够在不同服务器之间进行数据传输和方法调用的机制。 Ehcache RMI 提供了这样的机制。它通过 Java 的远程方法调用技术,将 Ehcache 缓存的数据和方法暴露给远程服务器。这样,远程服务器就能够通过网络访问 Ehcache 缓存数据,执行方法操作,而无需直接访问原始的数据库或其他资源。 Ehcache RMI 的自动化特性指的是,它能够自动处理远程方法调用的细节,如序列化和反序列化对象、传输数据、调用方法等。开发人员只需配置正确的网络地址和端口,并提供需要远程访问和操作的缓存数据和方法,Ehcache RMI 就会自动完成数据传输和方法调用的过程。 总结来说,Ehcache RMI 是一种能够自动化实现远程方法调用的机制,它利用 Java RMI 技术,通过网络访问和操作 Ehcache 缓存数据。开发人员只需进行简单的配置,Ehcache RMI 就能够自动处理远程方法调用的细节,方便实现多个应用程序之间的数据共享和访问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值