How to implement remote method calling in Java

Source: Internet
Author: User

 

I,Java remote method call

Remote Method Invocation (RMI) is a distributed object software package introduced by Java1.1. Its appearance simplifies the communication between Java applications on multiple machines. Compared with CORBA, RMI is weak and can only be used in Java systems.

2. Implement a simple RMI
To use RMI, four main classes must be constructed: local interfaces of remote objects, RMI customers, remote object implementations, and RMI servers. The RMI server generates an instance for remote object implementation and registers it with a special URL. the RMI client searches for the object on the remote server and converts it to the local interface type if it finds it, then use it like a local object. The following is a simple RMI example. A remote object returns only one message string. To make this example more valuable, we need to improve the remote object implementation class.

1. Local interface class of remote object (Rem. java)

This class is just an interface, rather than an implementation. The RMI client can use it directly. The RMI server must implement it through a remote object and register an instance with a URL.

Import java. rmi .*;
Public interface Rem extends Remote {public String getMessage () throws RemoteException ;}

The Local interface (Rem) must be public; otherwise, an error occurs when the client loads a remote object that implements the interface. In addition, it must inherit from java. rmi. Remote. Every method in the interface must throw a Remote exception java. rmi. RemoteException.

2. RMI customer class (RemClient. java)

RMI customers use Naming. lookup to search for objects on the specified remote host. If they find the objects, convert them to the Rem type of the Local interface and use them like a local object. The difference with CORBA is that RMI customers must know the URL of the remote service host. This URL can be specified through rmi: // host/path or rmi: // host: port/path, if the port number is omitted, 1099 is used. Naming. lookup may produce three exceptions: RemoteException, NotBoundException, and MalformedURLException. RemoteException, Naming, and NotBoundException are defined in java. rmi. *, and MalformedURLException is defined in java.net. In addition, the client will pass the Serializable object to the remote object, so you should also enter java. io. * In the program .*.
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"; // read the remote host name from the command line
// Search for the object on the remote host through the URL and convert it to the local interface Rem type
Rem remObject = (Rem) Naming. lookup ("rmi: //" + host + "/Rem ");
System. out. println (remObject. getMessage (); // call the remote object method.
} Catch (RemoteException re) {System. out. println ("RemoteException:" + re );
} Catch (NotBoundException nbe) {System. out. println ("NotBoundException:" + nbe );
} Catch (MalformedURLException mfe) {System. out. println ("MalformedURLException:" + mfe );
}}}
3. Remote Object implementation class (RemImpl. java)
This class truly implements the remote object called by RMI. It must be inherited from UnicastRemoteObject, and its constructor should throw a RemoteException.
Import java. rmi .*;
Import java. rmi. server. UnicastRemoteObject;
Public class RemImpl extends UnicastRemoteObject implements Rem {
Public RemImpl () throws RemoteException {} // The constructor throws a RemoteException
Public String getMessage () throws RemoteException {
Return ("Here is a remote message. ") ;}} // returns a message string to the RMI customer. 4. RMI server class (RemServer. java) This class creates a remote object to implement a RemImpl instance, and then registers it with a specific URL. The so-called registration is through Naming. bind or Naming. rebind to bind the RemImpl instance to the URL. Import java. rmi. *; import java.net. *; public class RemServer {public static void main (String [] args) {try {RemImpl localObject = new RemImpl (); // generate an instance Naming for remote object implementation. rebind ("rmi: // Rem", localObject); // bind a remote object instance to rmi: // Rem} catch (RemoteException re) {System. out. println ("RemoteException:" + re);} catch (MalformedURLException mfe) {System. out. println ("MalformedURLException:" + mfe );}}}

Iii. Compile and run
Compile the RMI client and server. This will automatically compile the local interface and remote object implementation of remote objects.
Javac RemClient. java // automatically compile the local interface Rem. java of remote objects
Javac RemServer. java // automatically compile remote objects to implement RemImpl. java
Generate customer acceptance module and Server framework
Rmic RemImpl
This constructs RemImpl_Stub.class and RemImpl_Skeleton.class. Copy Rem. class, RemClient. class, and RemImpl_Stub.class to the RMI client, and copy Rem. class, RemImpl. class, RemServer. class, and RemImpl_Skeleton.class to the RMI server.
Start RMI Registration
Rmiregistry
// Run the command on the server. No matter how many remote objects exist, this operation only needs to be performed once.
Run java RemServer. class
// Start the RMI server (executed on the server)
Java RemClient. class
// Start the RMI customer and output "Here is a remote message ."

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.