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
本博客为学习交流用,凡未注明引用的均为本人作品,转载请注明出处,如有版权问题请及时通知。由于博客时间仓促,错误之处敬请谅解,有任何意见可给我留言,愿共同学习进步。