欢迎光临
我们一直在努力

CORBA例子(转)————————我也不知道是什麽东东:-)-JSP教程,J2EE/EJB/服务器

建站超值云服务器,限时71元/月

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

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

2. produce stub and skeleton files through idltojava.exe
   idltojava hello.idl
   idltojava is not include in the jdk. but you can download it from idldojava.

3. write a server program like this

// 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 "\nhello world !!\n";
    }   
  
}

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

4. write a client program like this
// 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[] = {nc};
            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);
    }
    }
}

5. complie these files

   javac *.java helloapp/*.java

6. run the application
   
  a. first youve to run the name service prior to the others likethis
     c:\>tnameserv
  b. run server
     c:\>java helloserver
  c. run client
     c:\>java helloclient

if you have problems with this example, do let me know!

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » CORBA例子(转)————————我也不知道是什麽东东:-)-JSP教程,J2EE/EJB/服务器
分享到: 更多 (0)

相关推荐

  • 暂无文章