下載并設置java platform這里就不詳細講了。
這里以一個java的corba實例說明corba這種c-s結構的程序的實現。
NB:這篇文章雖然是用java語言寫的,但是由于corba的多語言特性,idl文件即可以用java編譯器編譯,也可以使用其他支持corba的語言編譯例如c#. 把以下的java 格式改成c#風格照樣可以使用。而且c-s沒什么聯系,你也可以用c#寫server,用java寫client,這就是corba強悍的地方
!
1)首先要熟悉idlj 語言,這個是專門進行接口設計的語言,它與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的奧秘了。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
