• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

CORBA例子(转)————————我也不知道是什麽东东:-)

发布: 2007-7-01 18:47 | 作者: admin | 来源: | 查看: 15次 | 进入软件测试论坛讨论

领测软件测试网 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 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

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

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网