Develop Java Distributed Programs with RMI and CORBA
RMI (Remote Method Call) and CORBA (General Object Call Agent Architecture) are two most important and widely used distribution object system architecture. Each architecture has exceptions, which are not applied to different areas from e-commerce to medical insurance systems. Any of the two is a difficult job in the application to the project. This article probably introduces RMI and CORBA and demonstrates how to develop an example of a remote download file.
. Introduction Distributed Object System
. Introduction RMI and CORBA
.
. Demonstrate how to transfer files from remote hosting from remote hosting from RMI and CORBA
. Compare CORBA and RMI
C / S mode
The client / server mode is a form of distribution calculation of the server and client exchange information. In this mode, clients and servers communicate with the same language - they all understand protocols.
The C / S mode has different ways, and typical applications are low-level Socket development. Developing with Socket means we must design a set of command sets that C / S communicate with each other. An example is an HTTP protocol, providing a GET method, and C / S must be implemented in order to get the document that is transferred.
Distributed mode
System-based distributed objects are used to isolate objects of client requests and server-side services by defining a good interface.
Data requests and execution are separated, this is the main difference between the C / S mode and distributed mode.
In a distributed model, the client sends a message to an object. This object determines which service is executed.
Services or methods, Selection is executed by objects or agents. RMI and CORBA are examples of this model.
RMI
RMI is a system that makes you easily develop distributed market objects. RMI is easier to develop than Sockets and does not need to define protocols. With RMI, developers will consider the local method of calling local categories, which is actually transmitted from remote transmission, translation, and passes the results to the local.
RMI application start
Develop RMI, including the following steps:
1 Define a remote interface
2 Application remote interface
3 server development
4 client development
5 Generate backbone code, start RMI registration, server, client
Here we tell the specific implementation of each step.
Example: File Transmission Application
Allow any type of file to the remote host. The first step defines the remote interface of the description method.
Define remote interface
Code One is the remote interface of downloading the file. Interface FileInterface provides a method of downloading a file, returning the file's byte set.
Code Sample 1: FileInterface.java
Import java.rmi.remote;
Import java.rmi.remoteexception;
Public interface fileInterface extends remote {
Public Byte [] Downloadfile (String FileName) Throws
RemoteException;
}
note:
For the client to load remote objects containing remote interfaces, the interface must be defined as public
The Remote interface must be expanded to implement a remote object
Each method in the interface must throw a RemoteException exception
Application remote interface
The next step is to implement interface fileInterface, a simple application in code.
Note To implement interface fileInterface, class fileIMPL must extend UnicastRemoteObject.
This shows that the class fileIMPL creates a single and cannot copy the RMI object transmitted by TCP.
Code Sample 2: fileImpl.java
Import java.io. *;
Import java.rmi. *;
Import java.rmi.server.UnicastRemoteObject;
Public Class FileImpl Extends UnicastRemoteObjectImplements FileInterface {
PRIVATE STRING NAME;
Public fileIMPL (String s) throws remoteexception {
Super ();
Name = s;
}
Public Byte [] Downloadfile (String FileName) {
Try {
File File = New File (filename);
BYTE BUFFER [] = new byte [(int) file.length ()];
BufferedInputStream Input = New
BufferedInputStream (New FileInputStream (FileName);
Input.read (Buffer, 0, Buffer.Length);
INPUT.CLOSE ();
Return (Buffer);
} catch (exception e) {
System.out.println ("FileImpl:" E.getMessage ());
E.PrintStackTrace ();
Return (NULL);
}
}
}
Server development
The third step is to develop the server side, there are three steps:
1 Create a RMISECURityManager instance and install it
2 Creating an instance of a remote object
3 Register the object with the RMI.
Code Sample 3: FileServer.java
Import java.io. *;
Import java.rmi. *;
Public class fileserver {
Public static void main (String Argv []) {
IF (system.getsecuritymanager () == null) {
System.SetSecurityManager (New RMISecurityManager ());
}
Try {
FileInterface FI = New FileImpl ("FileServer");
Naming.rebind ("// 127.0.0.1/fileserver", fi);
} catch (exception e) {
System.out.println ("FileServer:" E.getMessage ());
E.PrintStackTrace ();
}
}
}
This Naming.Rebind ("// 127.0.0.1/fileserver", fi) Description RMI registration is running in the default port 1099. If the RMI registration is in other ports, this sentence should be changed to: naming.rebind ("// 127.0.0.1:4500/fileserver", fi)
Client development
The next step is to develop the client. The client remotely invokes any method defined by the remote interface. For implementation, the client must first obtain an instance of the remote object of the RMI registration. Once the download file method is downloaded, don't call it. Such as code 4. In this application, the client receives two command line parameters.
Code Sample 4: FileClient.java
Import java.io. *;
Import java.rmi. *;
Public class fileclient {
Public static void main (String Argv []) {
IF (argv.length! = 2) {
System.out.println ("USAGE: Java FileClient FileName Machinename);
System.exit (0);
Try {
String name = "//" argv [1] "/ fileserver";
FileInterface Fi = (fileInterface) Naming.lookup (name);
Byte [] filedata = fi.downloadfile (Argv [0]);
File File = New File (Argv [0]);
BufferedoutputStream Output = New
BufferedoutputStream (New FileoutputStream (file.getname ()));
Output.write (FileData, 0, FileData.Length);
Output.flush ();
Output.close ();
} catch (exception e) {
System.err.Println ("FileServer Exception:" E.GetMessage ());
E.PrintStackTrace ();
}
}
}
Run this application
In order to run this application, we must have a backbone code, then compile the server-side, client code, RMI registration, and finally start the application.
In order to generate a backbone code, we use the RMIC command:
Command line: RMIC fileImpl
Two files will be generated: fileImpl_stub.class and fileimpl_skel.class. Client proxy and server backbone.
Next, compile SERVER, Client code
Finally, start RMI registration, run.
Command line> RMIREGISTRY Portnumber
After the registration is complete, you can start the server-side application. If RMI security is applied, you need a security policy to process. Here is a simple security policy:
Grant {
Permission java.security.Allpermission "" ""
}
Note: This is just a simple security policy. If you are more important applications, you need more stringent security policies.
Copy all classes except for the client class. Confirm the security policy After policy.txt, start the server-side application with the following command.
Command line> java -djava.security.policy = policy.txt FileServer
In order to start client applications in different machines, you need to copy (fileInterface.class) and (fileImpl_stub.class).
Command line> Java FileClient FileName Machinename