esuslogo
 [To advertise Java(tm) Events here, contact joris@esus.com!]
banner

Java™
by example!






New @ Esus.com


  gb  In-house search engine for better results!

  gb  Get updates with the esus.com
newsletter!









  Home 
 Browse Categories 
 Ask a Java Question 
 Help 
  For Java Tips & Tricks, subscribe to the esus.com newsletter!
Search Java Q&A, Links, API's:   adv 

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)

This code sample is only viewable to esus.com members
Login or become a member!


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 code sample is only viewable to esus.com members
Login or become a member!


This creates a directory called BookStore that contains 5 files:


This code sample is only viewable to esus.com members
Login or become a member!


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:

This code sample is only viewable to esus.com members
Login or become a member!


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:

This code sample is only viewable to esus.com members
Login or become a member!

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:

This code sample is only viewable to esus.com members
Login or become a member!


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:

This code sample is only viewable to esus.com members
Login or become a member!


6) Run!

- Start up the name server:

This code sample is only viewable to esus.com members
Login or become a member!

will start the name server and listen to the default port 900. You can specify a different
port to listen to:

This code sample is only viewable to esus.com members
Login or become a member!

- Start the BookInfoServer:

This code sample is only viewable to esus.com members
Login or become a member!

If the name server resides on a different host or port, you can specify this:

This code sample is only viewable to esus.com members
Login or become a member!

- Start the BookInfoClient:

This code sample is only viewable to esus.com members
Login or become a member!

As with the server, you can specify the name server coordinates using ORBInitialHost and ORBInitialPort.

Result:

This code sample is only viewable to esus.com members
Login or become a member!




Further Information
Author of answer: Joris Van den Bogaert

Comments to this answer are only viewable by members. Login or become a member!





Terms of Service | Privacy Policy | Contact

Copyright © 2000-2003 Esus.com - All Rights Reserved 
Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. Esus.com is independent of Sun Microsystems, Inc. All other trademarks are the sole property of their respective owners.