by
9
7
406k
1
Top 1% !
Popular
Pearl of Wisdom
Nice
Tagged
Specified
OpenSource
Popularity: 1999th place
Created
Modified Mar 24, 2014

Published on:

ClassNameHelloServer
InputName0args
InputType0String
Languagejava
LicenseMIT_X11
MethodNamemain
OutputTypevoid

Simple CORBA java server

This code shows an example of simple corba server in java.

A CORBA server needs a local ORB object. 
The server instantiates an ORB and registers its servant objects so that the ORB can find the server when it receives an invocation for it. 

the server is a process that instantiates one or more servant objects. 
Each servant implements the interface generated by idltojava and performs the operations on that interface. 

The server needs a HelloServant object, defined below.

Note the server waits for client to invoke the remote method.
For such reason, Object.wait requires HelloServer to remain alive (though quiescent) until an invocation comes from the ORB. Because of its placement in main, after an invocation completes and sayHello returns, the server will wait again.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/32753/Simple-CORBA-java-server?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
// The package containing our stubs. import HelloApp.*; // HelloServer will use the naming service. import org.omg.CosNaming.*; // The package containing special exceptions thrown by the name service. import org.omg.CosNaming.NamingContextPackage.*; // All CORBA applications need these classes. import org.omg.CORBA.*; public class HelloServer { 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 HelloServant helloRef = new HelloServant(); orb.connect(helloRef); // Get the root naming context org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); NamingContext ncRef = NamingContextHelper.narrow(objRef); // Bind the object reference in naming NameComponent nc = new NameComponent("Hello", ""); NameComponent path[] = {nc}; ncRef.rebind(path, helloRef); // Wait for invocations from clients java.lang.Object sync = new java.lang.Object(); synchronized(sync){ sync.wait(); } } catch(Exception e) { System.err.println("ERROR: " + e); e.printStackTrace(System.out); } } } class HelloServant extends _HelloImplBase { public String sayHello() { return "\nHello world!!\n"; } }

Other Steps and Solutions

If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus