服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java corba入门


below is a simple example of a corba program
download the source file 

<b>1. produce a idl file like this</b>
   hello.idl
   module helloapp {
     interface hello    {         
         string sayhello();
    };
  };

<b>2. produce stub and skeleton files through idltojava.exe</b>
   idltojava hello.idl
   idltojava is now named as idlj.exe and is included in the jdk. 

<b>3. write a server program like this </b>

// helloserver.java 
  
import helloapp.*;

import org.omg.cosnaming.*;
import org.omg.cosnaming.namingcontextpackage.*;
import org.omg.corba.*;

import java.io.*;
class helloservant extends _helloimplbase 
{
    public string sayhello()
    {
       return "hello world !!"; 
    }   
  
}

public class helloserver {

    public static void main(string args[])
    {
try{
    // create and initialize the orb
    orb orb = orb.init(args, null);

    // create 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[] = ;
    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);
}
    }
}    

<b>4. write a client program like this</b>
// helloclient.java
import helloapp.*;
import org.omg.cosnaming.*;
import org.omg.corba.*;

public class helloclient 
{
    public static void main(string args[])
    {
try{
    // create and initialize the orb
    orb orb = orb.init(args, null);

            // get the root naming context
            org.omg.corba.object objref = 
orb.resolve_initial_references("nameservice");
            namingcontext ncref = namingcontexthelper.narrow(objref);
            // test
            system.out.println("ok..");                
            // resolve the object reference in naming
            namecomponent nc = new namecomponent("hello", "");
            namecomponent path[] = ;
            hello helloref = hellohelper.narrow(ncref.resolve(path));

    // call the hello server object and print results
           //string oldhello = helloref.lastmessage();
           //system.out.println(oldhello);
    string hello = helloref.sayhello();
    system.out.println(hello);

} catch (exception e) {
    system.out.println("error : " + e) ;
    e.printstacktrace(system.out);
}
    }
}

<b>5. complie these files</b>

   javac *.java helloapp/*.java

<b>6. run the application</b>
   
  a. first you’ve to run the name service prior to the others likethis
     c:>tnameserv
  b. run server
     c:>java helloserver
  c. run client 
     c:>java helloclient

扫描关注微信公众号