Stub and Skeleton introduction
The essence of RMI is to implement a call between different JVMs, which is implemented by opening a stub and skeleton in two JVMs, both of which implement the passing of parameters and return values through socket communication.
Some examples of RMI code can be found online, but most of them are through extend the interface Java.rmi.Remote implementation (Basic tutorial Qkxue.net), has been packaged very perfect, inevitably make people have mirrors feeling (Tengyun technology ty300.com). The following example is what I see in Enterprise JavaBeans, or EJB, although it is very rough, but it is very intuitive, it is helpful to quickly understand how it works.
Ⅰ:rmi working principle (custom implementation of a stub and Skeleton)
1. Define a person's interface, which has two business method, Getage () and GetName ()
Person Code:
Public interface Person {
public int getage () throws Throwable;
Public String GetName () throws Throwable;
}
2. Person's implementation Personserver class
Personserver Code:
public class Personserver implements person {
private int age;
private String name;
Public Personserver (String name, int.) {
This.age = age;
THIS.name = name;
}
public int getage () {
return age;
}
Public String GetName () {
return name;
}
}
3. OK, now we are going to call the two business method of Getage () and GetName () on the client machine, then we have to write the corresponding stub (client side) and skeleton (server side) program. This is the implementation of the stub:
Person_stub Code:
Stub person_stub Implementation:
Import Java.io.ObjectOutputStream;
Import Java.io.ObjectInputStream;
Import Java.net.Socket;
public class Person_stub implements person {
private socket socket;
Public Person_stub () throws Throwable {
Connect to Skeleton
Socket = new Socket ("computer_name", 9000);
}
public int getage () throws Throwable {
Pass method name to skeleton
ObjectOutputStream OutStream =
New ObjectOutputStream (Socket.getoutputstream ());
Outstream.writeobject ("Age");
Outstream.flush ();
ObjectInputStream instream =
New ObjectInputStream (Socket.getinputstream ());
return Instream.readint ();
}
Public String GetName () throws Throwable {
Pass method name to skeleton
ObjectOutputStream OutStream =
New ObjectOutputStream (Socket.getoutputstream ());
Outstream.writeobject ("name");
Outstream.flush ();
ObjectInputStream instream =
New ObjectInputStream (Socket.getinputstream ());
Return (String) instream.readobject ();
}
}
Note that person_stub, like personserver, implements person. They all implement the Getage () and GetName () two business method, the difference is that Personserver is true, Person_stub is to establish a socket connection and send a request to skeleton, The Personserver method is then called through skeleton, and the result is returned.
4. Skeleton (Skeleton) implementation
Person_skeleton Code:
Import Java.io.ObjectOutputStream;
Import Java.io.ObjectInputStream;
Import Java.net.Socket;
Import Java.net.ServerSocket;
public class Person_skeleton extends Thread {
Private Personserver MyServer;
Public Person_skeleton (Personserver server) {
Get reference of Object Server
This.myserver = server;
}
public void Run () {
try {
New socket at Port 9000
ServerSocket serversocket = new ServerSocket (9000);
Accept stub ' s request
Socket socket = serversocket.accept ();
while (socket! = NULL) {
Get stub ' s request
ObjectInputStream instream =
New ObjectInputStream (Socket.getinputstream ());
String method = (string) instream.readobject ();
Check method name
if (Method.equals ("Age")) {
Execute Object Server ' s business method
int age = Myserver.getage ();
ObjectOutputStream OutStream =
New ObjectOutputStream (Socket.getoutputstream ());
return result to Stub
Outstream.writeint (age);
Outstream.flush ();
}
if (method.equals ("name")) {
Execute Object Server ' s business method
String name = Myserver.getname ();
ObjectOutputStream OutStream =
New ObjectOutputStream (Socket.getoutputstream ());
return result to Stub
Outstream.writeobject (name);
Outstream.flush ();
}
}
} catch (Throwable t) {
T.printstacktrace ();
System.exit (0);
}
}
public static void Main (String args []) {
New Object Server
Personserver person = new Personserver ("Richard", 34);
Person_skeleton Skel = new Person_skeleton (person);
Skel.start ();
}
}
Skeleton class extends from Thread, it runs in the background, at any time to receive the client sent request. and the corresponding business method is called according to the key sent over.
5. The last one, the implementation of the client
Personclient Code:
public class Personclient {
public static void Main (String [] args) {
try {
Person person = new person_stub ();
int age = Person.getage ();
String name = Person.getname ();
SYSTEM.OUT.PRINTLN (name + ' is ' + age + ' years old ');
} catch (Throwable t) {
T.printstacktrace ();
}
}
}
The essence of Client (personclient) is that it needs to know the definition of the person interface and instance a person_stub, invoking business method through stubs, as to how the stub communicates with the server, The client will have no control.
Notice how it's written:
Person person = new person_stub (); instead of person_stub person = new person_stub (); why? Because to interface programming, hehe.
RMI essentially generates 2 classes of Stub,skeleton to pass parameters and return values, using value-passing methods
Similar to a previously written chat room program, the passed object should implement the Java.io.Serializable interface
Stub and Skeleton introduction