Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use RMI in Java

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to use RMI in Java". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

RMI introduction

RMI (Remote Method Invocation) model is a kind of distributed object application. Using RMI technology, an object in one JVM can call an object method in another JVM and get the result of the call. The other JVM here can be on the same computer or on a remote computer. Therefore, RMI means that you need a Server side and a Client side.

The Server side usually creates an object and makes it remotely accessible.

This object is called a remote object.

The Server side needs to register that this object can be accessed remotely by Client.

The Client side calls the methods on the objects that can be accessed remotely, and the Client side can communicate with the Server side and transmit information to each other.

At this point, it is found that it is very convenient to use RMI to build a distributed application. Like RPC, it can communicate with each other between distributed applications, and even is very similar to the current micro-service idea.

How RMI works

As the saying goes, before you start writing RMI code, it is necessary to understand how RMI works and how the Client side communicates with the Server side in RMI.

The following figure can help us understand the workflow of RMI.

There is a thing called Stub on the Client side, which is sometimes called a stub. It is the proxy object of RMI Client. The main function of Stub is to construct a block of information when requesting a remote method, and the RMI protocol will send this block to the Server side.

This block of information consists of several parts:

Remote object identifier.

The method description of the call.

Parameter values after marshalling (object serialization is used in the RMI protocol).

Since there is a Stub on the Client side that can construct a block of information to send to the Server side, then the Server side must have an object that receives this information quickly, called Skeleton.

Its main work is as follows:

Parse the calling object identifier and method description in the information fast, and call the specific object method on the Server side.

Gets the return value or outlier of the call.

The return value is grouped and returned to the client Stub.

At this point, the result of a call from the Client side to the Server side can be obtained.

RMI development

Through the above introduction, we know the concept of RMI and how RMI works, and the following describes the development process of RMI.

This will be demonstrated through a scenario, assuming that the Client side needs to query user information, and the user information exists on the Server side, so the RMI protocol interface is opened on the Server side for the client to call the query.

RMI Server

The main purpose of the Server side is to build a class User that can be transferred and a class UserService that can be accessed remotely. At the same time, this object should be registered to RMI and open to the client.

Define the server interface (you need to inherit the Remote class, and the method needs to throw RemoteException).

The User object is defined in step 3.

Implement the server interface (you need to inherit the UnicastRemoteObject class and implement the defined interface).

Define the object to be transferred, which needs to implement the Serializable interface.

Classes that need to be transferred must implement the serialization interface, otherwise an error will be reported during the transfer. A simple tutorial on how to generate serialVersionUID in IDEA is attached at the end of the article. The gynecological hospital in Zhengzhou is a good www.qudouwy.com.

Register (rmiregistry) the remote object and start the server program.

The server binds the UserService object as the object for remote access, and the port is set to 1900 at startup.

Define the object to be transferred, which needs to implement the Serializable interface.

Classes that need to be transferred must implement the serialization interface, otherwise an error will be reported during the transfer. A simple tutorial on how to generate serialVersionUID in IDEA is attached at the end of the article.

Register (rmiregistry) the remote object and start the server program.

The server binds the UserService object as the object for remote access, and the port is set to 1900 at startup.

RMI Client

Compared with the Server side, the Client side is much simpler. Classes that are remotely accessible and need to be transferred are introduced directly, and a call can be made through the port and the address bound to the Server side.

This is the end of the content of "how to use RMI in Java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report