From: http://www.china1024.com/bytesoft/info_show.jsp? News_id = 425
Java RMI (Remote Method Invocation remote method call) is implemented in Java in jdk1.1, which greatly enhances Java's ability to develop distributed applications. Java, as a popular network development language, has great power in its powerful ability to develop distributed network applications, RMI is one of the core solutions for developing a network distributed application system with hundreds of thousands of pure Java. In fact, it can be regarded as the Java version of RPC. However, traditional RPC cannot be well applied to distributed object systems. Java RMI supports communication between Program-level objects stored in different address spaces to achieve seamless remote calls between remote objects. RMI currently uses Java remote message exchange protocol jrmp (Java remote messaging protocol) for communication. Jrmp is a protocol designed for Java remote objects. Therefore, Java RMI has the advantages of Java's "write once, run anywhere" and is a pure Java solution for distributed application systems. Application systems developed using Java RMI can be deployed on any platform that supports JRE (Java Run Environment java, runtime environment. However, because jrmp is designed for Java objects, RMI does not support application systems developed in non-Java languages. It cannot communicate with objects written in non-Java languages. This article illustrates how to use RMI to implement Java distributed applications from the perspective of programs.
I. RMI system Operating Mechanism
RMI applications generally include two independent programs: server programs and client programs. A typical server application creates multiple remote objects so that these remote objects can be referenced and waits for the client to call the methods of these remote objects. A typical client program obtains one or more remote object references from the server, and then calls the remote object method. RMI provides a mechanism for communication and information transmission between servers and clients.
During communication with remote objects, RMI uses standard mechanisms: stub and skeleton. Stub of the remote object acts as the local customer representative or agent of the remote object. The caller will call the local stub method, while the local stub will be responsible for executing the method call to the remote object. In RMI, the remote object's stub is the same as the remote interface set implemented by the remote object. When the stub method is called, the following operations will be performed: (1) initializing the connection with the remote virtual machine that contains the remote object; (2) grouping the parameters of the remote virtual machine (writing and transmitting ); (3) Wait for the method call result; (4) uncompile (read) the return value or return an exception; (5) return the value to the caller. To show the caller a simple call mechanism, Stub hides details such as parameter serialization and network-level communication. In a remote virtual machine, each remote object can have a corresponding skeleton (skeleton is not required in the jdk1.2 environment ). Skeleton is responsible for allocating calls to actual remote objects. It performs the following operations when receiving a method call: (1) uncompile (read) parameters of the remote method; (2) Call methods of the actual remote object implementation; (3) groups (write and transmit) the results (return values or exceptions) to the caller. Stub and skeleton are generated by the rmic compiler.
To write distributed object applications using RMI, you need to do the following: (1) locate remote objects. Applications can use one of the two mechanisms to get reference to remote objects. Rmiregistry, a simple naming tool for RMI, can be used to register its remote objects, and remote object references can be passed and returned as part of common operations. (2) Communication with remote objects. The details of remote communication between objects are handled by RMI. For programmers, remote communication looks like a standard Java method call. (3) Load class bytecode for objects passed as parameters or return values. Because RMI allows calling programs to send pure Java objects to remote objects, RMI provides the necessary mechanism to load object code and transmit object data. When the RMI distributed application is running, the server calls the registration service program to associate the name with a remote object. The client registers the service program on the server to search for the remote object with the name of the remote object, and then calls its method.
*
......
[Read the full text]