1, RMI is the short name of the remote method call, as its names imply, it can help us to find and execute the remote object, in layman's words, the remote call is like a class on a machine, and then in the B machine call this class method.
2. EMI Terminology
Before studying the code, let's look at what code we have to write:
remote object: This interface only defines a method, and we should understand that the interface can contain both the code of the method and the definition of the method. The remote object contains the definition of each method to be exported and implements the remote interface in Java.rmi.
remote Object implementation: This is a class that implements a remote object. If you implement a remote object, you can overwrite all the methods in that object, so the implementation class for the remote object will really contain the method code that we want = export.
Remote server: This is a class that is used as a server, and it is relative to the client that is accessing the method remotely. It stores the strings and objects that are bound.
remote client: This is a class that helps us to access remote methods to help, and it is also an end user.
Stub (stub) and skeleton (skeleton)
Stubs and skeleton are generated through the rmic command, our program to be remote call, the underlying must be a socket byte transmission, transfer to the server or client peer, and then deserialize the object into the corresponding object, these network transmission process requires security, stability and so very troublesome operation , the stub resides on the client and assumes the role of the agent of the remote object's implementation, and the skeleton class helps the remote object communicate with the stub on the RMI connection. RMI client and stub exchange, stub and skeleton communication, skeleton is responsible for interacting with the server, so with stubs and skeleton we do not need to implement the details of the underlying communication, we make a remote call, You only need to manipulate the method through the interface, so that the distributed call implements the transparency of the location, that is, the remote call is just like a local call.
Server-side implementation
1. Service Interface
/** * Service Interface * Defines the set of behaviors */ Import Java.rmi.Remote; Import java.rmi.RemoteException; Public Interface extends Remote { /** * Sends a message to the client on the server and returns the "Hello World " String * / public throws remoteexception;}
2. Service implementation
/*** Service Implementation * Inherit UnicastRemoteObject*/ImportJava.rmi.server.UnicastRemoteObject;Importjava.rmi.RemoteException; Public classHelloimplextendsUnicastRemoteObjectImplementsIhello {//necessary for PublicHelloimpl ()throwsRemoteException {Super(); } @Override Publicstring SayHello (String message) {System.out.println ("================client Message" +message); return"Hello world!"; }}
3. Start the RMI server and expose the service
/***rmi Server * *, create a service * *, start the server * *, registration Services*/ImportJava.rmi.registry.*;Importjava.rmi.*;ImportJava.rmi.server.*; Public classHelloServer { Public Static voidMain (string[] args)throwsException {//1Ihello Hello =NewHelloimpl (); //2 Start the server to start a registry and bind the registry to a port (default port 1099)Locateregistry.createregistry (8888); //3 The standard format for registering a service to a registry binding is: RMI://Host:port/name (where the protocol name can be omitted, the following two formulations are correct)Naming.bind ("Rmi://localhost:8888/hello", hello); System.out.println ("======== server started."); }}
Client implementation
1. Service Interface
/** * Service Interface * Defines the set of behaviors */ Import Java.rmi.Remote; Import java.rmi.RemoteException; Public Interface extends Remote { /** * Sends a message to the client on the server and returns the "Hello World " String * / public throws remoteexception;}
2. Client implementation
/*** 1, who is the service realization? Server * 2, how to get service lookup * 3, how to invoke the service interface*/Importjava.rmi.*; Public classhelloclient { Public Static voidMain (string[] args)throwsException {//1. Find ServiceIhello Hello = (Ihello) naming.lookup ("Rmi://localhost:8888/hello"); System.out.println (Naming.lookup ("Rmi://localhost:8888/hello")); //2. CallSystem.out.println (Hello.sayhello ("haha")); }}
RMI (Remote Method invocation) Concept recovery