Remote method call
Remote Method Invocation (RMI) allows users to access
The Java object on the host and remotely call its method. The program object is the customer, while the remote object is the server
Server. Remote objects can also be customers of another remote service object. By using continuity (Serial
Local objects and original type values can be passed as parameters to remote objects. This
This method allows Java programs to use distributed computing to distribute the workload to multiple Java virtual machines.
Working Principle
RMI system structure, which has several layers on both the client and server.
-------------------
| Customer | Server |
--------------------
|
-----------------------
| Placeholder program | Backbone Network |
-------------------------
|
------------------------------------
| Remote reference layer |
------------------------------------
|
------------------------------------
| Transmission layer |
------------------------------------
Method call from the client object through the placeholder Program (Stub), Remote Reference layer (Remote Reference)
Layer) and Transport Layer (Transport Layer) down, pass to the host, and then pass
The transport layer goes up through the Remote Call layer and the backbone network (Skeleton) to reach the server object.
The placeholder program acts as the proxy of the remote server object so that the object can be activated by the customer.
The remote reference layer processes semantics, manages communication between single or multiple objects, and determines that the call should be sent to
There are still multiple servers. The Transport Layer manages the actual connections and traces the methods that can be called.
Remote Object. The server-side Backbone Network calls the actual method of the server object and obtains
Return Value. The returned value is passed back to the client through the remote reference layer and the transmission layer of the server, and then to the client.
Returned by the transport layer and remote call layer. Finally, the placeholder program obtains the returned value.
To complete the preceding steps, you need to perform the following steps:
1. Generate a remote interface
2. implement remote objects (server programs)
3. Generate placeholder program and backbone network (server program)
4. Write server programs
5. Write customer programs
6. Register a remote object
7. Start a remote object
The specific implementation is as follows:
1. Generate a remote interface
Package c15.ptime;
Import java. rmi .*;
Public interface PerfectTimeI extends Remote {
Long getPerfectTime () throws RemoteException;
}
2. implement remote objects (server programs)
Package c15.ptime;
Import java. rmi .*;
Import java. rmi. server .*;
Import java. rmi. registry .*;
Import java.net .*;
Public class PerfectTime
Extends UnicastRemoteObject
Implements PerfectTimeI {
Public long getPerfectTime ()
Throws RemoteException {
Return System. currentTimeMillis ();
}
Public PerfectTime () throws RemoteException {
Super ();
}
Public static void main (String [] args ){
System. setSecurityManager (
New RMISecurityManager ());
Try {
PerfectTime pt = new PerfectTime ();
Naming. rebind (
"// Zhouty: 2005/PerfectTime", pt );
System. out. println ("Ready to do time ");
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
4. Compile remote objects (server programs)
Javac-classpath.-d. PerfectTime. java
5. Generate root and stem (placeholder program and backbone Program)
Rmic-classpath.-d. c15.ptime. PerfectTime
6. Register a remote object
Start rmiregistry 2005
7. Start the server program
Java-Djava. rmi. server. codebase = file: // d:/TestRMI/c15.ptime. Per
FectTime
8. Write client programs
Package c15.ptime;
Import java. rmi .*;
Import java. rmi. registry .*;
Public class DisplayPerfectTime {
Public static void main (String [] args ){
System. setSecurityManager (
New RMISecurityManager ());
Try {
PerfectTimeI t =
(PerfectTimeI) Naming. lookup (
"192.168.0.171: 2005/PerfectTime ");
For (int I = 0; I <10; I ++)
System. out. println ("Perfect time =" +
T. getPerfectTime ());
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
9. Compile the client program
Javac-classpath.-d. DisplayPerfectTime. java
10. Modify the JVM configuration file (both the client and server must be modified)
Using jre_home1_policytool.exe
11. Start the customer Program
Java-classpath. c15.ptime. DisplayPerfectTime
12. Returned results
Perfect time= 967274884390
Perfect time= 967274884450
Perfect time= 967274884450
Perfect time= 967274884450
Perfect time= 967274884500
Perfect time= 967274884500
Perfect time= 967274884560
Perfect time= 967274884610
Perfect time= 967274884610
Perfect time= 967274884610
<------------ ------------->
Refer:
JAVA programming ideas
JBUILDER2
Sun jdk document
--
I think, so I am