RMI and CORBA are often considered competing from each other because both provide transparent access to remote distributed objects. But both technologies are actually complementary, and the length of one can make up for the shortcomings of the other. The combination of RMI and CORBA produces RMI-IIOP, and RMI-IIOP is the basis for the development of Java, corporate server. In this article, Java Developer Damian Hagge briefly introduces RMI-IIOP, and then show you how to build and run a simple, Java-based RMI-IIOP client / server application. Please take a look at how much RMI can work on IIOP.
In 1997, IBM and Sun Microsystems launched a cooperative program aimed at promoting Java as a development technology development technology. Two companies have especially strive to use Java as a server-side language, generate enterprise code that can combine the existing architecture. What you need is a remote transmission technology, which has less resources for Java's RMI (Remote Method Invocation, Remote Method Call) and more mature CORBA (Common Object Request Broker Architecture, public object requesting agency architecture) Technical robustness. For this need, RMI-IIOP came out, which helped push Java language to the leading position of the mainstream language developed by server-side companies.
In this article, I will briefly introduce RMI-IIOP, the goal is to enable you to start using this technology in an enterprise development solution. To explain what RMI-IIOP is, I think it is important to provide information about Corba and RMI, which may not be found in a typical introduction of various technologies. If you are not familiar with the basics of CORBA or RMI, I suggest you read some introductory information before you read it. Please refer to the reference, there are some articles and tutorials.
Before I specifically discuss RMI-IIOP, we will first look at CORBA and RMI to use the mechanism to request data for requests. CORBA will be our main example because RMI-IIOP data is built on the CORBA Transfer Protocol (IIOP). We will review this transport protocol and ORB (Object Request Broker, Object Request Agent) to send requests, locate remote objects, and transfer objects on the network.
Remote object transfer The CORBA request is done by using the IIOP protocol. Briefly, IIOP will represent a series of bytes in any IDL (Interface Definition Language, Interface Definition Language) constructed in a standardized format. Then assume that there is a Java client to assign a CORBA request to the C server. The client application has a reference to the remote object in the form of a Java interface, and calls an operation of the interface. Essentially, the interface calls it to achieve the corresponding implementation of the operation, this implementation will be located in the stub (STUB) (stub).
The stub assists the method call to the ORB, and the ORB consists of two parts: client ORB and server ORB. The responsibility of the client ORB is to be encapsulated in request, put it on the network, and pass the specific location. The role of server ORB is to listen to the requests uploaded from the network and convert these requests into language-enabled methods to be understood. To learn more about the role of CORBA ORB, see the reference part.
After the stub assigns method call, the client ORB converts the request and all parameters to a standardized byte format in this case. Next, the request is sent to the server ORB through the wire, and the server orb should be listening to the incoming request. The server-side ORB will read the byte of the data and convert the request to the C server to make meaningful things. The C server method will perform its function (ie, call the requested method) and use the same mechanism to return the result to the client through IIOP. RMI handles the request in a similar manner, but it uses JRMP (Java Remote Messaging Protocol, Java Remote Messaging Protocol) as its transfer protocol. Of course, RMI transmission also relates to serialization of Java objects.
Differences in CORBA and RMI
Corba is running on the IIOP protocol; RMI uses JRMP. Corba is independent of language; RMI is pure Java to Java. RMI uses JNDI to locate the remote object; CORBA uses cosnaming. RMI will sequence the object; CORBA is not.
Remote object Positioning CORBA uses the COSNAMING naming service to locate the remote object. CosNaming provides a framework for the Name Server's binding (or reference) for the CORBA server process. When the CORBA client sends a CosNaming request to the Name Service, the name service returns the interoperable object reference for the process (Interoperable Object Reference (IOR). Next, the client uses the IOR directly to communicate with the server process.
The iOR contains information about the server process, such as the location of the server process. One of the shortcomings of cosnaming services is that IOR is difficult to understand to humans - at least for those who do not have electronic brains. Conversely, RMI is friendly for users. It uses a registry that runs over JNDI (very similar to named service very similar) to locate the remote object. The RMI Registry uses the Java Reference object (which consists of several RefadDR objects) to identify and locate the remote object. These Java objects are more friendly than IOR.
Not long ago, Cobra combines an interoperable name service (INTEROPERABLE NAMING Service (InS)) into its object-location (Object-Location) mode. INS runs on cosnaming, using human can read URLs as its object location. INS does not use a naming service; vice versa, it will call directly to the specified URL. See the reference information to learn more about INS.
RMI is so better about CORBA: Is CORBA or RMI? The answer depends on what you want to do. CORBA is a large-architecture that runs on a third or fourth-generation protocol running in the industry standard. If all attachments provided by CORBA (eg transaction, security interceptors, event channels, more), CORBA appears to be a solution for enterprise applications. The biggest disadvantage of CORBA is that it is complicated. To be skilled using CORBA, developers usually have to experience steep training curves.
Conversely, RMI is relatively easy to learn. Create a client / server implementation, bind to registry and remote objects, quite simple to use RMI calls and / or reception requests. RMI resources are much smaller than CORBA, because JRMP is a significant agreement than IIOP. However, RMI lacks CORBA industrial grade attachments, and is a pure Java-based mechanism. So, what we really need is the flexibility and ease of use of RMI, and CORBA's business is ready, right? Then start discussing RMI-IIOP. Why is RMI-IIOP?
RMI-IIOP has the intensity of CORBA and the flexibility of RMI. Developers can easily use RMI-IIOP, RMI-IIOP is also easy to integrate into most enterprise infrastructure.
RMI-IIOP Overview RMI-IIOP allows you to run RMI calls on the IIOP only with minimal modification. With RMI-IIOP, you can write simple and easy-to-understand Java code while using Corba's rich corporate function kit. Moreover, the flexibility of the code is large enough, which can be run on the RMI or IIOP. This means that your code can run in a pure Java environment (when a small resource occupancy quantity and flexibility is critical, or integrated into an existing CORBA infrastructure after making a small amount of modification to the code.
One of RMI-IIOP is very powerful is that it allows you to write a pure Java client / server implementation without losing the flexibility of RMI sequence. The RMI-IIOP is connected by overriding Java serialization and converting Java classes into IIOP on the wire. On the other, the Java class is read from the wire as IIOP, then create a new instance of this class (reflection), all the values of all members of the class is complete - Oh: This is the java serialization on IIOP!
In order to enable RMI-IIOP to achieve transparent object positioning, the ORB vendor has used Java CosNaming service providers (or said in addition to pedestrians). The plugin works under the JNDI API to access the CORBA naming service. Although I didn't have a flower space here, this naming solution is not ideal. As a result, many suppliers - especially the application server supplier - develops a special object positioning mechanism for RMI-IIOP.
RMI-IIOP also supports an extended INS as a Java cosnaming service. Because I believe that INS will determine the future direction of the object positioning, so we use INS in this article.
Note: Because Sun has not fully followed the OMG INS standard, the register_initial_reference of the org.omg.corba.orb interface has not been publicly publiced, so the source code provided herein will not work with Sun JDK. You will need IBM Developer Kit for Java Technology, version 1.3.1 or later. However, I have created an example of using a named service with Sun compatible, you can download it from the reference part.
I have to build RMI-IIOP and say enough, let's write code! In the following sections, we will build a simple, Java-based client / server RMI-IIOP application. This application consists of three parts: RMI interface, server application, and client application. Examples are characterized by Java sequences above IIOP, so you can see how Java classes are instantiated by clients, how to pass to the server, change by server, and then pass all modifications to the client. Part 1: Defining the interface Under RMI-IIOP, we can choose to use RMI or IDL to define interfaces. Because we want to see how RMI is running on the IIOP, we will use the RMI definition example interface. Listing 1 is our simple example RMI interface:
Listing 1. RMIINTERFACE.JAVA
/ *
* Remote Interface
* /
Public Interface RMiinterface Extends Java.rmi.Remote {
Public string hello () throws java.rmi.remoteException;
Public Serclass Alterclass (Serclass ClassObject)
Throws java.rmi.remoteexception;
}
RMiInterface defines a Hello () method and an AlterClass (Serclass) method. The latter method is used by Serclass, and Serclass is a Java class that implements the Serializable, the AlTerClass (Serclass) method returns the same type of class with its parameters. Serclass is a simple class with several members, each with a corresponding Getter method. These methods are shown in Listing 2:
Listing 2. Serclass.java
/ **
* This class is intended to be serialized over RMI-IIOP.
* /
Public Class Serclass Implements Java.io.Serializable {
//Members
Private int x;
PRIVATE STRING mYString;
// conncture
Public Serclass (int X, string mystring)
Throws java.rmi.remoteexception {
THIS.X = X;
THISTRING = mystring;
}
// Some Accessor Methods
Public int getX () {return x;}
Public void setx (int x) {this.x = x;}
Public string getString () {return myString;
Public void setstring (string str) {mystring = str;}
}
This is all our simple interface. Now let's study the server class.
Part 2: Building a server We will use a server class (Server.java) that acts as both the RMiInterface implementation class and the main method (to launch our service). Server.java inherits Javax.rmi.PortableRemoteObject. Thus, it contains all the features needed to bind yourself as a Remote interface to ORB and start listening requests. Listing 3 is the code of the server:
Listing 3. Server.java
/ *
* Simple Server * /
Import java.util. *;
Import java.rmi.remote;
Import java.rmi.remoteexception;
Import javax.rmi.portableremoteObject;
Import javax.rmi.corba.tie;
Import javax.rmi.corba.util;
Import Org.omg.PortableServer.poA;
Import Org.omg.PortablesRver. *;
Import Org.omg.PortableServer.Servant;
Import org.omg.corba.orb;
Public Class Server Extends PortableRemoteObject
Implements rmiinterface {
// Must Explicitly Create Default Constructionor
// To Throw RemoteException
Public server () throws remoteexception {
}
// Implementation of rmiInterface methods
Public string hello () throws remoteException {
Return "Hello there!";
}
Public Serclass Alterclass (Serclass ClassObject)
Throws remoteException {
// Change The Values of Serclass and Return IT.
// Add 5 to x
ClassObject.setX (
ClassObject.getx () 5);
// ALTER THE STRING
ClassObject.setstring
ClassObject.getstring () ": i've altered you");
Return ClassObject;
}
Public static void main (String [] args) {
Try {
// Create the orb passing in the port to listen on
Properties PROPS = New Properties ();
Props.PUT ("com.ibm.corba.listenerport", "8080");
ORB ORB = Orb.init (args, props);
// instantiate the server
// this Will Automatic or Call ExportObject (this)
Server s = new server ();
// now get the stub for ur server object -
// this Will Be Both
// a transote interface and an org.omg.corba.object
Remote r = portableremoteObject.tostub (s);
// register the process Under the name
// by which it can be found
(com.ibm.corba.iiop.orb) orb).
Register_initial_reference ("OURLITTLECLIENT",
(org.omg.corba.object) r);
System.out.println ("Hello Server Waiting ...");
// it's what Easy - // We're Registered and Listening for incoming requests
Orb.run ();
} catch (exception e) {
E.PrintStackTrace ();
}
}
}
Hey, what happened here? The code of the server application is very long, then let's talk about it separately. First, as mentioned earlier, the Server class implements RMIINTERFACE and provides all the methods for it. You can see the Hello () method of the RMIinterface at the front part of the code and the implementation of the AlterClass (SerClass) method. The Hello () method is just returning to the string "Hello there!". Alterclass (Serclass) method uses the Serclass object as a parameter, modify the value of the member, and then returns a new object - all through RMI-IIOP.
The main method of Server.java initializes an ORB. This ORB will be set to 8080's com.ibm.corba.listenerPort attribute as a parameter. This will cause the ORB to listen to the incoming request on port 8080. Please note that com.ibm.corba.listenerport is a proprietary IBM property. If you want to run these code on another supplier's ORB, you should refer to the vendor's documentation and find the appropriate properties. (Sun uses com.sun.corba.poa.orbPersistentServerport, but it can only work only when you use the PoA (Portable Object Adapter, the Portable Object Adapter) Servo (servant).)
After initializing the ORB, the main method then instantiates the Server object. Because this Server object is also a portableremoteObject, the default constructor automatically calls ExportObject (this). This object is now ready to receive remote calls.
Then, we need to register this object by calling orb.register_initial_reference (string, orb.omg.corba.object). To do this, we need to put our Server object as a reference to org.omg.corba.object. This point is called by calling PortableRemoteObject.tostub (s) because the object returned has achieved java.rmi.Remote and Org.omg.Corba.Object.
Then, the returned org.omg.corba.Object object is registered as "OURLITTLETLIENT" to the server-side ORB. To ensure that the INS request is able to locate the object, we use registration to call register_initial_reference. When the INS calls into ORB, the ORB will look for an object that has been registered with the name being requested. Since we registered objects as "OURLITTLETLETLET, so when an INS call enters our server ORB requires" outtleclient ", we will know which object is looking for.
Finally, I am sure that you have noticed that we enforce ORB into com.ibm.corba.iiop.orb. Because Sun has not publicly public register_initial_reference, IBM SDK can not disclose it. Therefore, we must force our ORB to enhance our ORB into IBM ORB. As Sun is increasingly following OMG, JDK's future version (1.4.0) will not need this forced transformation. That's it! Very simple - um, it is a bit. Our server is now waiting to pass the client INS request. But how about the client?
Part 3: Building a client client application code as shown in Listing 4:
Listing 4. Client.java
/ *
* Client Application
* /
Import javax.rmi.portableremoteObject;
Import org.omg.corba.orb;
Public class client {
Public static void main (String [] args) {
Try {
ORB ORB = Orb.init (args, null);
// here's the url for the local host
String insurl =
"CORBALOC: IIOP: 1.2@localhost: 8080 / OURLITTLECLIENT";
// Get the reference to the remote process
Org.omg.corba.object objref = orb.string_to_Object (insurl);
// Narrow It Into Our RMiInterface
RMIINTERFACE RI =
(RMIINTERFACE) PortableremoteObject.narrow (Objref, RMiInterface.class);
// Call the Hello Method
System.out.println ("Received from Server:" ri.hello () "/ n");
// Try RMI Serialization
Serclass se = new Serclass (5, "Client String!");
// Pass the class to be altered on the server
// of course behind the screnes this class is being
// Serialized over Iiop
SE = ri.alterclass (se);
// Now let's see the result
System.out.Println ("Serialization Results: / N"
"Integer WAS 5 Now IS" se.getx () "/ n"
String Was / "Client String! /"
Now IS / "" se.getstring() "/ "");
} catch (exception e) {
E.PrintStackTrace ();
}
}
}
How to Decompose the client code client code is simpler than the server code. We initialize an ORB and call String_to_Object (String), where String is our INS URL. Constructing the INS URL is quite simple: First, we specify that we use the CORBALOC URL (see Resources) and IIOP Protocol version 1.2. Next, we add the host name (www.whaatever.com) and the port to which you want to connect. Finally, we specify the name of the service we have to find. Results Ins URL is Corbaloc: IIOP: 1.2@localhost: 8080 / OURLITTLETLET. When we pass this URL to orb.string_to_Object (string), the ORB will assign a request to the specified server to request the requested service. Suppose everything is normal, then the ORB will receive an object reference to the service (actually an IOR). Then, we will quote the object to the mandatory transformation (Narrow), which we can use, that is, RMIInterface, which we prepare for the start of the calling method.
After calling a simple Hello method (it should not need any explanation), we can start to explore the serialization function of RMI-IIOP. First, we create a serclass, a serialized Java class, and initialize its member variables. Next, we will pass this class into our method, and the method is written to the server via IIOP. The server reads into the class and re-creates it as a server-side Java object, modifies its member value, and then returns it (using IIOP) as the return value of the method. When it is received after the remote method calls, we see that its members have been modified by the server. It is so simple: Java serialization is performed on the IIOP.
Part 4: Run examples Please note that the examples of us created here must be run in IBM Developer Kit for Java Technology, version 1.3.1 or later. If you prefer to use Sun JDK, download it specific to Sun-specific source code, you should run it in Sun 1.4.0 JDK or later. This source code includes an readme.txt file that explains the difference between the IBM SDK version and the Sun JDK version. If you don't have IBM Developer Kit for Java Technology (and you want one), please download one now; they are free.
Here is the step of running example:
Download the source file. Enter javac * .java, all files in Javac. Run the RMIC for the Server class: RMIC-IIOP Server. Start the server: In Windows, enter Start Java Server. Start the client: In Windows, enter Start Java Client.
Little Note About RMI-IIOP and EJB components EJB 2.0 specification points to the EJB component must be able to run on RMI and RMI-IIOP. Add RMI-IIOP as an online protocol for EJB components, has brought great help to integrate J2EE environments to existing enterprise infrastructure (mostly Corba fairly dense). But it also caused some problems.
Simply put, it is to integrate customized components and EJB components asking you (developer) to process the Plumbing, otherwise they will be abstracts for you in the EJB architecture. So far, there is no simple solution to this problem, may never have. With the development of technologies such as web services, solutions may have occurred, but it is not known. Conclusion: What should I do later I hope this article is how easy it is to demonstrate to build and run the RMI-IIOP client / server application. You can modify the examples we use, replace the client or server with pure CORBA, but do this will remove Java serialization in your application.
If you want to use RMI-IIOP in a CORBA environment, see how the IDL maps to Java and how Java maps to IDL. If you want to deploy RMI-IIOP in an unsafe environment (ie not your own PC), study CORBA security features (such as interceptors and CORBA security models) and other CORBA corporate functions (such as transaction processing) are good idea. All of CORBA can be used when you run RMI-IIOP.
Reference