Author: bluesky35 (blue) development environment: Windows2000 JBuilderX JDK1.4.2 remote method invocation (Remote Method Invocation, RMI) can communicate with the target objects between different Java Virtual Machine (JVM) use RMI to write client -. Server applications include 6 basic steps: 1. Define Remote Interface 2. Implement Remote Interface 3. Write customers using remote objects 4. Generate STUB (Customer Agents) and Skeletom 5. Start registry and registration object 6 Running the server and the customer to introduce each step by a simple instance .1. Defining a remote interface Create a project called RMisample in jbuilder, then create a Class of Riminterface in 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 class implements a remote interface then create a project called the root HelloServer, to achieve the above interfaces, the following code. : Import java.rmi. *; import java.rmi.server. *; import java.util.date;
PublicaTreMoteObject 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 a specific method of Sayhello in the interface. 3. Write customers using remote objects Create a Class called HelloClient in the Project root directory to write customers 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 realize the mutual communication between the virtual machine. 4. Generate a Stub (Customer Agent) and Skeletom A Command window, generates a STUB (Customer Agent) with the RMIC HelloServer statement. The resulting result is shown below: 5. Start the registry and register the object to open Java_Home / JRE / LIB / Security / Java.Policy, Change the original authorization method to Permission java.net.socketpermission "*: 1024-65535", "accept, connection, listen, resolve"; then save exit. Use the command RMIREGISTRY -J-DJAVA.Security.Policy = java.policy Start the registry, as shown below: Then create a Class of Registerit in the Project root directory, 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);}}} With the java registerit command to register the interface, as shown below: 6. Run the server and the customer finally, run the customer Diffuse program, as shown below:
Ok, about RMI, you will introduce here, see you next time ^ - ^!