1)首先要熟悉idl語言,這個(gè)是專門進(jìn)行接口設(shè)計(jì)的語言,它與java沒關(guān)系,有自己的語法,具體的規(guī)則需要大家自己再網(wǎng)上研究,這里不多說了(或者訪問如下網(wǎng)站詳細(xì)察看http://www.iona.com/support/docs/manuals/orbix/33/html/orbix33cxx_pguide/IDL.html)。
module HelloApp
{
interface Hello
{
string sayHello();
oneway void shutdown();
};
};
這 里定義了一個(gè)簡單的interface, 將其保存為hello.idl, 然后再dos命令框里面輸入 idlj.exe -fall hello.idl 編譯。之后會出現(xiàn)一個(gè)叫做HelloApp的目錄,corba就是通過這個(gè)目錄里面的類來進(jìn)行c-s之間的數(shù)據(jù)溝通。
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的目錄。編譯這個(gè)文件就不多說了。
3)還記得在hello中定義的interface嗎?我們需要對自己定義的接口中的方法進(jìn)行實(shí)現(xiàn),因此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 //必須繼承這個(gè)類,在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
這個(gè)文件最好放在一個(gè)新建的目錄,已表示和server有區(qū)別,放在一起也沒有關(guān)系。如果分開的話,記著把HelloApp這個(gè)目錄復(fù)制到client的目錄來。
5)好啦!已經(jīng)可以開始爽了,我們編譯所有的java文件
6)再dos窗口輸入orbd.exe –ORBInitialPort 1234(端口號可以自定義,但是記得s-c要保持一致),啟動corba服務(wù)。
7)啟動服務(wù)器:java HelloServer –ORBInitialPort 1234 –ORBInitialHost localhost
8)啟動客戶端:java HelloClient –ORBInitialPort 1234 –ORBInitialHost localhost
9)嚴(yán)格執(zhí)行上述過程是應(yīng)該直接成功的。 已經(jīng)經(jīng)過測試。
10)然后再仔細(xì)研究這段代碼,你就會發(fā)現(xiàn)corba的奧秘了。From: http://www.javaeye.com/topic/136691
本博客為學(xué)習(xí)交流用,凡未注明引用的均為本人作品,轉(zhuǎn)載請注明出處,如有版權(quán)問題請及時(shí)通知。由于博客時(shí)間倉促,錯(cuò)誤之處敬請諒解,有任何意見可給我留言,愿共同學(xué)習(xí)進(jìn)步。