Java remote method call (RMI)

Source: Internet
Author: User

Java Remote Method Invocation (RMI) is a mechanism for implementing RPC. Java RMI implementation can be divided into the following steps: 1. create a remote interface and declare a remote method; 2. create remote objects and implement remote methods; 3. the server starts the RMI Registration Service and registers remote objects. 4. the client searches for remote objects and calls remote methods. The remote interface has 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. 5. if the parameter or return value is an object, the object must implement the serial number interface Serializable. The Code is as follows: 1) create a remote interface and declare the remote method package com. server; Java code import java. rmi. remote; import java. rmi. remoteException; public interface RmiSample extends Remote {public int sum (int a, int B) throws RemoteException; public void save (Student s) throws RemoteException; public Student get () throws RemoteException ;} 2) create a remote object and implement the remote interface method. Java code package com. server; import java. rmi. remoteException; import java. rmi. server. extends; public class RmiSampleImpl extends UnicastRemoteObject implements RmiSample {/*****/private static final long serialVersionUID =-plaintext; protected RmiSampleImpl () throws RemoteException {super ();} @ Override public int sum (int a, int B) throws RemoteException {return a + B;} @ Override public void save (Student s) throws RemoteException {System. out. println ("student id is:" + s. getId (); System. out. println ("student name is:" + s. getName () ;}@ Override public Student get () throws RemoteException {Student s = new Student (); s. setId (2); s. setName ("Student 2"); return s ;}} 3) The server starts the RMI Registration Service and registers the remote object with the Java code package com. server; import java.net. malformedURLException; import java. rmi. naming; import java. rmi. remoteException; import java. rmi. registry. locateRegistry; public class RmiSampleServer {public static void main (String [] args) {try {LocateRegistry. createRegistry (8808); // LocateRegistry. createRegistry (1099); RmiSampleImpl server = new RmiSampleImpl (); Naming. rebind ("// localhost: 8808/SAMPLE-SERVER", server); // Naming. rebind ("server", server);} catch (MalformedURLException me) {System. out. println ("Malformed URL:" + me. toString ();} catch (RemoteException re) {System. out. println ("Remote Exception:" + re. toString () ;}}} 4) the client searches for remote objects and calls the remote method Java code package com. client; import java. rmi. remoteException; import java. rmi. naming; import com. server. rmiSample; import com. server. student; public class RmiSampleClient {public static void main (String [] args) {try {String url = "// localhost: 8808/SAMPLE-SERVER "; // RmiSample RmiObject = (RmiSample) Naming. lookup ("server"); RmiSample RmiObject = (RmiSample) Naming. lookup (url); System. out. println ("3 + 2 =" + RmiObject. sum (3, 2); Student s = new Student (); s. setId (1); s. setName ("Student 1"); RmiObject. save (s); Student student = RmiObject. get (); System. out. println ("student id is:" + student. getId (); System. out. println ("student name is:" + student. getName ();} catch (RemoteException rex) {System. out. println ("Error in lookup:" + rex. toString ();} catch (java.net. malformedURLException me) {System. out. println ("Malformed URL:" + me. toString ();} catch (java. rmi. notBoundException ne) {System. out. println ("NotBound:" + ne. toString ());}}}

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.