Objective:
Through this article, we can deepen the understanding of Java RMI, know how it works, how to use and so on. Also in order to deepen my own understanding, so the collation of the written. The shortcomings, also hope to point out.
Concept Explanation:
RMI (remotemethodinvocation): A remote method call, as the name implies, invokes a method of a non-local object in a remote way and returns the result. Using remote calls often solves local computational bottlenecks, such as distributed accounting, a recent fire-fighting alpha Dog Man-machine war, which is said to use thousands of CPUs.
JRMP (Java Remote Method Protocol):Java Remoting Protocol, which is the protocol that completes the Java-to-Java remote invocation, based on the TCP protocol.
stub and skeleton: These two concepts will be used below, explained here, skeleton is placed on the server side of the agent, it knows where the real object. A stub is a proxy placed on the client, which records the lookup and invocation of skeleton information. Understood as a remote object reference.
Easy to confuse concepts:
The difference between a remote method call and a remote procedure call: The remote method call is unique to Java and is based on the JRMP object Stream protocol implementation, which supports the transport of Java serialization objects. Remote procedure calls are based on socket technology and cannot transmit Java objects, and the socket sockets protocol supports multiple languages. They are all transmitted based on the TCP protocol. The remote method call transmits the Java serialization object and the base data type, and the remote procedure call does not support the transport object.
RMI Call Model:
From a macro perspective, you want to make a remote call to do two things, 1, the server registers a remote object in the local object registry that can be called. 2, the client requests a reference to the remote object to the remote object registry.
RMI implementations in Java:
Let's start with an example of how RMI is used in Java, and then analyze how the source code is implemented according to the codes.
1, first create a remote object interface, inherit from remote (later in the source of the analysis why this interface)
Package Remote.test;import java.rmi.remote;import java.rmi.remoteexception;/** * Remote interface to enable remote * @author LXZ * */public I Nterface Iremote extends remote{public String Show () throws remoteexception;//declaration method}
2, interface implementation, need to inherit the UnicastRemoteObject class, etc. will be analyzed
Package Remote.test;import java.rmi.remoteexception;import java.rmi.server.unicastremoteobject;/** * Remote interface implementation, Inherit UnicastRemoteObject * @author LXZ * */public class Remoteimpl extends UnicastRemoteObject implements iremote{ Public Remoteimpl () throws remoteexception{}//constructor method public String Show () throws remoteexception{//call method implementation System.out.println ("enter"); System.out.println (This.tostring ()); Return "Remote Call succeeded";} }
3, service side to local port 1234 Object Registry Register object and its name
Package Remote.test;import Java.net.malformedurlexception;import Java.rmi.alreadyboundexception;import Java.rmi.remoteexception;import Java.rmi.registry.locateregistry;import java.rmi.registry.registry;/** * Server-side startup, create an object registration list on the port, register the remote Call object to the object registry * @author LXZ * */public class TestServer {public static void Main (string[] args) Throws Malformedurlexception, RemoteException, Alreadyboundexception, interruptedexception { RemoteImpl r = new Remoteimpl ();//Create a remote object Registry rr = locateregistry.createregistry (1234);//Create an object registry on port 1234, If you have already created it, use the GetRegistry method to get the rr.bind ("Testrmi", r);//Register the Object System.out.println (r.tostring ()) in the registry;} }
4, according to the JDK API, the above remote services, even if the installation is complete, the following through the client call test
Package Remote.test;import Java.net.malformedurlexception;import Java.rmi.naming;import java.rmi.NotBoundException Import java.rmi.remoteexception;/** * Client start, Get object reference in remote object Registry * @author LXZ * */public class TestClient {public Stati c void Main (string[] args) throws Malformedurlexception, RemoteException, notboundexception { iremote r = (iremote) Na Ming.lookup ("Rmi://localhost:1234/testrmi");//Gets the stub String a = R.show () of the testrmi in the remote 1234 Port Object Registry ,//invokes the referenced method, The stub is actually called, and the stub interacts with the server and returns the result System.out.println (a);} }
The results of the implementation are as follows:
------------------------------------------
Service side:
Remoteimpl[unicastserverref [Liveref: [endpoint:[192.168.1.253:58169] (local), OBJID:[-60651394:1539D5944E6:-7FFF, -6910034932968554489] []]
Client:
Remote Call succeeded
------------------------------------------
This completes a complete example of a remote object registration and a remote object method invocation. Now, based on this example, why is it inheriting unicastremoteobject, implementing remote, registering with the registry, and so on.
First, the remote object implementation class needs to inherit the UnicastRemoteObject class, UnicastRemoteObject has the ability to register as a remote object, to generate remote references, etc., all have been packaged by the JDK, do not need to write, some of the implementation of the Sun package at the beginning of the , not public.
UnicastRemoteObject Inheritance Relationship:
With the remote object implementation class, look at the server-side startup logic, where:
Locateregistry class: The object registry used to create or get a port
Locateregistry.createregistry: This method means getting the remote object Registry Reference and returning the Registry object
Registry: The interface that really operates the remote object registry
Then
Rr.bind ("Testrmi", R);
Using the Registry object, register the remote object you just created as the name Testrmi. Here is another way of writing, the effect is the same.
Locateregistry.createregistry (1234); Create, if already created can omit this sentence naming.bind ("Rmi://localhost:1234/testrmi", r);//need to bring an upper port
Naming: Tool classes interacting with the object registry
Above is the entire logic that the server has created from the remote object to the object registration. The logic of the client invocation is simple, after obtaining a reference to the remote object through the naming tool class, it can be used normally.
(Iremote) Naming.lookup ("Rmi://localhost:1234/testrmi");
The "reference" returned here is different from the usual object reference, which is the reference information for the remote object. Once you get this reference, you can invoke the method in the same way you would use a real object.
End.
Http://www.cnblogs.com/qinggege/p/5306852.html
Java RMI (remote method invocation) instance and analysis (GO)