How to implement a remote method call in Java

zhaozj2021-02-11  196

How to implement remote method calls in Java, Guo Town, Zhao Wei

1. Remote Method Invocation, RMI in Java is a distributed object package introduced by Java1.1, which simplifies communication between Java applications on multiple machines. Compared to CORBA, RMI features are weaker and can only be used for Java systems.

Second, implement a simple RMI to use RMI, you must construct four major classes: local interfaces, RMI customers, remote object implementations, and RMI servers. The RMI server generates an instance of the remote object implementation, and register it with a special URL. The RMI customer finds an object on the remote server. If you find it to convert it cost the interface type, then use it like a local object. Below is a simple RMI example, the remote object only returns a message string. To make this example more valuable, what we need to do is to improve the remote object implementation class.

1. Rem.java of the remote object (Rem.java) This class is just an interface instead of implementation, the RMI client can use it directly, the RMI server must implement it through a remote object, and register it with a URL. An example. Import java.rmi. *;

Public Interface Rem Extends Remote {Public String GetMessage () THROWS RemoteException;

The local interface (REM) must be public, otherwise the client will errors when loading a remote object that implements the interface. In addition, it must inherit from java.rmi.Remote, each method in the interface must throw a remote exception java.rmi.RemoteException.

2.RMI customer class (Remclient.java)

RMI customers use Naming.lookup to find objects on the specified remote host, if they are found to convert it cost the ground REM type, then use it like a local object. Unlike CORBA, the RMI customer must know the URL of the remote service host, which can be specified by RMI: // Host / Path or RMI: // Host: Port / Path, if the port number is omitted, 1099. Naming.lookup may generate three exceptions: RemoteException, NOTBOUNDEXCEPTION, MALFORMEDURLEXCEPTION, three unusual needs to capture. RemoteException, Naming and NotboundException are defined in java.rmi. *, MalforMedurLexception is defined in java.net. *. In addition, the client will pass a stringable object SERIALIZABLE to the remote object, so it should also be entered in Java.IO. *.

Import java.rmi. *;

Import java.net. *;

Import java.io. *;

Public class remclient {

Public static void main (String [] args) {

Try {

String host = (args.length> 0)? Args [0]: "localhost"; file: // read remote host name from the command line

File: // Find objects on the remote host through the URL and convert it to the local interface REM type

Rem Remobject = (remote) Naming.lookup ("RMI: //" Host "/ remote");

System.out.println (Remobject.getMessage ()); File: // Method for calling the remote object} catch (remoteException re) {system.out.println ("RemoteExce:" Re);

} catch (notboundexception nbe) {system.out.println ("notboundexce:" Nbe);

} catch (MalformedurLexception MFE) {system.out.println ("MalforMedurLexce:" MFE);

}}}

3. Remote object implementation class (Remimpl.java)

This class really implements a remote object called by RMI client, which must inherit from UnicastRemoteObject, and its constructor should throw RemoteException exception.

Import java.rmi. *;

Import java.rmi.server.UnicastRemoteObject;

Public Class Remimum Extends UnicastRemoteObject IMPLEments Rem {

Public Remimpl () throws remoteException {} file: // Constructor throws RemoteException exception

Public string getMessage () throws remoteException {

Return ("Here Is A Remote Message.");}} file: // Returns a message string to the RMI customer

4.RMI server class (Remserver.java)

This class creates an instance of the remote object implementation Remimpl, then register it with a specific URL, so-called registration is to bind the RemImpl instance to the URL via naming.bind or naming.rebind.

Import java.rmi. *;

Import java.net. *;

Public class remserver {

Public static void main (String [] args) {

Try {

Remimpl localobject = new remimpl (); file: / / Generate an instance of remote object implementation

Naming.Rebind ("RMI: /// Rem", localobject); file: // Binds the remote object instance to RMI: /// REM

(RemoteException Re) {System.out.println ("RemoteException:" Re);

} catch (MalformedurLexception MFE) {system.out.println ("MalforMedurLexce:" MFE);

}}}

Third, compile and run 1. Compile RMI customers and servers, which will automatically compile local interfaces and remote objects of remote objects.

Javac transclient.java file: // Automatically compile the local interface of the remote object Rem.java

Javac Remserver.java File: // Automatic Compile Remote Object Remimpl.java

2. Generate customer undertaking modules and server frameworks

RMIC Remimpl

This will construct radimpl_stub.class and remimpl_skeleton.class. Copy Rem.class, RemClient.class and Remimpl_stub.class to the RMI client, copy Rem.class, Remimpl.class, RemServer.class, and Remimpl_skeleton.class to the RMI server. 3. Start RMI registration

RMIREGISTRY

File: // Execute it on the server. No matter how many remote objects, this operation only needs to be done.

4. Run

Java Remserver.class

File: // Start the RMI server (executed on the server)

Java Remclient.class

File: // Start the RMI customer, will output "Here Is A Remote Message."

转载请注明原文地址:https://www.9cbs.com/read-4518.html

New Post(0)