Java RMI (Java remote method call instance parsing)

Source: Internet
Author: User

In Java, remote objects are instances of classes that implement remote interfaces. remote interfaces declare each method to be called remotely. When you need to create a remote object, you can pass an interface to hide implementation details at the grassroots level. You can send messages through the interface handle.

Remote interfaces have the following features:
1) the remote interface must be public. Otherwise, if the client and remote interface are in the same package, an error is returned when you try to mount a remote object that implements the remote interface.
2) the remote interface must be extended to Java. RMI. Remote.
3) except for exceptions specific to the application, every method in the remote interface must declare java. RMI. RemoteException in its throws clause. (Or the RemoteException parent class ).

4) A remote object passed as a parameter or return value (whether directly or embedded in a local object) must be declared as a remote interface, rather than an implementation class.

For example:

1) Create the remote interface testremote. java. The source code is as follows:

Import java. RMI. Remote;
Import java. RMI. RemoteException;

Public interface testremote extends remote {
Public int add (int A, int B) throws RemoteException;
}

2) create a remote interface implementation class testremoteimpl. java. The source code is as follows:

Import java. RMI. RemoteException;
Import java. RMI. server. unicastremoteobject;

Public class testremoteimpl extends unicastremoteobject implements testremote {
Protected testremoteimpl () throws RemoteException {
Super ();
}

Public int add (int A, int B) throws RemoteException {
Return A + B;
}
}

3) Compile the server code server. java. The source code is as follows:

Import java. RMI. Naming;
Import java. RMI. Registry. locateregistry;

Public class server {
Public static void main (string [] ARGs) throws exception {
Try {
Locateregistry. createregistry (8808 );
// Create a remote object
Testremote = new testremoteimpl ();
// Bind the prize name to the object
Naming. rebind ("RMI: // localhost: 8808/Server", testremote );
System. Out. println ("RMI server is running ...... ");
} Catch (exception e ){
E. printstacktrace ();
}
}
}

4) Compile the client code client. java. The source code is as follows:

Import java. RMI. Naming;

Public class client {
Public static void main (string [] ARGs ){
Try {
String url = "RMI: // localhost: 8808/Server ";
Testremote rmiobject = (testremote) Naming. Lookup (URL );
System. Out. println ("1 + 2 =" + rmiobject. Add (1, 2 ));
} Catch (exception exc ){
System. Out. println ("error in lookup:" + exc. tostring ());
}
}
}

5) Compile the source code:

Javac server. Java

Javac client. Java

6) Create root and stem for remote object implementation:

To create a stub program and a skeleton file, run the rmic compiler with the full name of the compiled class package that contains remote object implementation.

Stub is the proxy of a remote object on the client. It passes the RMI call to the server-side skeleton (skeleton). The latter is responsible for passing the call to the actual remote method,

Enter the following information:

Rmic-d e: \ RMI testremoteimpl

Where; E: \ RMI is the location where the code is saved.

7) run the server and client programs:

Java Server

Java client

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.