Hello.idl
|
module HelloApp{
interface Hello {
string sayHello(in string message);
};
};
|
HelloImpl.java
|
/* * @author javamxj
(CSDN Blog) 创建日期 2004-12-27 */
import org.omg.CORBA.*;
import HelloApp.HelloPOA;
public class HelloImpl extends HelloPOA {
private ORB orb;
public void setORB(ORB orb_val) {
orb = orb_val;
}
/*
实现接口声明方法sayHello */
public String sayHello(String message) {
System.out.println("
我在CORBA的服务器端,客户端正在调用/'sayHello/'方法。 ");
System.out.println("Hello " + message);
return message;
}
}
|
HelloServer.java
|
/* * @author javamxj
(CSDN Blog) 创建日期 2004-12-27 */
import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;
public class HelloServer {
public static void main(String args[]) {
try {
/*
创建和初始化 ORB */
ORB orb = ORB.init(args, null);
/*
获取对RootPOA的引用,启动POAManager */
POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
rootpoa.the_POAManager().activate();
/*
创建一个实例并将其向 ORB 注册 */
HelloImpl h = new HelloImpl();
h.setORB(orb);
System.out.println("
将实例注册到ORB ");
/*
获取对服务端的对象引用 */
org.omg.CORBA.Object ref = rootpoa.servant_to_reference(h);
Hello href = HelloHelper.narrow(ref);
/*
从名称服务中获取根元素名称上下文 */
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
/*
在Hello名称下注册新对象 */
String name = "Hello";
NameComponent path[] = ncRef.to_name(name);
ncRef.rebind(path, href);
/*
等待客户端的调用。 */
orb.run();
System.out.println("
等待CORBA客户端调用...");
}
catch (Exception e) {
System.err.println("ERROR: " + e);
e.printStackTrace(System.out);
}
System.out.println("HelloServer Exiting ...");
}
}
|
HelloClient.java
|
/* * @author javamxj
(CSDN Blog) 创建日期 2004-12-27 */
import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CORBA.*;
public class HelloClient {
public static void main(String args[]) {
try {
/*
创建和初始化 ORB */
ORB orb = ORB.init(args, null);
/*
获取根命名上下文 */
org.omg.CORBA.Object objRef = orb .resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
/*
在名称上下文中查找Hello对象,获取对它的引用。 */
String name = "Hello";
Hello h = HelloHelper.narrow(ncRef.resolve_str(name));
System.out.println("
我在客户端,开始调用CORBA服务器端的/'sayHello/'方法");
System.out.println("
欢迎, " + h.sayHello("javamxj blog"));
} catch (Exception e) {
System.out.println("
错误 : " + e);
e.printStackTrace(System.out);
}
}
}
|
==============JACORB2.3,20140307===========
在2.3版本下编译IDL语法有变
变成:idl *,或者idl hello.idl
同时为了易于管理,设置环境变量时,如下
JACORB_HOME=E:\Java\JacORB2.3
CLASSPATH=.;%JACORB_HOME%/lib/antlr-2.7.2.jar;%JACORB_HOME%/lib/avalon-framework-4.1.5.jar;%JACORB_HOME%/lib/concurrent-1.3.2.jar;%JACORB_HOME%/lib/idl.jar;%JACORB_HOME%/lib/jacorb.jar;%JACORB_HOME%/lib/logkit-1.2.jar;%JACORB_HOME%/lib/wrapper-3.0.3.jar;%JACORB_HOME%/classes;
=====END=====