手机版
你好,游客 登录 注册
背景:
阅读新闻

使用Java进行CORBA编程-JacORB-Event Service

[日期:2012-01-11] 来源:Linux社区  作者:fw0124 [字体:大 中 小]
Client侧(Consumer)
1)创建一个类,继承自PushConsumerPOA,这个类就是Client侧的Consumer的实现类。
   这个类的push()方法,在Event Channel发送事件通知过来的时候会被调用。
   在这里添加对事件通知的处理代码。
2)向NameService查询并获得Event Channel的引用(EventChannel对象),查询时需要提供Server侧注册时使用的Channel名。
3)调用EventChannel对象的for_consumers()方法,得到一个ConsumerAdmin对象。
4)调用ConsumerAdmin对象的obtain_push_supplier()方法,得到一个ProxyPushSupplier对象。
5)创建1)中的Consumer的实现类的一个实例。
   在上面4)得到的ProxyPushSupplier对象上调用connect_push_consumer()方法,
   把Consumer的实现类的实例传给此方法。
6)现在Client侧也已经连接到Event Channel。
   这时候如果Server侧(Supplier)发送事件通知,Consumer的实现类的push()方法就会被调用了。

下面提供一个例程,运行此例子需要启动NameService,
启动方法和例程的启动方法见 http://www.linuxidc.com/Linux/2012-01/51262.htm

Server代码
1)PushSupplierImpl

[Java]
  1. package learning.corba.event.newsbroadcast.server;  
  2.   
  3. import org.omg.CosEventComm.PushSupplierPOA;  
  4.   
  5. public class PushSupplierImpl extends PushSupplierPOA {  
  6.   
  7.     public void disconnect_push_supplier() {  
  8.         System.out.println("disconnect_push_supplier");  
  9.     }  
  10.   
  11. }  

2)NewsBCServer

[java]
  1. package learning.corba.event.newsbroadcast.server;  
  2.   
  3. import java.util.Properties;  
  4.   
  5. import org.jacorb.events.EventChannelImpl;  
  6. import org.omg.CORBA.Any;  
  7. import org.omg.CORBA.ORB;  
  8. import org.omg.CosEventChannelAdmin.ProxyPushConsumer;  
  9. import org.omg.CosEventChannelAdmin.SupplierAdmin;  
  10. import org.omg.CosEventComm.Disconnected;  
  11. import org.omg.CosEventComm.PushSupplierHelper;  
  12. import org.omg.CosNaming.NamingContextExt;  
  13. import org.omg.CosNaming.NamingContextExtHelper;  
  14. import org.omg.PortableServer.POA;  
  15. import org.omg.PortableServer.POAHelper;  
  16.   
  17. public class NewsBCServer implements Runnable {  
  18.   
  19.     private ORB orb;  
  20.     private ProxyPushConsumer proxyPushConsumer;  
  21.   
  22.     public static void main(String[] args) {  
  23.         try {  
  24.             Properties props = new Properties();  
  25.             props.put("org.omg.PortableInterceptor.ORBInitializerClass.bidir_init",  
  26.                       "org.jacorb.orb.giop.BiDirConnectionInitializer");  
  27.               
  28.             ORB orb = ORB.init(args, props);  
  29.             POA rootPoa = POAHelper.narrow(  
  30.                     orb.resolve_initial_references("RootPOA"));  
  31.             rootPoa.the_POAManager().activate();  
  32.                   
  33.             NamingContextExt ncExt = NamingContextExtHelper.narrow(  
  34.                     orb.resolve_initial_references("NameService"));  
  35.               
  36.             // Create event channel   
  37.             EventChannelImpl eventChannel = new EventChannelImpl(orb, rootPoa);  
  38.             ncExt.rebind(ncExt.to_name("NewsBC.EventChannel"),  
  39.                     rootPoa.servant_to_reference(eventChannel));  
  40.               
  41.             // Obtain the push consumer proxy object   
  42.             PushSupplierImpl pushSupplierImpl = new PushSupplierImpl();  
  43.             SupplierAdmin supplierAdmin = eventChannel.for_suppliers();  
  44.             ProxyPushConsumer proxyPushConsumer = supplierAdmin.obtain_push_consumer();  
  45.             proxyPushConsumer.connect_push_supplier(  
  46.                     PushSupplierHelper.narrow(rootPoa.servant_to_reference(pushSupplierImpl)));  
  47.               
  48.             Thread t = new Thread(new NewsBCServer(orb, proxyPushConsumer));  
  49.             t.start();  
  50.               
  51.             orb.run();  
  52.               
  53.         } catch (Exception e) {  
  54.             e.printStackTrace();  
  55.         }  
  56.     }  
  57.       
  58.     public NewsBCServer(ORB orb, ProxyPushConsumer proxyPushConsumer) {  
  59.         this.orb = orb;  
  60.         this.proxyPushConsumer = proxyPushConsumer;  
  61.     }  
  62.   
  63.     public void run() {  
  64.         int i = 0;  
  65.           
  66.         try {  
  67.             while(!Thread.interrupted()) {  
  68.                 Any event = this.orb.create_any();  
  69.                 event.insert_long(i++);  
  70.                   
  71.                 this.proxyPushConsumer.push(event);  
  72.                 Thread.sleep(3000);  
  73.             }  
  74.         } catch (InterruptedException e) {  
  75.             e.printStackTrace();  
  76.         } catch (Disconnected e) {  
  77.             e.printStackTrace();  
  78.         }  
  79.           
  80.         this.proxyPushConsumer.disconnect_push_consumer();  
  81.         this.orb.shutdown(true);  
  82.     }  
  83. }  

Client代码
1)PushConsumerImpl

[java]
  1. package learning.corba.event.newsbroadcast.client;  
  2.   
  3. import org.omg.CORBA.Any;  
  4. import org.omg.CORBA.ORB;  
  5. import org.omg.CosEventChannelAdmin.ProxyPushConsumer;  
  6. import org.omg.CosEventChannelAdmin.ProxyPushSupplier;  
  7. import org.omg.CosEventComm.Disconnected;  
  8. import org.omg.CosEventComm.PushConsumerPOA;  
  9.   
  10. public class PushConsumerImpl extends PushConsumerPOA {  
  11.       
  12.     private ORB orb;  
  13.     private ProxyPushSupplier proxyPushSupplier;  
  14.     private int eventCount = 0;  
  15.       
  16.     public PushConsumerImpl(ORB orb, ProxyPushSupplier proxyPushSupplier) {  
  17.         this.orb = orb;  
  18.         this.proxyPushSupplier = proxyPushSupplier;  
  19.     }  
  20.       
  21.     public void disconnect_push_consumer() {  
  22.         System.out.println("disconnect_push_consumer");  
  23.     }  
  24.   
  25.     public void push(Any event) throws Disconnected {  
  26.         System.out.println("event:" + event.extract_long());  
  27.         if (eventCount++ > 10) {  
  28.             proxyPushSupplier.disconnect_push_supplier();  
  29.             orb.shutdown(false);  
  30.         }         
  31.     }  
  32. }  

2)NewsBCClient

[java]
  1. package learning.corba.event.newsbroadcast.client;  
  2.   
  3. import java.util.Properties;  
  4.   
  5. import org.omg.CORBA.ORB;  
  6. import org.omg.CosEventChannelAdmin.ConsumerAdmin;  
  7. import org.omg.CosEventChannelAdmin.EventChannel;  
  8. import org.omg.CosEventChannelAdmin.EventChannelHelper;  
  9. import org.omg.CosEventChannelAdmin.ProxyPushSupplier;  
  10. import org.omg.CosEventComm.PushConsumer;  
  11. import org.omg.CosEventComm.PushConsumerHelper;  
  12. import org.omg.CosNaming.NamingContextExt;  
  13. import org.omg.CosNaming.NamingContextExtHelper;  
  14. import org.omg.PortableServer.POA;  
  15. import org.omg.PortableServer.POAHelper;  
  16.   
  17. public class NewsBCClient {  
  18.     public static void main(String[] args) {  
  19.         try {  
  20.             Properties props = new Properties();  
  21.             props.put("org.omg.PortableInterceptor.ORBInitializerClass.bidir_init",  
  22.                       "org.jacorb.orb.giop.BiDirConnectionInitializer");  
  23.             ORB orb = ORB.init(args, props);  
  24.               
  25.             NamingContextExt ncExt = NamingContextExtHelper.narrow(  
  26.                     orb.resolve_initial_references("NameService"));  
  27.               
  28.             EventChannel eventChannel = EventChannelHelper.narrow(  
  29.                     ncExt.resolve_str("NewsBC.EventChannel"));  
  30.               
  31.               
  32.             ConsumerAdmin consumerAdmin = eventChannel.for_consumers();  
  33.             ProxyPushSupplier proxyPushSupplier = consumerAdmin.obtain_push_supplier();  
  34.   
  35.             POA rootPoa = POAHelper.narrow(orb  
  36.                     .resolve_initial_references("RootPOA"));  
  37.             rootPoa.the_POAManager().activate();  
  38.             PushConsumerImpl pushConsumerImpl = new PushConsumerImpl(orb, proxyPushSupplier);  
  39.             PushConsumer pushConsumer = PushConsumerHelper.narrow(  
  40.                     rootPoa.servant_to_reference(pushConsumerImpl));  
  41.             proxyPushSupplier.connect_push_consumer(pushConsumer);  
  42.               
  43.             orb.run();            
  44.         } catch (Exception e) {  
  45.             e.printStackTrace();  
  46.         }  
  47.     }  
  48. }  
linux
相关资讯       JAVA基础教程 
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* 问题 发表于 2012/8/8 17:01:02
运行不了