Today’s sharing started, please give us more advice ~

Java RMI: Java Remote Method Invocation (RMI) is an application programming interface (API) for Remote procedure Invocation in the Java programming language. It enables programs running on the client to invoke objects on the remote server. The remote method invocation feature enables Java programmers to distribute operations across a network environment. The whole purpose of RMI is to simplify the use of remote interface objects as much as possible.

We know that Remote Procedure Calls (RPC) can be used by one process to Call procedures in another process (most likely on another Remote host), thus providing the ability to distribute procedures. Java’s RMI takes RPC a step further by providing communication between distributed objects.

RMI (Remote Method Invocation) is a Method that allows an object running on one Java VIRTUAL machine to call an object running on another Java Virtual machine.

The two virtual machines can be running in different processes on the same machine or on different machines on the network.

【 JavaRMI 】

First, the working principle

RMI enables a Java program to call methods on Java objects on another computer in the network as if they were being called on the same machine. In plain English: machine A has A class on top of it, and machine B calls the methods in this class through remote calls.

RMI, the Remote Method Invocation that is the backbone of Enterprise JavaBeans, is a convenient way to build distributed Java applications. RMI is very easy to use, but it is very powerful.

The foundation of RMI is the interface, and the RMI architecture is based on an important principle: defining the interface is separate from defining the concrete implementation of the interface. Let’s set up a simple remote computing service and a client program to use it through a concrete example.

Ii. RMI includes:

  1. Interface definition for the remote service
  2. A concrete implementation of the remote service interface
  3. Stub and Skeleton files
  4. A server running remote services
  5. An RMI naming service that allows clients to discover the remote service
  6. Provider of class files (an HTTP or FTP server)
  7. A client program that requires this remote service

What is RMI used for?

The purpose of RMI is to provide services for remote communication between distributed Java applications, providing distributed services.

At present, it is mainly used in various J2EE project frameworks, such as Spring and EJB (both Spring and EJB encapsulate RMI technology).

Implementing RMI in Spring:

(1) Define service interfaces on the server side, and define specific classes to implement these interfaces;

(2) on the server side using org. Springframework. Remoting. Rmi. RmiServiceExporter class registration service;

(3) on the client side using org. Springframework. Remoting. Rmi. RmiProxyFactoryBean to achieve the function of remote service agent;

(4) Define classes on the client that access the same service interfaces as those on the server

4. Limitations of RMI?

RMI currently uses Java Remote Messaging Protocol (JRMP) for communication. JRMP is a protocol specifically designed for Java remote objects, and because JRMP is specifically designed for Java objects, RMI has insufficient support for applications developed in non-Java languages. Cannot communicate with objects written in a language other than Java (meaning only remote calls to code that are Java programs on both the client and server sides are supported).

V. Limitations of RMI?

Since both the client and server are written in Java, the only requirement for platform compatibility is that both are running on version-compatible Java virtual machines.

RMI calls the parameters and return values of the remote method

When calling a method on a remote object, the client can pass the object as a parameter in addition to the data of the primitive type, and the corresponding return value, which can return the primitive type or object, is implemented through Java’s object serialization technology. (In other words: parameters or return values must implement the Serializable interface if they are objects)

The basic model of RMI applications

RMI architecture

Stub/Skeleton layer: client side Stub and server side frame;

Remote Reference layer: Handles remote reference behavior

Transport layer: Establishment and management of connections, and tracing of remote objects

RMI classes and interfaces (classes needed to complete a simple RMI).

(I) Remote interface: it is a marked interface that does not define methods

Public interface Remote{}

In RMI, the remote interface declares a set of methods that can be invoked from a remote Java virtual machine. The remote interface meets the following requirements:

1. The Remote interface must extend the Java.rmi.Remote interface directly or indirectly and must be declared public unless the client is in the same package as the Remote interface

Methods declared in remote interfaces must include RemoteException (or one of its superclasses, IOExcepion or Exception) in addition to throwing an application-specific Exception

3. In a remote method declaration, a remote object declared as a parameter or return value must be declared as a remote interface, not the implementation class of that interface.

(2) The RemoteObject abstract class implements Remote and Serializable interfaces. It and its subclasses provide RMI server functions.

The LocateRegistry Final () class is used to get a reference to the bootstrap remote object registration server program for a particular host (that is, to create stubs), or to create a remote object registration service program that can receive calls on a particular port.

Server-side: Provides remote object services to other clients

SomeService servcie =… ; // Remote object services

  • Registry Registry = LocateRegisty. GetRegistry (); //Registry is an interface that inherits Remote. This method returns the local host’s reference to the Remote object Registry on the default Registry port 1099.

  • GetRegistry (int Port) returns the local host’s reference to the remote object Registry on the specified port;

  • GetRegistry (String Host) returns a reference to the remote object Registry on default Registry port 1099 by the specified host;

  • GetRegistry (String Host, int Port) returns a reference to the remote object Registry on the specified host and port

  • Registry. Bind (” I serve “, the service). // bind(String name,Remote obj) to bind a Remote reference to the name specified in this registry. Name: name associated with the remote reference obj: reference to a remote object (usually a stub)

  • Unbind (String name) Unbind the name specified in the registry.

  • Rebind (String name,Remote obj) rebind, replace if name already exists but Remote is different, discard the existing binding if Remote is the same

  • Lookup (String name) Returns the Remote reference in the registry bound to name, which returns Remote

  • String[] list() returns an array of names bound in this registry. This array will contain a snapshot of the names bound in this registry when this method is called.

Client side: provides the corresponding service request to the server.

Registry Registry = LocateRegisty. GetRegistry ();

SomeService servcie. = (SomeService) registry lookup (” I serve “);

Servcie.requestService();

Naming and Registry are similar.

Client:

Server side:

(5) RMISecurityManager class

In RMI referencing applications, if the security manager is not set, stubs and classes can only be loaded from the local classpath, which ensures that the application is protected from code downloaded by remote method calls

The following code must be executed to install RMISecurityManager before downloading the code from the remote host:

System. SetSecurityManager (new RMISecurityManager ());

X. Demo development

To write a demo, we split it into two parts: server-side code and client-side code. Client-side code is designed to use server-side code. Of course this code is very simple, just for illustration, practical use will make it more complicated.

(1) Our purpose

Create a Server-side Java Project that contains the remote code, define the interface, define the interface implementation, and then create a client-side Java Project that uses the methods in the remote service through RMI.

(ii) Our code structure

(3) Remote service code

  1. Interface definition for the remote service

The first step is to create and compile the Java code for the service interface. This interface defines all provide the function of the remote service, it’s source program: UserManagerInterface. Java

Interfaces must inherit from Remote classes, with each defined method throwing a RemoteException object.

  1. The concrete implementation of the interface

The second step is to implement the above interface:

UserManagerImp.java

3. Define a bean that implements the Implements Serializable interface. Serializable objects that can be transferred between client and server.

Account.java

  1. Define the main program entry on the server side.

Entry.java

(4) Client code

  • Export the Account class and interface UserManagerInterface on the Server as a JAR package named rmiserverInterface.jar. Import to client.
  • Project — Export — Java — jar file — next — Select Account class and interface UserManagerInterface — name it: rmiserverInterface.jar

  • Create a New Java Project, import the JAR package, and write the client code.
  • code

ClientEntry.java

  • Run the server-side code first, then the client code, and the results are displayed. The client can run multiple times, fetching the server-side object each time. If you want to run the client code again, you need to change the port number. If you do not change the port number, it shows that the port number is occupied.

Today’s share has ended, please forgive and give advice!