Java&CORBA编程实例2
要熟练掌握CORBA也并不容易。本文再提供一个Java与CORBA编程的例子,以进一步加深对CORBA的认识。
一、编写IDL文件
counter.idl源码:
-
module CounterApp{
-
interface Counter{
-
readonly attribute long value;
-
void inc();
-
void dec();
-
};
-
};
二、用idlj生成需要的文件
命令:idlj -fall counter.idl
生成的文件如下图所示:
三、编写服务器端文件
1)编写Server.java
Server.java源码:
-
package server;
-
import CounterApp.*;
-
-
import java.util.Properties;
-
import org.omg.CORBA.*;
-
import org.omg.PortableServer.POA;
-
import org.omg.PortableServer.POAHelper;
-
import java.io.*;
-
import static java.lang.System.*;
-
-
public class Server {
-
public static void main(String[] args){
-
try{
-
Properties props = getProperties();
-
ORB orb = ORB.init(args, props);
-
org.omg.CORBA.Object obj = null;
-
POA rootPOA = null;
-
try{
-
obj = orb.resolve_initial_references("RootPOA");
-
rootPOA = POAHelper.narrow(obj);
-
}catch(org.omg.CORBA.ORBPackage.InvalidName e){
-
-
}
-
CounterImpl c_impl = new CounterImpl();
-
Counter c = c_impl._this(orb);
-
try{
-
FileOutputStream file = new FileOutputStream("Counter.ref");
-
PrintWriter writer = new PrintWriter(file);
-
String ref = orb.object_to_string(c);
-
writer.println(ref);
-
writer.flush();
-
file.close();
-
out.println("Server started."+" Stop: Ctrl-c");
-
}catch(IOException ex){
-
out.println("File error: "+ex.getMessage());
-
exit(2);
-
}
-
rootPOA.the_POAManager().activate();
-
orb.run();
-
}catch(Exception ex){
-
out.println("Exception: "+ex.getMessage());
-
exit(1);
-
}
-
}
-
}
2)编写CounterImpl.java
CounterImpl.java源码:
-
package server;
-
import CounterApp.*;
-
-
public class CounterImpl extends CounterPOA {
-
private int count;
-
public CounterImpl(){
-
count = 0;
-
}
-
public void inc(){
-
count++;
-
}
-
public void dec(){
-
count--;
-
}
-
public int value(){
-
return count;
-
}
-
}
四、编写客户端文件
Client.java源码:
-
package client;
-
import CounterApp.*;
-
-
import java.util.*;
-
import java.io.*;
-
import org.omg.CORBA.*;
-
import static java.lang.System.*;
-
public class Client {
-
public static void main(String[] args){
-
try{
-
Properties props = getProperties();
-
ORB orb = ORB.init(args, props);
-
String ref = null;
-
org.omg.CORBA.Object obj = null;
-
try{
-
Scanner reader = new Scanner(new File("Counter.ref"));
-
ref = reader.nextLine();
-
}catch(IOException ex){
-
out.println("File error: "+ex.getMessage());
-
exit(2);
-
}
-
obj = orb.string_to_object(ref);
-
if(obj == null){
-
out.println("Invalid IOR");
-
exit(4);
-
}
-
Counter c = null;
-
try{
-
c = CounterHelper.narrow(obj);
-
}catch(BAD_PARAM ex){
-
out.println("Narrowing failed");
-
exit(3);
-
}
-
int inp = -1;
-
do{
-
out.print("Counter value: "+c.value()+"/nAction(+/-/e)?");
-
out.flush();
-
do{
-
try{
-
inp = in.read();
-
}catch(IOException ioe){}
-
}while(inp != '+' && inp != '-' && inp != 'e');
-
if(inp == '+')
-
c.inc();
-
else if(inp == '-')
-
c.dec();
-
}while(inp != 'e');
-
}catch(Exception ex){
-
out.println("Exception: "+ex.getMessage());
-
exit(1);
-
}
-
}
-
}
五、整个项目结构图
如图所示:
六、运行程序:
1)启动orbd
start orbd -ORBInitialPort 1050 -ORBInitialHost localhost
2)开始Server服务器
java server/Server -ORBInitialPort 1050 -ORBInitialHost localhost
注:如在同一台主机上运行,可省略-ORBInitialHost localhost
3)运行客户端应用程序
java client/Client -ORBInitialPort 1050 -ORBInitialHost localhost
七、运行结果截图:
服务器端截图:
客户端截图:
阅读(1741) | 评论(0) | 转发(0) |