Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4024992
  • 博文数量: 626
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 11080
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-23 13:08
文章分类

全部博文(626)

文章存档

2015年(72)

2014年(48)

2013年(506)

分类: Java

2013-09-30 09:41:41

Java&CORBA编程实例2

 要熟练掌握CORBA也并不容易。本文再提供一个Java与CORBA编程的例子,以进一步加深对CORBA的认识。

 

一、编写IDL文件

counter.idl源码:

 

  1. module CounterApp{  
  2.     interface Counter{  
  3.         readonly attribute long value;  
  4.         void inc();  
  5.         void dec();  
  6.     };  
  7. };  


 

二、用idlj生成需要的文件

命令:idlj -fall counter.idl

生成的文件如下图所示:

文件结构

 

三、编写服务器端文件

1)编写Server.java

Server.java源码:


  1. package server;  
  2. import CounterApp.*;  
  3.   
  4. import java.util.Properties;  
  5. import org.omg.CORBA.*;  
  6. import org.omg.PortableServer.POA;  
  7. import org.omg.PortableServer.POAHelper;  
  8. import java.io.*;  
  9. import static java.lang.System.*;  
  10.   
  11. public class Server {  
  12.     public static void main(String[] args){  
  13.         try{  
  14.             Properties props = getProperties();  
  15.             ORB orb = ORB.init(args, props);  
  16.             org.omg.CORBA.Object obj = null;  
  17.             POA rootPOA = null;  
  18.             try{  
  19.                 obj = orb.resolve_initial_references("RootPOA");  
  20.                 rootPOA = POAHelper.narrow(obj);  
  21.             }catch(org.omg.CORBA.ORBPackage.InvalidName e){  
  22.                   
  23.             }  
  24.             CounterImpl c_impl = new CounterImpl();  
  25.             Counter c = c_impl._this(orb);  
  26.             try{  
  27.                 FileOutputStream file = new FileOutputStream("Counter.ref");  
  28.                 PrintWriter writer = new PrintWriter(file);  
  29.                 String ref = orb.object_to_string(c);  
  30.                 writer.println(ref);  
  31.                 writer.flush();  
  32.                 file.close();  
  33.                 out.println("Server started."+" Stop: Ctrl-c");  
  34.             }catch(IOException ex){  
  35.                 out.println("File error: "+ex.getMessage());  
  36.                 exit(2);  
  37.             }  
  38.             rootPOA.the_POAManager().activate();  
  39.             orb.run();  
  40.         }catch(Exception ex){  
  41.             out.println("Exception: "+ex.getMessage());  
  42.             exit(1);  
  43.         }  
  44.     }  
  45. }  


2)编写CounterImpl.java

CounterImpl.java源码:


  1. package server;  
  2. import CounterApp.*;  
  3.   
  4. public class CounterImpl extends CounterPOA {  
  5.     private int count;  
  6.     public CounterImpl(){  
  7.         count = 0;  
  8.     }  
  9.     public void inc(){  
  10.         count++;  
  11.     }  
  12.     public void dec(){  
  13.         count--;  
  14.     }  
  15.     public int value(){  
  16.         return count;  
  17.     }  
  18. }  


 

四、编写客户端文件

Client.java源码:


  1. package client;  
  2. import CounterApp.*;  
  3.   
  4. import java.util.*;  
  5. import java.io.*;  
  6. import org.omg.CORBA.*;  
  7. import static java.lang.System.*;  
  8. public class Client {  
  9.     public static void main(String[] args){  
  10.         try{  
  11.             Properties props = getProperties();  
  12.             ORB orb = ORB.init(args, props);  
  13.             String ref = null;  
  14.             org.omg.CORBA.Object obj = null;  
  15.             try{  
  16.                 Scanner reader = new Scanner(new File("Counter.ref"));  
  17.                 ref = reader.nextLine();  
  18.             }catch(IOException ex){  
  19.                 out.println("File error: "+ex.getMessage());  
  20.                 exit(2);  
  21.             }  
  22.             obj = orb.string_to_object(ref);  
  23.             if(obj == null){  
  24.                 out.println("Invalid IOR");  
  25.                 exit(4);  
  26.             }  
  27.             Counter c = null;  
  28.             try{  
  29.                 c = CounterHelper.narrow(obj);  
  30.             }catch(BAD_PARAM ex){  
  31.                 out.println("Narrowing failed");  
  32.                 exit(3);  
  33.             }  
  34.             int inp = -1;  
  35.             do{  
  36.                 out.print("Counter value: "+c.value()+"/nAction(+/-/e)?");  
  37.                 out.flush();  
  38.                 do{  
  39.                     try{  
  40.                         inp = in.read();  
  41.                     }catch(IOException ioe){}  
  42.                 }while(inp != '+' && inp != '-' && inp != 'e');  
  43.                 if(inp == '+')  
  44.                     c.inc();  
  45.                 else if(inp == '-')  
  46.                     c.dec();  
  47.             }while(inp != 'e');  
  48.         }catch(Exception ex){  
  49.             out.println("Exception: "+ex.getMessage());  
  50.             exit(1);  
  51.         }  
  52.     }  
  53. }  


 

五、整个项目结构图

如图所示:

项目结构图

 

六、运行程序:

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

 

七、运行结果截图:

服务器端截图:

服务器端截图
 

 客户端截图:

 客户端截图

 

阅读(1653) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~