java corba_Java Corba

1)首先要熟悉idl语言,这个是专门进行接口设计的语言,它与java没关系,有自己的语法,具体的规则需要大家自己再网上研究,这里不多说了(或者访问如下网站详细察看http://www.iona.com/support/docs/manuals/orbix/33/html/orbix33cxx_pguide/IDL.html)。

module HelloApp

{

interface Hello

{

string sayHello();

oneway void shutdown();

};

};

这 里定义了一个简单的interface, 将其保存为hello.idl, 然后再dos命令框里面输入 idlj.exe -fall hello.idl 编译。之后会出现一个叫做HelloApp的目录,corba就是通过这个目录里面的类来进行c-s之间的数据沟通。

2)下一步,就是我们的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 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 the

// 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

将其保存为HelloServer.java.放在刚才的hello.idl的目录。编译这个文件就不多说了。

3)还记得在hello中定义的interface吗?我们需要对自己定义的接口中的方法进行实现,因此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 of

// 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 //必须继承这个类,在helloApp目录中已自动生成

{

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

同样放在server所在目录中。

4)接下来是客户端(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

这个文件最好放在一个新建的目录,已表示和server有区别,放在一起也没有关系。如果分开的话,记着把HelloApp这个目录复制到client的目录来。

5)好啦!已经可以开始爽了,我们编译所有的java文件

6)再dos窗口输入orbd.exe –ORBInitialPort 1234(端口号可以自定义,但是记得s-c要保持一致),启动corba服务。

7)启动服务器:java HelloServer –ORBInitialPort 1234 –ORBInitialHost localhost

8)启动客户端:java HelloClient –ORBInitialPort 1234 –ORBInitialHost localhost

9)严格执行上述过程是应该直接成功的。 已经经过测试。

10)然后再仔细研究这段代码,你就会发现corba的奥秘了。From: http://www.javaeye.com/topic/136691

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值