Simple use of RMI?

This is the back-end small class of the monastery. Each article is shared from

[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]

Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:

[simple use of RMI?]

Hello, I'm the 10th student of Zhengzhou branch of it Academy. I'm an honest, pure and kind java programmer.

Today, I'd like to share with you the Java task 7 on the official website of the Academy, expanding the knowledge points in thinking - the simple use of RMI?

1、 Background introduction

RPC (remote procedure call): remote procedure call, which is used for one process to call a procedure in another process, thus providing the process distribution capability.

RMI (remote method invocation): remote method invocation, which is a step forward on the basis of RPC, provides communication between distributed objects. Allows objects running on one Java virtual machine to call methods on objects running on another Java virtual machine. The two virtual machines can be running in different processes on the same computer or in different computers on the network.

2、 Knowledge analysis

General working principle of RMI:

The server side provides services. The remote methods that can be called should be exposed in the service and expressed in the form of interface. In this way, the client can call the remote methods through the service interface to realize complex business logic. On the server side, the methods provided in the interface must be implemented first, so that the client call can complete certain business logic; Then we need to generate skeleton. In skeleton, we really realize the call to business methods, complete the call process requested by customers, and return the obtained call method results to the client through the serialization mechanism for response. On the client side, the data (object) returned by the server is received through the stub, that is, the deserialization is carried out here, that is, the byte stream transmitted by the network is read, and then the reconstruction is carried out. In both skeleton and stub, the network communication is processed, such as establishing sockets and establishing network connections, so as to prepare for the actual business needs.

Problems needing attention during RMI use:

1. Data value transfer: we all know that reference types are used in Java programs Parameters (excluding basic types) are passed by reference. There is no problem when passing in the same virtual machine, because the reference of parameters corresponds to the same memory space. However, in the distributed system, since the objects no longer exist in the same memory space, the object reference of virtual machine a has no meaning to virtual machine B. at this time, I We need to change reference passing to value passing, that is, serialize the object into bytes, and then use a copy of the byte to pass between the client and the server;

2. Remote object discovery: a remote object reference is required before calling the remote object method. How to obtain the remote object reference is a key problem in RMI. It may be easier to understand if the remote object discovery is compared with the IP address discovery. In our daily use of the network, we basically locate a website through the domain name, but in fact, the network locates the website through the IP address, so a mapping process is required, Domain name system (DNS) appears for this purpose. In the domain name system, the domain name is used to find the corresponding IP address to access the corresponding server. Then the corresponding IP address is equivalent to the reference of remote objects, and DNS is equivalent to a registry (Registry). In RMI, the domain name is equivalent to the identifier of the remote object. The client accesses the registry by providing the identifier of the remote object. This identifier is similar to the URL address format: rmi://host:port/name 。

3、 Frequently asked questions

1. How to use RMI to call remote objects 2 How to use spring RMI to complete the call of remote objects

4、 Coding practice

See video for details.

5、 Extended thinking

Difference between RPC and RMI:

1. Method calling methods are different: RMI calls remote methods through the stub object on the client as a remote interface. Each remote method has a method signature. If a method is executed on the server, but no matching signature is added to the remote interface (stub), the new method cannot be called by the RMI client; The PC sends a request to the remote host through the network service protocol. The request contains a parameter set and a text value, usually in the form of "classname. Methodname". The RPC remote host searches for the matching classes and methods, executes the methods after finding them, encodes the results, and sends them back through the network protocol.

2. Different applicable languages: RMI is only used for Java; RPC is a network service protocol, independent of the operating system and language.

6、 References

7、 More discussion

1. What are the ways to register RMI services?

LocateRegistry class, Java rmi. Naming class, Java naming. The initialcontext class can register services.

2.java. rmi. What role does the naming class play?

java. rmi. The naming class is used to store and obtain references to remote objects in the remote object registry. Each method of naming class receives a string object in URL format as its parameter.

3. RMI development process?

1) Define the remote interface 2) implement the remote interface 3) prepare the server object for remote call 4) generate the stub (client agent) and skeleton (server entity) 5) find the remote object with rmiregistry 6) run and test the RMI distributed application

Ppt link video link

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>