Java源码:CORBA入门
2008-04-09 03:59:58来源:互联网 阅读 ()
本文出自:www.chinajavaworld.com 作者: jdeveloper
Below is a simple example of a CORBA program
download the source file
1. produce a idl file like this
hello.idl
module HelloApp {
interface Hello {
string sayHello();
};
};
2. produce stub and skeleton files through idltojava.exe
idltojava hello.idl
idltojava is now named as idlj.exe and is included in the JDK.
3. write a server program like this
// HelloServer.java
import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.io.*;
class HelloServant extends _HelloImplBase
{
public String sayHello()
{
return "\nHello world !!\n";
}
}
public class HelloServer {
public static void main(String args[])
{
try{
// create and initialize the ORB
ORB orb = ORB.init(args, null);
// create servant and register it with the ORB
HelloServant helloRef = new HelloServant();
orb.connect(helloRef);
// get the root naming context
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(objRef);
// bind the Object Reference in Naming
NameComponent nc = new NameComponent("Hello", "");
NameComponent path[] = {nc};
ncRef.rebind(path, helloRef);
// wait for invocations from clients
java.lang.Object sync = new java.lang.Object();
synchronized (sync) {
sync.wait();
}
} catch (Exception e) {
System.err.println("ERROR: " e);
e.printStackTrace(System.out);
}
}
}
4. write a client program like this
// HelloClient.java
import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CORBA.*;
public class HelloClient
{
public static void main(String args[])
{
try{
// create and initialize the ORB
ORB orb = ORB.init(args, null);
// get the root naming context
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(objRef);
// test
System.out.println("OK..");
// resolve the Object Reference in Naming
NameComponent nc = new NameComponent("Hello", "");
NameComponent path[] = {nc};
Hello helloRef = HelloHelper.narrow(ncRef.resolve(path));
// call the Hello server object and print results
//String oldhello = helloRef.lastMessage();
//System.out.println(oldhello);
String Hello = helloRef.sayHello();
System.out.println(Hello);
} catch (Exception e) {
System.out.println("ERROR : " e) ;
e.printStackTrace(System.out);
}
}
}
5. complie these files
javac *.java HelloApp/*.java
6. run the application
a. first you've to run the Name Service prior to the others likethis
c:\>tnameserv
b. run server
c:\>java HelloServer
c. run client
c:\>java HelloClient
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:RSA加密实例分析
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash