Java RMI (Remote method invocation)

Source: Internet
Author: User

RMI (remote method invocation) The Chinese name is a remote methods call that can be used for distributed computing.
Here do not go into the details of RMI, this blog main Apple told RMI actual combat and what needs to be noted, if you want to see the detailed introduction, please see: Baidu Encyclopedia RMI

RMI is divided into server and client

Server: Create service side:
LocateRegistry.createRegistry(端口);Naming.rebind("rmi://IP地址:端口/RMI服务名称",RMI服务对象);//rebind也可以用bind替代

Example:

LocateRegistry.createRegistry(10010);Naming.rebind("rmi://127.0.0.1:10010/Test", new TestServer());
The class definition rules for RMI service objects: 1. The UnicastRemoteObject2 must be inherited. The constructor method must declare the throw RemoteException3. A custom business interface must be implemented and remote cannot be implemented directly, and the interface to be invoked by the client must be extracted to the interface 4. The client is calling The return value of the interface method must implement Serializable, Although the RMI service does not receive an error while the RMI client calls this method, the RMI service object's interface definition rules are: 1. The interface must inherit Remote2. Interface methods must declare an example of the throw remoteexception:
 Public  interface testserverinterface extends Remote {String getString ()throwsRemoteException; Student getstudent (String name)throwsRemoteException;} Public  class testserver extends unicastremoteobject  implements  Testserverinterface {     Public TestServer()throwsremoteexception {Super(); }@Override     PublicStringgetString()throwsremoteexception {returnSystem.currenttimemillis () +": This method has been called by the client"; }@Override     PublicStudentgetstudent(String name)throwsremoteexception {return NewStudent (Name+system.currenttimemillis ()); }}
Client: Create and use client:
server= (RMI服务对象的接口) Naming.lookup("rmi://IP地址:端口/RMI服务名称");返回值 result=server.你要调用的方法(可能需要传入的参数);

Example:

TestServerInterface serverInterface= (TestServerInterface) Naming.lookup("rmi://localhost:10010/Test");System.out.println(">>>" + serverInterface.getString());System.out.println(">>>" + serverInterface.getStudent("name1"));
Interface definition rules for RMI service objects:

1. Can be inconsistent with the interface method defined by the service side, there can be no side of the service side, the server does not have this side can have, but the call will be error
2. No need to inherit from remote, but it doesn't make sense to inherit.
3. The interface method does not need to declare throws the remoteexception, the declaration throws also the line but meaningless
4. Generally consistent with the service side
Example:

publicinterface TestServerInterface {    String getString();    Student getStudent();}
Special note: First, the client uses the "RMI service object Interface" and the service End with "RMI service object Interface" is two unused classes, because the client and the server is generally not on a machine. Second, if the value returned by the client is a custom type, The return value that is defined by the client has the following rule: 1. Serializable2 must be implemented. If the server does not have a serialversionuid defined, the client side must also have no defined serialVersionUID3. If the server has a defined serialversionuid, the customer The end side must also define the Serialversionuid and the Serialversionuid value is consistent with the server Serialversionuid value, otherwise the serialization and version number inconsistency exception is reported example 1: Server return Custom type:
 Public  class Student implements Serializable {    Private Static Final LongSerialversionuid =1LPrivateString name; Public Student(String name) { This. name = name; } PublicStringGetName() {returnName } PublicStringtoString(){returnName }}
The client accepts the custom type:
 Public  class Student implements Serializable {    Private Static Final LongSerialversionuid =1LPrivateString name; Public Student(String name) { This. name = name; } PublicStringGetName() {returnName } PublicStringtoString(){returnName }}
Example 2: The server returns a custom type:
publicclass Student implements Serializable {    private String name;    publicStudent(String name) {        this.name = name;    }    publicgetName() {        return name;    }    publictoString(){        return name;    }}
The client accepts the custom type:
publicclass Student implements Serializable {    private String name;    publicStudent(String name) {        this.name = name;    }    publicgetName() {        return name;    }    publictoString(){        return name;    }}
Full code:

Project diagram:

Service side:
Package Test;import Java.io.serializable;import Java.rmi.naming;import java.rmi.remote;import Java.rmi.remoteexception;import Java.rmi.registry.locateregistry;import java.rmi.Server. UnicastRemoteObject; Public  class rmiservermain {     Public Static voidMain (string[] args) throws Exception {Locateregistry.createregistry (10010); Naming.bind ("Rmi://127.0.0.1:10010/test",NewTestServer ()); }} interface testserverinterface extends Remote {String getString () throws remoteexception; Student getstudent (String name) throws RemoteException;} class testserver extends unicastremoteobject  implements  Testserverinterface {    Private Static Final LongSerialversionuid =1L PublicTestServer () throws RemoteException {Super(); } @Override PublicString getString () throws RemoteException {returnSystem.currenttimemillis () +": This method has been called by the client"; } @Override PublicStudent getstudent (String name) throws RemoteException {return NewStudent (name + System.currenttimemillis ()); }} class Student implements Serializable {    Private Static Final LongSerialversionuid = theLPrivateString name; PublicStudent (String name) { This. name = name; } PublicString GetName () {returnName } PublicString toString () {returnGetClass (). Getsimplename () +": Name="+ Name; }}
Client
 PackageTestImportjava.io.Serializable;Importjava.rmi.Naming; Public  class rmiclientmain {     Public Static void Main(string[] args)throwsException {Testserverinterface serverinterface = (testserverinterface) naming.lookup ("Rmi://127.0.0.1:10010/test"); System.out.println (">>>"+ serverinterface.getstring ()); System.out.println (">>>"+ Serverinterface.getstudent ("Stuname")); }}/** * Client's service interface, not must inherit remote, and the method does not have to declare throw remoteexception<br> * But the package name, class name must be exactly the same as the server. <br> * method can be deleted, can be indiscriminate, but the method of scrambling you just do not call on the line, to invoke the error. <br> * methods have different structure, such as return type, parameter type. * * @author Tang * * * *Interface Testserverinterface {String getString (); Student getstudent (String name);} Class Student implements Serializable {/** * Serialversionuid variables must be consistent with the service side. <br> * If the server does not define this variable, the client must also not define this variable <br> * If the server has defined this variable, the client must also define this variable, and the value of the variable will be the same * /    Private Static Final LongSerialversionuid = theLPrivateString name; Public Student(String name) { This. name = name; } PublicStringGetName() {returnName } PublicStringtoString() {returnGetClass (). Getsimplename () +": Name="+ Name; }}

Java RMI (Remote method invocation)

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.