1)首先要熟悉idl语言,这个是专门进行接口设计的语言,它与java没关系,有自己的语法,具体的规则需要大家自己再网上研究,这里不多说了(或者访问如下网站详细察看)。
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
本博客为学习交流用,凡未注明引用的均为本人作品,转载请注明出处,如有凯发k8网页登录的版权问题请及时通知。由于博客时间仓促,错误之处敬请谅解,有任何意见可给我留言,愿共同学习进步。