How RMI Works

Source: Internet
Author: User
Tags object serialization

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
    1. Package org.shirdrn.rmi.server;
    2. Import java.io.IOException;
    3. Public interface Shirdrnservice {
    4. Public String GetServerTime () throws IOException, ClassNotFoundException;
    5. Public String getgreetings () throws IOException, ClassNotFoundException;
    6. }

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
  1. Package org.shirdrn.rmi.server;
  2. Import java.io.IOException;
  3. Import Java.util.Date;
  4. Public class Shirdrnserviceimpl implements Shirdrnservice {
  5. Public Shirdrnserviceimpl () {
  6. }
  7. Public String GetServerTime () throws IOException, classnotfoundexception{
  8. Date date = new Date ();
  9. String time = date.tolocalestring ();
  10. return time;
  11. }
  12. Public String getgreetings () throws IOException, classnotfoundexception{
  13. Date date = new Date ();
  14. int hour = Date.gethours ();
  15. String greetings = "";
  16. if (Hour < && hour > 6) {
  17. Greetings = "Good morning!"  ";
  18. }
  19. Else if (hour <= && hour > ) {
  20. Greetings = "Good afternoon!"  ";
  21. }
  22. else{
  23. Greetings = "Good evening!"  ";
  24. }
  25. return greetings;
  26. }
  27. }

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
  1. Package org.shirdrn.rmi.client;
  2. Import java.io.IOException;
  3. Import Java.io.ObjectInputStream;
  4. Import Java.io.ObjectOutputStream;
  5. Import Java.net.Socket;
  6. Import java.net.UnknownHostException;
  7. Import Org.shirdrn.rmi.server.ShirdrnService;
  8. Public class Shirdrnstub implements shirdrnservice{
  9. Socket socket;
  10. Public shirdrnstub () {
  11. try {
  12. Socket = New socket ("56987b31c0b246d",8888);
  13. } catch (Unknownhostexception e) {
  14. E.printstacktrace ();
  15. System.out.println ("[message] Create socket exception: Unknown host name.  ");
  16. } catch (IOException e) {
  17. E.printstacktrace ();
  18. }
  19. }
  20. Public String GetServerTime () throws IOException, classnotfoundexception {
  21. ObjectOutputStream Oos = new ObjectOutputStream (Socket.getoutputstream ());
  22. Oos.writeobject ("GetServerTime");
  23. Oos.flush ();
  24. ObjectInputStream ois = new ObjectInputStream (Socket.getinputstream ());
  25. return (String) ois.readobject ();
  26. }
  27. Public String getgreetings () throws IOException, classnotfoundexception {
  28. ObjectOutputStream Oos = new ObjectOutputStream (Socket.getoutputstream ());
  29. Oos.writeobject ("getgreetings");
  30. Oos.flush ();
  31. ObjectInputStream ois = new ObjectInputStream (Socket.getinputstream ());
  32. return (String) ois.readobject ();
  33. }
  34. }

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
  1. Package org.shirdrn.rmi.server;
  2. Import Java.io.ObjectInputStream;
  3. Import Java.io.ObjectOutputStream;
  4. Import Java.net.ServerSocket;
  5. Import Java.net.Socket;
  6. Public class Shirdrnskeleton extends Thread {
  7. Shirdrnserviceimpl Shirdrnserviceimpl;
  8. Public Shirdrnskeleton (Shirdrnserviceimpl shirdrnserviceimpl) {
  9. This.shirdrnserviceimpl = Shirdrnserviceimpl;
  10. }
  11. public Void Run () {
  12. try {
  13. ServerSocket serversocket = new ServerSocket (8888);
  14. Socket socket = serversocket.accept ();
  15. While (socket! = null) {
  16. ObjectInputStream ois = new ObjectInputStream (Socket.getinputstream ());
  17. String method = (string) ois.readobject ();
  18. if (method.equals ("GetServerTime")) {
  19. String servertime = Shirdrnserviceimpl.getservertime ();
  20. ObjectOutputStream Oos = new ObjectOutputStream (Socket.getoutputstream ());
  21. Oos.writeobject (Servertime);
  22. Oos.flush ();
  23. }
  24. if (method.equals ("getgreetings")) {
  25. String greetings = shirdrnserviceimpl.getgreetings ();
  26. ObjectOutputStream Oos = new ObjectOutputStream (Socket.getoutputstream ());
  27. Oos.writeobject (Greetings);
  28. Oos.flush ();
  29. }
  30. }
  31. } catch (Exception e) {
  32. E.printstacktrace ();
  33. }
  34. }
  35. public static void Main (string[] args) {
  36. Shirdrnserviceimpl SSI = new Shirdrnserviceimpl ();
  37. Shirdrnskeleton skeleton = new Shirdrnskeleton (SSI);
  38. Skeleton.start ();
  39. }
  40. }

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
  1. Package org.shirdrn.rmi.client;
  2. Import java.io.IOException;
  3. Import Org.shirdrn.rmi.server.ShirdrnService;
  4. Public class Shirdrnclient {
  5. Public shirdrnclient () {
  6. }
  7. public static void Main (string[] args) throws IOException,
  8. classnotfoundexception {
  9. Shirdrnservice stub = new Shirdrnstub ();
  10. System.out.println ("Getting server time ...");
  11. SYSTEM.OUT.PRINTLN ("Server Time:" +stub.getservertime ());
  12. System.out.println ("getting greetings ...");
  13. System.out.println ("Greeting:" +stub.getgreetings ());
  14. }
  15. }

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

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.