RMI remote method call explanation tutorial
1, RMI Overview
RMI (RemoteMETHOD Invocation)
RMI is a distributed object package that simplifies communication between Java applications on multiple computers.
Must be above JDK1.1 or more
RMI used
Java.rmi.Remote
All objects that can be called must be implemented
Java.rmi.server.UnicastRemoteObject
All objects that can be called must expand this class
What is RMI
Remote method call is a mechanism between a computer between a computer to call each other, starting a mechanism of the other process.
Using this mechanism, the object on a computer is using the method on another computer.
Sequence syntax rules and the grammar rules that are called between the objects between the local machine.
advantage
This mechanism is designed to distribute the system, and the programming has brought great convenience.
Just follow the RMI rules design, you can do not have to ask the network details below RMI, such as TCP and Socket, etc.
The communication between any two computers is completely responsible by RMI. Calling objects on a remote computer is just as convenient to local objects.
1, object-oriented:
RMI can transmit a complete object as a parameter and return value, not just a predefined data type.
That is, complex types such as Java Haxi table can be passed as a parameter.
2, movable properties:
RMI can move attributes from the client to the server, or move from the server to the client.
3, design method:
Object transfer enables you to take advantage of the power-oriented technology in distributed computing, such as two-layer and three-layer structural systems.
If the user can pass attribute, you can use object-oriented design in your own solution.
All object-oriented design methods do not rely on different properties to play features, if you cannot pass a complete object - including implementation and type
- will lose the advantages provided in design methods.
4, security:
RMI uses Java's built-in security mechanism to ensure the security of user systems when downloading executing programs.
RMI uses a safety management procedure designed to protect the system from malicious applets.
5, easy to write and use
RMI makes the Java remote service program and the writing of Java client access to these sergers have become easier and simple.
The remote interface is actually a Java interface.
In order to implement the function of RMI, you must create a remote object any object that can be called to be called must implement a remote interface. But remote
The interface itself does not contain any method. Thus you need to create a new interface to extend the remote interface.
The new interface will contain all methods that can be called remotely. Remote objects must implement this new interface, due to new interfaces extension
Remote interface, implements a new interface, meets the requirements of remote objects to implement remote interfaces, each object implemented
As a remote object reference.
Five steps to create a remote method call:
1. Define a remote interface interface that each method in the interface must declare it will generate a RemoteException exception.
2. Define a class that implements the interface.
3. Use the RMIC program to generate the residual root and framework required for remote implementation.
4. Create a client program and the server for RMI calls.
5. Start Registry and run your remote server and client.
First step, extend the remote interface
Creating a RMI program first to do is to create an interface to extend the remote interface. You can add any hope that you can add in this interface.
Method of calling on a remote machine.
Interface example
Import java.rmi.remote;
Import.java.rmi.remoteexception;
Public Interface RemoteInterface Extends Java.rmi.Remote
{
Public String Start (String MSG).
}
A method of returning a string is defined in the interface
The local interface (RemoteInterface) must be public, otherwise the client will errors when loading a remote object that implements the interface. It must inherit from java.rmi.Remote, each method in the interface must throw a remote exception java.rmi.RemoteException.
Throw this abnormal reason
Since any remote method call actually needs many low-level network operations, network errors may happen at any time during the call.
Therefore, all RMI operations should be placed in the TRY-CATCH block.
Step 2 define a class that implements the interface.
This class must inherit the UnicastRemoteObject class.
Expand java.rmi.server.UnicastRemoteObject
UnicastRemoteObject As the name suggests, the client is a one-to-one connection to the server object instance.
Extended the class routine that implements interfaces
Server key statement
Naming.rebind ("/ rmitest", rmitst);
// Name and interface binding
In the third step, generate a residual root and frame code
In RMI, the code generated on the client and the code of the anti-modulating return value are referred to as the residual root. This part of the code is called "backbone" on some books.
The reverse modulation call parameters generated on the server and the code that modes the actual method call to transfer the return value are called the framework.
Generate tools for generating residual roots and frames
RMIC Command Board Tool (RMI Compiler)
format:
RMIC ClassName
Step 4, create a client program for RMI call
Import java.rmi.naming;
Import java.rmi.notboundexception;
Import java.rmi.remoteexception;
Import java.net.malformedurlexception;
Public class remoteClientTest {
Static RemoteInterface Gserve = NULL;
Void Display () {
Try {// Find Remote Objects
Gserve = (transoteinterface) Naming.lookup ("// localhost / rmitst");
String msg = GSERVER.START ();
System.out.println ("Server Return Information:" MSG);
Catch (Exception E1) {System.out.println (E1);
}
Public static void main (String [] args) {
RemoteClientTest App = New RemoteClientTest ();
app.display ();
}
Client-end key statement
Gserve = (transoteinterface) Naming.lookup ("// localhost / rmitst");
/ / Find remote objects
Step 5, find the remote object with RMIREGISTRY
This is a mapping between tools, maintenance text names, and remote objects, which can be remotely accessed.