Java CORBA instance (Author: zixi)

Source: Internet
Author: User

This article is similar to the tutorial provided by Sun. If there is a helloimpl Java file in the middle, the commands for compiling will be different.

The original command is: Server javac helloserver. Java helloapp/*. Java

Client javac helloclient. Java helloapp/*. Java

The current command can be viewed below

Download and set Java platform.

Here, we use a Java-based CORBA instance to describe the implementation of a C-s structure program.

NB: although this article was written in Java, due to the multi-language feature of CORBA, The IDL file can be compiled using the Java compiler, you can also use other languages that support CORBA for compiling, such as C #. you can change the following Java format to the C # style. In addition, C-S has no connection. You can also use C # To write the server and Java to write the client. This is a powerful part of the CORBA!

1) first, you must be familiar with the idlj language. It is a language specially designed for interfaces. It has nothing to do with Java and has its own syntax. You need to study the specific rules on the Internet, not much here (or visit the following website to view http://www.iona.com/support/docs/manuals/orbix/33/html/orbix33cxx_pguide/IDL.html in detail ).

Module helloapp
{

Interface hello
{
String sayhello ();
Oneway void Shutdown ();
};
};

A simple interface is defined here, saved as hello. IDL, and then input idlj.exe-fall hello. IDL in the doscommand box to compile. A directory named helloapp will appear later. The class in this directory is used to communicate data between C-s.

2) The next step is our server:

// A server for the hello object

Import helloapp .*;
Import org. Omg. cosnaming .*;
Import org. Omg. cosnaming. namingcontextpackage .*;
Import org. Omg. CORBA .*;
Import org. Omg. portableserver .*;
Import org. Omg. portableserver. PoA;
Import java. util. properties;

Public class helloserver {

Public static void main (string ARGs []) {
Try {
// Create and initialize the orb
Orb orb = orb. INIT (ARGs, null );

// Get reference to rootpoa & activate the poamanager
POA rootpoa =
(PoA) orb. resolve_initial_references ("rootpoa ");
Rootpoa. the_poamanager (). Activate ();

// Create servant and register it with the orb
Helloimpl = new helloimpl ();
Helloimpl. setorb (ORB );

// Get object reference from the servant
Org. Omg. CORBA. Object ref =
Rootpoa. servant_to_reference (helloimpl );

// And cast the reference to a CORBA reference
Hello href = hellohelper. Narrow (REF );

// Get the root naming context
// Nameservice invokes the transient Name Service
Org. Omg. CORBA. Object objref =
Orb. resolve_initial_references ("nameservice ");
// Use namingcontextext, which is part of
// Interoperable Naming Service (INS) specification.
Namingcontextext ncref =
Namingcontextexthelper. Narrow (objref );

// Bind the object reference in naming
String name = "hello1 ";
Namecomponent path [] = ncref. to_name (name );
Ncref. rebind (path, href );

System. Out. println
("Helloserver ready and waiting ...");

// Wait for invocations from clients
Orb. Run ();
}
 
Catch (exception e ){
System. Err. println ("error:" + E );
E. printstacktrace (system. Out );
}

System. Out. println ("helloserver exiting ...");
 
} // End main
} // End class

Save it as helloserver. java. In the hello. IDL directory just now. Compile this file.

3) Do you still remember the interface defined in hello? Therefore, helloimp. Java

// The servant -- Object implementation -- For the hello
// Example. Note that this is a subclass of hellopoa, whose
// Source file is generated from the compilation
// Hello. IDL using j2idl.

Import helloapp .*;
Import org. Omg. cosnaming .*;
Import org. Omg. cosnaming. namingcontextpackage .*;
Import org. Omg. CORBA .*;
Import org. Omg. portableserver .*;
Import org. Omg. portableserver. PoA;

Import java. util. properties;

Class helloimpl extends hellopoa // This class must be inherited and is automatically generated in the helloapp directory.

{
Private Orb orb;

Public void setorb (ORB orb_val ){
Orb = orb_val;
}

// Implement sayhello () method
Public String sayhello ()
{
Return "/nhello world !! /N ";
}

// Implement Shutdown () method
Public void Shutdown (){
Orb. Shutdown (false );
}
} // End class
Also in the directory where the server is located.

4) The following is the client (helloclient. Java ):

// A sample Java IDL object client application.
Import helloapp .*;
Import org. Omg. cosnaming .*;
Import org. Omg. cosnaming. namingcontextpackage .*;
Import org. Omg. CORBA .*;

Public class helloclient
{
Static Hello helloimpl;
String [] x = new string [6];
Public static void main (string ARGs []) {
Try {
// Create and initialize the orb
Orb orb = orb. INIT (ARGs, null );

System. Out. println ("orb initialised/N ");

// Get the root naming context
Org. Omg. CORBA. Object objref =
Orb. resolve_initial_references ("nameservice ");

// Use namingcontextext instead of namingcontext,
// Part of the interoperable Naming Service.
Namingcontextext ncref =
Namingcontextexthelper. Narrow (objref );
 
// Resolve the object reference in naming
String name = "hello1 ";
Helloimpl =
Hellohelper. Narrow (ncref. resolve_str (name ));

System. Out. println
("Obtained a handle on Server Object :"
+ Helloimpl );
System. Out. println (helloimpl. sayhello ());
Helloimpl. Shutdown ();

}
Catch (exception e ){
System. Out. println ("error:" + E );
E. printstacktrace (system. Out );
}
} // End main

} // End class

It is better to put this file in a new directory, which indicates there is a difference with the server, and it does not matter if it is put together. If they are separated, copy the helloapp directory to the client directory.

5) Okay! Now we can get started. Let's compile all the java files.

Input orbd.exe-orbinitialport 1234 in the DOS window (the port number can be customized, but remember that s-c must be consistent) to start the CORBA service.

7) Start the server: Java helloserver-orbinitialport 1234-orbinitialhost localhost

8) Start the client: Java helloclient-orbinitialport 1234-orbinitialhost localhost

9) Strict execution of the above process should be successful directly. It has been tested.

10) then, after carefully studying this code, you will discover the mysteries of CORBA.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.