Development environment: Windows2000 JBuilderx JDK1.4.2
Remote Method Invocation, RMI can communicate with objects and objects between different Java virtual machines (JVMs).
Writing a customer using RMI - server application includes 6 basic steps:
Define remote interface
2. Realize remote interface
3. Write customers who use remote objects
4. Generate STUB (Customer Agent) and SKELETOM (Server Entity)
5. Start the registry and register the object
6. Run server and customers
By a simple example, you will introduce each step.
Define remote interface
Create a project called RMisample in JBuilder, then create a Class of Riminterface at the root directory, defining the interface code as follows:
Public Class Riminterface {
Interface hellointerface extends java.rmi.remote {
Public String Sayhello () THROWS JAVA.RMI.RemoteException;
}
}
2. Realize remote interface
Create a Class called HelloServer again in the Project root directory, implement the above interface, the code is as follows:
Import java.rmi. *;
Import java.rmi.server. *;
Import java.util.date;
Public Class HelloServer Extends UnicastRemoteObject Implements Riminterface.Hellointerface {
Public helloserver () throws remoteException {
Super ();
}
Public String Sayhello () throws java.rmi.remoteexception {
Return "It`s from bluesky!" new date ();
}
}
The SAYHELLO is the specific method of Sayhello in the interface.
3. Write customers who use remote objects
Create a Class called HelloClient again in the Project root directory, write the customer using the remote object, the code is as follows:
Import java.rmi. *;
Public class helloclient {
Public static void main (string args []) {
IF (system.getsecuritymanager () == null) {
System.SetSecurityManager (New RMISecurityManager ());
}
Try {
Riminterface.hellointerface obj = (riminterface.hellointerface) Naming.lookup ("/ helloserver");
String message = obj.sayhello ();
System.out.println (Message);
} catch (exception e) {
System.out.println (E.TOString ());
}
}
}
The client passes Naming.lookup ("/ HelloServer"); finds the remote interface of the Server and calls the method to implement mutual communication between the virtual machine.
4. Generate STUB (Customer Agent) and SKELETOM (Server Entity)
Newly open a Command window, generate STUB (Customer Agents) and SKELETOM with the RMIC HelloServer statement.
5. Start the registry and register the object
Open Java_Home / JRE / LIB / Security / Java.Policy, change the original authorization method to Permission java.net.socketpermission "*: 1024-65535", "accept, connect, listen, resolve"; then save exit. Use the command Rmiregistry -j-djava.security.policy = java.policy launches the registry. Then in the Project root directory, create a Class called Registerit, and write the registration program as follows:
Import java.rmi. *;
Public class registerit {
Public static void main (string args []) {
Try {
HelloServer obj = new helloserver ();
System.out.println ("Object IS" OBJ);
Naming.rebind ("/ helloserver", obj);
System.out.println ("Already Start");
} catch (exception e) {
System.out.print (e);
}
}
}
Use the java registerit command to register the interface.
6. Run server and customers
Finally, run the client program.
Ok, about RMI, you will introduce here, see you next time ^ - ^!