Distributed object technology is mainly to establish an application system framework and an object component in a distributed heterogeneous environment. Under the support of the application system framework, developers can encapsulate software functions into objects that are more manageable and used, which can interoperate across different soft and hardware platforms. At present, distributed interoperability standards mainly include Microsoft COM / DCOM standards, Sun's Java RMI standards and OMG organization CORBA standards.
Introduction to Java RMI
Remote Method Call (RMI, Remote Method Invocation) is a distributed object package introduced in JDK1.1, which greatly simplifies communication between Java applications in a distributed heterogeneous environment.
To use RMI, you must build four main classes: local interfaces, remote objects, RMI clients, and RMI servers. The RMI server generates an instance of the remote object implementation and registers with a proprietary URL. The RMI client finds a service object on the remote RMI server and converts it to the type of interface, and then uses it like a local object.
Below is a simple RMI instance, the RMI client outputs a statement through the method provided by the RMI server. Although it is very simple, it has mastered the basic principles and methods of Java RMI calls. When implementing complex applications, we need to do it only to improve the implementation of remote objects.
RMI instance analysis
1. Local interface declaration of remote objects (RMIOPERATE.JAVA)
· This class is just an interface declaration, and the RMI client can use it directly, and the RMI server must implement it through a remote object and register an instance of it with a proprietary URL.
· Remote interface extension
Java.rmi.Remote interface.
· In addition to all application specific exceptions, each method must also declare in the throws clause
Java.rmi.RemoteException (or
RemoteException's parent class).
Hello.java
/ *
* @Author javamxj (9cbs blog) creation date 2004-12-27
* /
Import java.rmi. *;
// RMI local interface must be derived from the Remote interface
Public interface hello extends remote {
// Declaration of specific methods in the interface, pay attention must be declared to throw RemoteException
String Sayhello (String Name) throws RemoteException;
}
2. Remote object implementation
This class should implement the local interface of the remote service object called by the RMI client, which must be inherited from UnicastRemoteObject, and the constructor should throw the RemoteException exception.
HelloIMPL.JAVA
/ *
* @Author javamxj (9cbs blog) creation date 2004-12-27
* /
Import java.rmi. *;
Import javax.rmi.portableremoteObject;
Public Class HelloIMPL Extends PortableremoteObject Implements Hello {
/* Constructor */
Public helloimpl () throws remoteException {
Super ();
}
/ * Realize the 'SayHello () method of declaring in the local interface * /
Public String Sayhello (String Message) Throws RemoteException {
System.out.println ("I am in the server side of the RMI, the client is calling the 'SayHello' method.");
System.out.println ("Hello" Message; Return Message;
}
}
3.RMI server class
This class creates a remote object to implement an instance of class HelloIMPL, and then register it through a proprietary URL. The so-called registration is to bind the HelloIMPL instance to the specified URL through the java.rmi.naming.bind () method or java.rmi.naming.rebind () method.
Helloserver.java /*
* @Author javamxj (9cbs blog) creation date 2004-12-27
* /
Import java.rmi. *;
Public class helloserver {
Public static void main (String [] args) {
// Set the security mechanism on the server side
/ *
IF (system.getsecuritymanager () == null) {
System.SetSecurityManager (New RMISecurityManager ());
}
* /
Try {
System.out.Println ("Start RMI Server ...");
/ * Create an implementation instance of a remote object * /
HelloImpl HiMPL = New HelloIMPL ();
System.out.println ("Register an instance to a proprietary URL");
Naming.Rebind ("HelloService", HIMPL);
System.out.println ("Waiting for RMI Client Tune ...");
System.out.println ("");
} catch (exception e) {
System.out.println ("Error:" E);
}
}
}
Please note the following parameters for the Rebind method call:
The first parameter is java.lang.string in the URL format, represents the location and name of the remote object.
You need to change the value of MyHost to the server name or IP address. Otherwise, if it is omitted in the URL, the host default is the current host and does not require a specified protocol (such as "HelloServer" in the URL. In the URL, you can choose to provide port numbers:, for example, "// myhost: 1234 / HelloServer". The port default is 1099. You need to specify the port number unless the server creates a registration service in the default 1099 port. The second parameter is implemented in an object from which to call the remote method. The RMI runtime will use a reference to the Remote Object Stub program instead of the actual remote object reference specified by the HIMPL parameter. Remote implementation objects (such as HelloIMPL instances) will always leave virtual machines that create their virtual machines. Therefore, when the client performs a lookup in the server's remote object registration service, the object containing the STUB program of the implementation is returned.
4.RMI client class
· RMI customers use the java.rmi.naming.lookup () method, find the RMI service object on the specified remote host, and convert it to the RMIoperate type of the RMIoperate type. It is different from CORBA that the RMI client must know the URL of the RMI service host, which can be specified by RMI: // Host / Path or RMI: // Host: Port / Path, if the port number is omitted, it is default Use 1099.
· Java.rmi.naming.lookup () method may generate three exceptions: java.rmi.RemoteException, java.rmi.notboundexception, java.net. Malformedurlexception, three exceptions need to be captured. HelloClient.java /*
* @Author javamxj (9cbs blog) creation date 2004-12-27
* /
Import java.rmi. *;
Public class helloclient {
Public static void main (String [] args) {
// Set the security mechanism on the server side
/ *
IF (system.getsecuritymanager () == null) {
System.SetSecurityManager (New RMISecurityManager ());
}
* /
/ * Default is local host and default port * /
String host = "localhost: 1099";
/ * When using the input parameters, set the host to the specified host * /
IF (args.length> 0)
Host = args [0];
Try {
/ * Remote implementation object * / according to the specified URL
/ * "H" is an identifier, we will use it to point to remote objects that implement the "Hello" interface * /
Hello H = (Hello) Naming.lookup ("RMI: //" Host "/ HelloService");
System.out.println ("Remove the remote object of the" Hello "interface:" H);
System.out.println ("I am on the client, start calling the 'SayHello' method of the RMI server side);
System.out.println ("Welcome," H.SAYHELLO ("JavaMxj Blog");
} catch (exception ex) {
System.out.println ("Error" EX);
}
}
}
5. Compile Code and Run System:
In the MS-DOS environment, create a D: / rmisample directory, copy the above 4 files into this directory, and then build two folders under this directory: Client and Server (treat them as a client, Server).
(1). Compile all source code
D: / rmisample> javac * .java
(2). Generate a client stub and server framework
D: / rmisample> RMIC HelloIMPL
This will generate HelloIMPL_STUB.CLASS and HelloIMPL_SKEL.CLASS.
(
Note: If you need to view the source code of these two classes, you can use the "RMIC-Keep HelloImpl" statement)
(3). Copy hello.class, helloclient.class, helloimpl_stub.class to the client directory;
Copy hello.class, helloserver.class, helloimpl_stub.class, HelloIMPL_STUB.CLASS to the Server directory.
(4). Start RMI registration
D: / RMisample / Server> RMIREGISTRY
(Note: I am running this system under the command console, I must open three console windows, one running RMIREGISTRY, a running server, and a run client.) (5). Run and call
● Execute HelloServer on the server
D: / RMisample / Server> Java HelloServer
● Run HelloClient on the local client
D: / RMisample / Client> Java HelloClient
● Run HelloClient on the remote client (need to specify the RMI server host name or IP address)
Java HelloClient 222.222.34.34
Results after running RMIREGISTRY and SERVER:
Run the result after the Client:
One point to pay attention, in the above example I commented on the code of security management, if you remove the comment, then you need to create a security policy file, such as the file name as policy.txt, the content is as follows:
Grant {permission java.security.Allpermission "", "";
This is a simple security policy that allows anyone to do anything, for your more critical applications, you must specify a more detailed security policy. Copy this file to the Client and Server directory, then run as follows:
D: / rmisample / server> java -djava.security.policy = policy.txt HelloServer
D: / rmisample / client> java -djava.security.policy = policy.txt helloclient