|
Java™ by example!
|
|
|
How do I get started with Java and CORBA?
CORBA is an RPC (Remote Procedure Call) mechanism that does more than simple procedure calls. It understands the notion of Object. It allows objects written in one language to call other objects written in the same or other languages. This example will show you how to write a Java CORBA Server and Java CORBA Client using Java IDL in simple steps. The BookStore app will allow the client to invoke two methods, getName and getPrice, pass it an ISBN number and get the Name or Price of that book. 1) Write an IDL file
As each programming languages has a different way to declare method signatures and types, we need a way to describe them in a language-independent manner if we want them to interoperate with each other. The IDL (Interface Definition Language) allows you to do this. The IDL for our BookStore looks like this: (Create a directory BookStoreApp and save this IDL as BookInfo.idl)
A module can be seen as a Java Package. Inside the module, you can have several interfaces, that map to Java Interfaces. In an IDL interface, you specify what methods will be remotely exported. 2) Map the IDL file to Java
The tool idltojava (or idlj in 1.3) will take the idl file and create the necessary client stub and server skeleton files as well as helper classes. Note: a servant is a server implementation, a server is a Java app that creates a servant instance, registers it with the ORB, and waits for client invocations to pass them on to the servant.
This creates a directory called BookStore that contains 5 files:
3) Create the Servant, the implementation of the IDL interface
The servant (the implementation of the interface generated by idltojava) does the actual server-side work. It needs to subclass from the abstract base class _BookInfoImplBase, the class that implements the interface BookInfo. BookInfoImpl.java:
4) Create the Server
Before any client can invoke a CORBA object, an ORB needs to be instantiated and initialized. During initialization, you can set a specific ORB implementation and set the host and port the server will be listening on (more information). Parameters passed to main are simply pass on to the ORB init method.
BookInfoServer.java:
The name server maps Strings to object references. You give your servant a name, register it with the orb, get a reference to the name server, and bind your servant reference to the name you gave it.
Compile:
5) Create the client:
The client is simple. It first creates an instance of the ORB, and initializes it with possible command-line parameters (eg. Host and Port). Then it gets a reference to the name service, and uses this and the server string ID to get the reference to the actual server object. From that moment it can use this reference as it was a local object reference.
BookInfoClient.java:
6) Run!
- Start up the name server:
will start the name server and listen to the default port 900. You can specify a different port to listen to:
- Start the BookInfoServer:
If the name server resides on a different host or port, you can specify this:
- Start the BookInfoClient:
As with the server, you can specify the name server coordinates using ORBInitialHost and ORBInitialPort. Result:
Further Information
Author of answer: Joris Van den Bogaert
Comments to this answer are only viewable by members. Login or become a member!
|
|
|
|
|