Corba
CORBA is an OMG organization for distributed program development standards on enterprise applications. The important point is that CORBA is just a specification. CORBA is well known in ORB. Several CORBA products have appeared in the market, such as Visibroke, Orbix, etc. Javaidl is another application in JDK1.3 and the above version of the core package.
CORBA design is independent of platform and language (Note: Similar to Web Service), CORBA can run on any platform, can be applied in any network, can be written in any language that supports the IDL interface.
Similar to RMI, CORBA objects also need to describe an interface description. However, the CORBA interface needs to be described in a similar to C , and it is necessary to point out that IDL is not a program language.
CORBA application start
Developing CORBA applications There are several steps:
1 Define IDL interface
2 Convert IDL to Java
3 application interface
4 development server side
5 Development Customer
6 Run the name server, server, and client
Let's take a step in step by step to use the application of CORBA development files, similar to RMI. We will use Javaidl, there are in JDK1.3.
Define interface
When you define the interface, we need to consider the type of operation supported by the server. In file transfer applications, you need to use a method of downloading a file. Example 5 With this interface FileInterface. The client will call this method to download the file.
The sequence and array in the IDL are relatively close, but it is not a fixed length.
Note that the String parameter type of Downloadfile has three transmission modes: in (from the client to the server), OUT (from the server side to the client), inout (two-way)
Code Sample 5: FileInterface.IDL
Interface fileInterface {
TypedEf sequern
Data Downloadfile (in string filename);
}
Once the interface definition is complete, you can build Java with the IDLJ compiler of JDK1.3.
The IDLJ compiler has some parameters, such as the -f parameter generates your designated customer-end code or service backbone code, or both. In this example, we will in both machines, so we use -ftrver, -fclient to generate server-side, client Java code.
Below we compile the interface to generate server-side code, with the following command:
Prompt> idlj -fserver fileInterface.idl
This command will generate SKELETON, HOLDER, HELPER, and other classes. Generate class _fileInterfaceImplbase, we will use this type of interface to implement applications.
Application interface
Below we will implement the download method.
Code Sample 6: FileServant.java
Import java.io. *;
Public class fileservant extends _fileinterfaceImplbase {
Public Byte [] Downloadfile (String FileName) {
File File = New File (filename);
BYTE BUFFER [] = new byte [(int) file.length ()];
Try {
BufferedInputStream Input = New
BufferedInputStream (New FileInputStream (FileName);
Input.read (Buffer, 0, Buffer.Length);
INPUT.CLOSE ();
} catch (exception e) {
System.out.println ("FileServant Error:" E.GETMESSAGE ()); E.PrintStackTrace ();
}
Return (Buffer);
}
}
Server development
Next we develop the server side. Includes the following steps:
1 Initialization ORB
2 Create a FILESERVANT object
3 Register an object name in the CORBA Name Service
4 Print a status information
5 Waiting for the client request
Code Sample 7: FILESERVER.JAVA
Import java.io. *;
Import org.omg.cosnaming. *;
Import Org.omg.cosnaming.namingContextPackage. *;
Import org.omg.corba. *;
Public class fileserver {
Public static void main (string args []) {
Try {
// Create and Initialize the ORB
ORB ORB = Orb.init (args, null);
// Create the servant and register it with the orb
FileServant FileRef = New FileServant ();
Orb.connect (fileRef);
// Get the root naming context
Org.omg.corba.Object objref =
Orb.resolve_initial_references ("Nameservice");
NamingContext ncref = NamingContextHelper.narrow (Objref);
// bind the object reason in naming
NameComponent NC = New NameComponent ("FileTransfer", ""
Namecomponent path [] = {nc};
Ncref.rebind (path, fileref);
System.out.Println ("Server Started ....");
// Wait for Invocations from Clom Clom Clom Clom Clom Clom Clom Cliants
Java.lang.object sync = new java.lang.Object ();
Synchronized (sync) {
Sync.wait ();
}
} catch (exception e) {
System.err.Println ("Error:" E.GetMessage ());
E.PrintStackTrace (System.out);
}
}
}
Once FILeServer has an ORB (Object Request Agent), it can register the CORBA service. It registers with the COS naming service set by OMG. It starts from the naming service root, returns a generated CORBA object. For reference
NamingContext object, must Narrowed Down a suitable type. Do as follows:
NamingContext ncref = NamingContextHelper.narrow (Objref);
Object NCREF Object IS is now an instance of org.omg.cosnaming.namingContext. You can use it to register a Corba service that is clearly served by a substitute binding method.
Development client
The next step is to develop a client application to get naming services, access, and find other services. After getting a FileTransfer service, you can call the download method.
Code Sample 8: fileclientimport java.io. *;
Import java.util. *;
Import org.omg.cosnaming. *;
Import org.omg.corba. *;
Public class fileclient {
Public static void main (String Argv []) {
Try {
// Create and Initialize the ORB
ORB ORB = Orb.init (Argv, Null);
// Get the root naming context
Org.omg.corba.Object objref =
Orb.resolve_initial_references ("Nameservice");
NamingContext ncref = NamingContextHelper.narrow (Objref);
NameComponent NC = New NameComponent ("FileTransfer", ""
// resolve the object reference in naming
Namecomponent path [] = {nc};
FileInterfaceOperations FileRef =
FileInterfaceHelper.Narrow (ncref.resolve (path));
IF (argv.length <1) {
System.out.println ("USAGE: Java FileClient FileName);
}
// Save the file
File File = New File (Argv [0]);
Byte data [] = fileref.downloadfile (argv [0]);
BufferedoutputStream Output = New
BufferedOutputStream (New FileoutputStream (Argv [0]));
Output.write (Data, 0, Data.length);
Output.flush ();
Output.close ();
} catch (exception e) {
System.out.println ("FileClient Error:" E.getMessage ());
E.PrintStackTrace ();
}
}
}
Running application
The final step is to run the application. There are several steps:
1 Run the CORBA naming service. Use the command tnameserv. But the provincial port is 900, if 900 can not be used, you can use another port 2500, the command is as follows:
Prompt> TNameServ -orbinitialPort 2500
2 Running the server When there is a port
Prompt> Java FileServer
When other ports such as 2500
Prompt> Java FileServer -orbinitialPort 2500
3 Generate client dry code
Prompt> idlj -fclient fileInterface.idl
4 Run the client, assume it at 2500 port
Prompt> java fileclient hello.txt -orbinitialport 2500
Hello.txt is the file to download
If naming service is in 4500 port
Prompt> java fileclient hello.txt -orbinitialhost gosling -orbinitialport 4500
We can also specify some parameters in ORB initialization:
Orb orb = orb.init (argv, null); Specifies the CORBA server, and the named port is as follows:
Properties PROPS = New Properties ();
Props.Put ("Org.omg.Corba.orbinitialHost", "Gosling");
Props.Put ("ORB.OMG.CORBA.ORBINITIALPORT", "2500");
ORB ORB = Orb.init (args, props);
test
In file transfer applications, the client needs to know the name of the file to be transferred, and the server side does not have a list of ways to display the file name, you can increase the method extension application. Of course, you can also develop UI clients replace the command line. When the client starts, it calls the server-side method to display the file list, allows the user to select a file to download it. As shown below:
Figure 1: gui-based file transfer client
Comparison of CORBA and RMI
Obviously, RMI is simpler than CORBA, and developers don't need to be familiar with IDL. Typically, CORBA has the following differences from RMI:
Different interface definitions, CORBA uses IDL, while RMI uses Java.
Corba's parameters have input, output types.
CORBA design is independent of language. It is a bridge of different language development systems.
CORBA objects cannot be garbage collection. As we mentioned, CORBA has nothing to do with the language. Like C does not support garbage collection. This is the shortcomings of CORBA. Once we created a CORBA object, it will always exist and know that you go to it. And RMI is automatically collected.
in conclusion
We can develop distributed applications with RMI and CORBA. They are close to the first step definition object interface.
The development system RMI and CORBA do one dependent on the project needs. I hope this article can give you information about distributed applications and can give you some reference when selecting a distributed architecture.
In case of translation, please criticize and correct, thank you! My mail: lisong@nyi.com.cn