Let’s first take a look at the definition of Java RMI:
RMI (Remote Method Invocation, remote method invocation) is implemented in JDK1.2 using Java. It Greatly enhances Java's ability to develop distributed applications. As a popular network development language, Java's huge power is reflected in its powerful ability to develop distributed network applications, and RMI is one of the core solutions for developing 100% pure Java network distributed application systems. In fact it can be considered as the Java version of RPC. But traditional RPC cannot be well applied to distributed object systems. Java RMI supports communication between program-level objects stored in different address spaces to achieve seamless remote calls between remote objects.
RMI remote calling steps
Interaction diagram of RMI:
RMI consists of 3 parts, the first one is rmiregistry (JDK Provides a program that can be run independently (in the bin directory), the second is a server-side program that provides remote objects to the outside world, and the third is a client-side program that wants to call methods of remote objects.
First, start the rmiregistry service. When starting, you can specify the port that the service listens on, or you can use the default port (1099).
Secondly, the server first instantiates an implementation class that provides services locally, and then uses the bind or rebind method of Naming/Context/Registry (Registry used in the example below) and other classes provided by RMI to instantiate the implementation just now The class is registered with rmiregistry and exposed to the outside world with a name.
Finally, the client obtains the implementation class from RMIService through the local interface and a known name (that is, the name exposed by rmiregistry) and then uses the lookup method of Naming/Context/Registry and other classes provided by RMI. In this way, although there is no implementation class of this class locally, all the methods are in the interface, and the method of the object can be called remotely.
The specific communication process between the stub and the backbone network:
The method call is from the client object through the stub and the remote reference layer (Remote Reference Layer) and transport layer (Transport Layer) downward, passed to the host, and then again through the transport layer, upward through the remote call layer and backbone network (Skeleton), to reach the server object.
The stub acts as a proxy for the remote server object, making the object available for activation by clients.
The remote reference layer handles semantics, manages communication of single or multiple objects, and determines whether calls should be sent to one server or multiple.
The transport layer manages the actual connection and keeps track of remote objects that can accept method calls.
The backbone network completes the actual method call to the server object and obtains the return value.
The return value is passed back to the client through the remote reference layer and the server-side transport layer, and then returned upward through the transport layer and remote call layer. Finally, the stub gets the return value.
JAVA RMI simple example
This example is the addition and subtraction method of the client calling the server-side remote object. The specific steps are:
1. Define a remote interface
import java.rmi.Remote; import java.rmi.RemoteException; /** * 必須繼承Remote接口。 * 所有參數(shù)和返回類(lèi)型必須序列化(因?yàn)橐W(wǎng)絡(luò)傳輸)。 * 任意遠(yuǎn)程對(duì)象都必須實(shí)現(xiàn)此接口。 * 只有遠(yuǎn)程接口中指定的方法可以被調(diào)用。 */ public interface IRemoteMath extends Remote { // 所有方法必須拋出RemoteException public double add(double a, double b) throws RemoteException; public double subtract(double a, double b) throws RemoteException; }
(Learning video sharing: java video tutorial)
2. Remote interface implementation class
import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import remote.IRemoteMath; /** * 服務(wù)器端實(shí)現(xiàn)遠(yuǎn)程接口。 * 必須繼承UnicastRemoteObject,以允許JVM創(chuàng)建遠(yuǎn)程的存根/代理。 */ public class RemoteMath extends UnicastRemoteObject implements IRemoteMath { private int numberOfComputations; protected RemoteMath() throws RemoteException { numberOfComputations = 0; } @Override public double add(double a, double b) throws RemoteException { numberOfComputations++; System.out.println("Number of computations performed so far = " + numberOfComputations); return (a+b); } @Override public double subtract(double a, double b) throws RemoteException { numberOfComputations++; System.out.println("Number of computations performed so far = " + numberOfComputations); return (a-b); } }
3. Server side
import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import remote.IRemoteMath; /** * 創(chuàng)建RemoteMath類(lèi)的實(shí)例并在rmiregistry中注冊(cè)。 */ public class RMIServer { public static void main(String[] args) { try { // 注冊(cè)遠(yuǎn)程對(duì)象,向客戶(hù)端提供遠(yuǎn)程對(duì)象服務(wù)。 // 遠(yuǎn)程對(duì)象是在遠(yuǎn)程服務(wù)上創(chuàng)建的,你無(wú)法確切地知道遠(yuǎn)程服務(wù)器上的對(duì)象的名稱(chēng), // 但是,將遠(yuǎn)程對(duì)象注冊(cè)到RMI Registry之后, // 客戶(hù)端就可以通過(guò)RMI Registry請(qǐng)求到該遠(yuǎn)程服務(wù)對(duì)象的stub, // 利用stub代理就可以訪問(wèn)遠(yuǎn)程服務(wù)對(duì)象了。 IRemoteMath remoteMath = new RemoteMath(); LocateRegistry.createRegistry(1099); Registry registry = LocateRegistry.getRegistry(); registry.bind("Compute", remoteMath); System.out.println("Math server ready"); // 如果不想再讓該對(duì)象被繼續(xù)調(diào)用,使用下面一行 // UnicastRemoteObject.unexportObject(remoteMath, false); } catch (Exception e) { e.printStackTrace(); } } }
4. Client The result of end
import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import remote.IRemoteMath; public class MathClient { public static void main(String[] args) { try { // 如果RMI Registry就在本地機(jī)器上,URL就是:rmi://localhost:1099/hello // 否則,URL就是:rmi://RMIService_IP:1099/hello Registry registry = LocateRegistry.getRegistry("localhost"); // 從Registry中檢索遠(yuǎn)程對(duì)象的存根/代理 IRemoteMath remoteMath = (IRemoteMath)registry.lookup("Compute"); // 調(diào)用遠(yuǎn)程對(duì)象的方法 double addResult = remoteMath.add(5.0, 3.0); System.out.println("5.0 + 3.0 = " + addResult); double subResult = remoteMath.subtract(5.0, 3.0); System.out.println("5.0 - 3.0 = " + subResult); }catch(Exception e) { e.printStackTrace(); } } }
is as follows:
server end
https://blog.csdn.net/xinghun_4/article/details/45787549
Related recommendations:The above is the detailed content of Analysis of Java RMI remote calling steps. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
