RMI working principle (GO) Work Socketjava Application Server network application RMI (remote method invocation) is a set of Java APIs that support the development of distributed applications. RMI defines a remote object using the Java language interface, which aggregates the Java serialization and Java Remote Method Protocol (Java Protocol). It greatly enhances the ability of Java to develop distributed applications. Simply put, this makes the original program in the same operating system as the method call, which becomes a method call between the different operating systems of the program. This also means that RMI can implement a call to execute a method between different JVMs. Since the Java EE is a distributed program platform, RMI mechanism realizes the communication between the program components and different operating systems.
Is the RMI architecture:
With the RMI architecture, you can already see how RMI works in general:
Server-side services that expose remote methods that can be invoked in the form of interfaces, so that clients can invoke remote methods through the service interface to implement complex business logic. On the server side, the implementation of the methods provided in the interface first, so that the client calls can complete a certain business logic, then need to generate skeleton, in the skeleton to really implement the call to the business method, the completion of the call of the client request process, The result of the fetch method is returned to the client through a serialization mechanism, which is answered. On the client side, the stub is adopted to receive the data (object) returned by the server, that is, it is deserialized here, that is, to read the stream of bytes of the network, and then refactor. In skeleton and stubs, network communication is processed, such as establishing sockets, establishing network connections, and preparing for the actual business needs.
As can be seen, the client does not really perform direct interaction with the server-side components.
Here, there is an important concept, which is the Java object Serialization mechanism. Serialization is the writing of an object to a stream so that the object can be transmitted over the network, which is executed at the output. Deserialization, that is, at the receiving end, in order to be able to obtain the transmitted object, it is necessary to reconstruct the stream of bytes of the object, and get the complete object again. The process of serializing a Java object is to serialize an instance of an existing class, starting with a concrete instance.
The following example is implemented by invoking the server-side method on the client, which can intuitively simulate the RMI work, further deepening the understanding of the RMI working mechanism. I developed tests using Oracle JDeveloper 10g.
Development process
1, in the service interface, the client can make a call method exposed to the client.
The Shirdrnservice interface lists the methods that can be called, and the Shirdrnservice interface is as follows:
Java code
- Package org.shirdrn.rmi.server;
- Import java.io.IOException;
- Public interface Shirdrnservice {
- Public String GetServerTime () throws IOException, ClassNotFoundException;
- Public String getgreetings () throws IOException, ClassNotFoundException;
- }
2, in the server side to really implement these methods in the service interface, the Shirdrnserviceimpl class implements the methods listed in the Shirdrnservice interface.
The implementation of the Shirdrnserviceimpl class is as follows:
Java code
- Package org.shirdrn.rmi.server;
- Import java.io.IOException;
- Import Java.util.Date;
- Public class Shirdrnserviceimpl implements Shirdrnservice {
- Public Shirdrnserviceimpl () {
- }
- Public String GetServerTime () throws IOException, classnotfoundexception{
- Date date = new Date ();
- String time = date.tolocalestring ();
- return time;
- }
- Public String getgreetings () throws IOException, classnotfoundexception{
- Date date = new Date ();
- int hour = Date.gethours ();
- String greetings = "";
- if (Hour < && hour > 6) {
- Greetings = "Good morning!" ";
- }
- Else if (hour <= && hour > ) {
- Greetings = "Good afternoon!" ";
- }
- else{
- Greetings = "Good evening!" ";
- }
- return greetings;
- }
- }
3, the implementation of the client stub
The Shirdrnstub class implements the Shirdrnservice interface, which is the most straightforward implementation of the client's access to the service result data. However, it does not deal directly with the implementation class that actually implements the service interface on the server side, but instead obtains the execution result data of the calling method through the network transmission.
Java code
- Package org.shirdrn.rmi.client;
- Import java.io.IOException;
- Import Java.io.ObjectInputStream;
- Import Java.io.ObjectOutputStream;
- Import Java.net.Socket;
- Import java.net.UnknownHostException;
- Import Org.shirdrn.rmi.server.ShirdrnService;
- Public class Shirdrnstub implements shirdrnservice{
- Socket socket;
- Public shirdrnstub () {
- try {
- Socket = New socket ("56987b31c0b246d",8888);
- } catch (Unknownhostexception e) {
- E.printstacktrace ();
- System.out.println ("[message] Create socket exception: Unknown host name. ");
- } catch (IOException e) {
- E.printstacktrace ();
- }
- }
- Public String GetServerTime () throws IOException, classnotfoundexception {
- ObjectOutputStream Oos = new ObjectOutputStream (Socket.getoutputstream ());
- Oos.writeobject ("GetServerTime");
- Oos.flush ();
- ObjectInputStream ois = new ObjectInputStream (Socket.getinputstream ());
- return (String) ois.readobject ();
- }
- Public String getgreetings () throws IOException, classnotfoundexception {
- ObjectOutputStream Oos = new ObjectOutputStream (Socket.getoutputstream ());
- Oos.writeobject ("getgreetings");
- Oos.flush ();
- ObjectInputStream ois = new ObjectInputStream (Socket.getinputstream ());
- return (String) ois.readobject ();
- }
- }
4, the implementation of server-side skeleton
The Shirdrnskeleton class inherits the thread, so it can start the server thread, and it is a single thread. In Shirdrnskeleton, there is a real interaction with the class that provides the service implementation. After establishing the connection, first obtains the method which the client calls, according to the customer's choice to directly interacts with the service realization, executes the method obtains the execution result data, thus serializes the result data, transmits to the customer through the network to complete the reply process.
After instantiating an instance of the Shirdrnskeleton class, start the thread, which is much like a server-side listener that listens for client actions to complete the service request.
Java code
- Package org.shirdrn.rmi.server;
- Import Java.io.ObjectInputStream;
- Import Java.io.ObjectOutputStream;
- Import Java.net.ServerSocket;
- Import Java.net.Socket;
- Public class Shirdrnskeleton extends Thread {
- Shirdrnserviceimpl Shirdrnserviceimpl;
- Public Shirdrnskeleton (Shirdrnserviceimpl shirdrnserviceimpl) {
- This.shirdrnserviceimpl = Shirdrnserviceimpl;
- }
- public Void Run () {
- try {
- ServerSocket serversocket = new ServerSocket (8888);
- Socket socket = serversocket.accept ();
- While (socket! = null) {
- ObjectInputStream ois = new ObjectInputStream (Socket.getinputstream ());
- String method = (string) ois.readobject ();
- if (method.equals ("GetServerTime")) {
- String servertime = Shirdrnserviceimpl.getservertime ();
- ObjectOutputStream Oos = new ObjectOutputStream (Socket.getoutputstream ());
- Oos.writeobject (Servertime);
- Oos.flush ();
- }
- if (method.equals ("getgreetings")) {
- String greetings = shirdrnserviceimpl.getgreetings ();
- ObjectOutputStream Oos = new ObjectOutputStream (Socket.getoutputstream ());
- Oos.writeobject (Greetings);
- Oos.flush ();
- }
- }
- } catch (Exception e) {
- E.printstacktrace ();
- }
- }
- public static void Main (string[] args) {
- Shirdrnserviceimpl SSI = new Shirdrnserviceimpl ();
- Shirdrnskeleton skeleton = new Shirdrnskeleton (SSI);
- Skeleton.start ();
- }
- }
5. Client test Program
By using the main function in the Shirdrnclient class, the method in the service interface provided by the server side is implemented to invoke:
Java code
- Package org.shirdrn.rmi.client;
- Import java.io.IOException;
- Import Org.shirdrn.rmi.server.ShirdrnService;
- Public class Shirdrnclient {
- Public shirdrnclient () {
- }
- public static void Main (string[] args) throws IOException,
- classnotfoundexception {
- Shirdrnservice stub = new Shirdrnstub ();
- System.out.println ("Getting server time ...");
- SYSTEM.OUT.PRINTLN ("Server Time:" +stub.getservertime ());
- System.out.println ("getting greetings ...");
- System.out.println ("Greeting:" +stub.getgreetings ());
- }
- }
6. Test process
Summary of Experience
1, the client is directly through the instantiation of a stub, because the stub implements the interface exposed by the service party, so you can directly through a stub instance to invoke the service method. However, this does not directly invoke the implementation of the service interface of the provider.
2, the service side has the real realization of the service interface. And the real invocation of the service interface implementation class is in the server-side skeleton, in the skeleton is responsible for invoking the real service implementation, the execution results are returned to the client, masking the implementation details of the method call.
3, the client stub and server-side skeleton established network connection between the network communication, the main is to carry out the results of transmission/reception, skeleton the results through the network to the client, and the stub through the proxy interface to receive the resulting data transmission.
How RMI Works