`
snoopy7713
  • 浏览: 1123514 次
  • 性别: Icon_minigender_2
  • 来自: 火星郊区
博客专栏
Group-logo
OSGi
浏览量:0
社区版块
存档分类
最新评论

OSGi容器中Bundle之间Synchronous Communication

阅读更多

  OSGi Core定义了一个服务层,提供了一个Bundle之间交互的简单机制,通过注册Java Object 至OSGi service registry。      Blueprint Container

    (1) Blueprint Configuration

    默认配置文件位于:ProjectDir/src/main/resources/OSGI-INF/blueprint

    默认XML文件命名空间:

Xml代码   收藏代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < blueprint   xmlns = "http://www.osgi.org/xmlns/blueprint/v1.0.0" >   
  3.     ...  
  4. </ blueprint >   

     自定义配置文件地址:

     Bundle-Blueprint: lib/account.xml, security.bp, cnf/*.xml

 

     Mandatory dependencies:

     OSGi Service 中Dependencies默认是必须的,但可通过设置reference和reference-list元素的availability为optional

     来改变这中现象。正常情况下,当Bluepring Container初始化时,通过一定范围的时间来允许Dependencies

     Resolve, 该时间么哦人为:5秒,可通过下面的方式自定义:

Xml代码   收藏代码
  1. Bundle-SymbolicName: org.fusesource.example.osgi-client;  
  2. blueprint.graceperiod: = true ;  
  3. blueprint.timeout: 10000   

    (2) Defining a Service Bean

    (3) Exporting a Service

    1. Exporting with a single interface

Xml代码   收藏代码
  1. < blueprint   xmlns = "http://www.osgi.org/xmlns/blueprint/v1.0.0" >   
  2.     < bean   id = "savings"   class = "org.fusesource.example.SavingsAccountImpl"   />   
  3.     < service   ref = "savings"   interface = "org.fusesource.example.Account"   />   
  4. </ blueprint >   

     2. Exporting with multiple interfaces

Xml代码   收藏代码
  1. < blueprint   xmlns = "http://www.osgi.org/xmlns/blueprint/v1.0.0" >   
  2.       
  3.     < bean   id = "savings"   class = "org.fusesource.example.SavingsAccountImpl"   />   
  4.     < service   ref = "savings" >   
  5.         < interfaces >   
  6.             < value > org.fusesource.example.Account </ value >   
  7.             < value > org.fusesource.example.SavingsAccount </ value >   
  8.         </ interfaces >   
  9.     </ service >   
  10. </ blueprint >   

    注意:interface和interfaces不能同时在service元素中使用。

    3. Exporting with auto-export

    例如:下面的代码中Blueprint将自动注册所有SavingsAccountImpl实现的public接口:

Xml代码   收藏代码
  1. < blueprint   xmlns = "http://www.osgi.org/xmlns/blueprint/v1.0.0" >   
  2.     < bean   id = "savings"   class = "org.fusesource.example.SavingsAccountImpl"   />   
  3.     < service   ref = "savings"   auto-export = "interfaces"   />   
  4. </ blueprint >   

     其中auto-export默认值为:disabled,其他可选值为:

     interfaces:注册该类实现的所有public的接口;

     class-hierarchy:注册该类自己以及他的父类,但不包含Object类;

     all-classes:注册该类自己以及他的父类,同时包括该类实现的接口。

     4. Setting service properties

Xml代码   收藏代码
  1. < blueprint   xmlns = "http://www.osgi.org/xmlns/blueprint/v1.0.0" >   
  2.     < bean   id = "savings"   class = "org.fusesource.example.SavingsAccountImpl"   />   
  3.     < service   ref = "savings"   auto-export = "interfaces"   ranking = "10" >   
  4.         < service-properties >   
  5.             < beans:entry   key = "bank.name"   value = "HighStreetBank"   />   
  6.         </ service-properties >   
  7.     </ service >   
  8. </ blueprint >   

    说明:A.  注册的service默认的Properties有:osgi.service.blueprint.compname值为service  bean 的id

                B.  service.ranking 是系统自动设置。

                C.  当系统查找服务时返回对个匹配的服务时,默认选择ranking值最高的服务,因此可配置该属性用以服务

     之间的区分。

     5. Specifying a registration listener

Xml代码   收藏代码
  1. < blueprint   xmlns = "http://www.osgi.org/xmlns/blueprint/v1.0.0"  . >   
  2.     < bean   id = "listenerBean"   class = "org.fusesource.example.Listener"   />   
  3.     < service   ref = "savings"   auto-export = "interfaces" >   
  4.         < registration-listener   ref = "listenerBean"   
  5.             registration-method = "register"   unregistration-method = "unregister"   />   
  6.     </ service >   
  7. </ blueprint >   

 

Java代码   收藏代码
  1. //Java   
  2. package  org.fusesource.example;  
  3.   
  4. public   class  Listener {  
  5.     public   void  register(Account service, java.util.Map serviceProperties) {  
  6.     }  
  7.   
  8.     public   void  unregister(Account service, java.util.Map serviceProperties) {  
  9.     }  
  10. }  

     说明:register和unregister方法的参数,第一个参数可以是service class或service class 实现的接口,因为该

     参数包含service instance,  如果该service bean 被声明为property, 在注册的时候是没有service instance可用

     的,因此此时该参数的值为null.

     第二个参数可以是java.util.Map或java.util.Dictionary,这个Map包含service 注册时的Properties.

     (4) Importing a Service

     使用reference和reference-list元素Import Service, 两者之间的区别是:reference用于访问stateless service,

     而reference-list用于访问stateful service.

     1. Matching by interface (stateless)

Xml代码   收藏代码
  1. < blueprint   xmlns = "http://www.osgi.org/xmlns/blueprint/v1.0.0" >   
  2.     < reference   id = "savingsRef"   interface = "org.fusesource.example.SavingsAccount"   />   
  3.     < bean   id = "client"   class = "org.fusesource.example.client.Client" >   
  4.         < property   name = "savingsAccount"   ref = "savingsRef"   />   
  5.     </ bean >   
  6. </ blueprint >   

 

Java代码   收藏代码
  1. package  org.fusesource.example.client;  
  2.   
  3. import  org.fusesource.example.SavingsAccount;  
  4.   
  5. public   class  Client {  
  6.     SavingsAccount savingsAccount;  
  7.   
  8.     // Bean properties   
  9.     public  SavingsAccount getSavingsAccount() {  
  10.         return  savingsAccount;  
  11.     }  
  12.   
  13.     public   void  setSavingsAccount(SavingsAccount savingsAccount) {  
  14.         this .savingsAccount = savingsAccount;  
  15.     }  
  16. }  

     2.  Matching by interface (stateful)    

Xml代码   收藏代码
  1. < blueprint   xmlns = "http://www.osgi.org/xmlns/blueprint/v1.0.0" >   
  2.     < reference-list   id = "savingsListRef"   
  3.         interface = "org.fusesource.example.SavingsAccount"   />   
  4.     < bean   id = "client"   class = "org.fusesource.example.client.Client" >   
  5.         < property   name = "savingsAccountList"   ref = "savingsListRef"   />   
  6.     </ bean >   
  7. </ blueprint >   

 

Java代码   收藏代码
  1. package  org.fusesource.example.client;  
  2.   
  3. import  org.fusesource.example.SavingsAccount;  
  4.   
  5. public   class  Client {  
  6.     java.util.List<SavingsAccount> accountList;  
  7.   
  8.     // Bean properties   
  9.     public  java.util.List<SavingsAccount> getSavingsAccountList() {  
  10.         return  accountList;  
  11.     }  
  12.   
  13.     public   void  setSavingsAccountList(java.util.List<SavingsAccount> accountList) {  
  14.         this .accountList = accountList;  
  15.     }  
  16. }  

     3. Matching service properties with a filter

Xml代码   收藏代码
  1. < reference   id = "savingsRef"   interface = "org.fusesource.example.SavingsAccount"   filter = "(bank.name=HighStreetBank)"   />   
  2.   
  3. < reference-list   id = "savingsRef"   interface = "org.fusesource.example.SavingsAccount"   filter = "(bank.name=HighStreetBank)"   />   

     4. Specifying whether mandatory or optional

     默认值:mandatory

Xml代码   收藏代码
  1. < reference   id = "savingsRef"   interface = "org.fusesource.example.SavingsAccount"   availability = "mandatory"   />   

     5. Specifying a reference listener

Xml代码   收藏代码
  1. < blueprint   xmlns = "http://www.osgi.org/xmlns/blueprint/v1.0.0" >   
  2.     < reference   id = "savingsRef"   interface = "org.fusesource.example.SavingsAccount" >   
  3.         < reference-listener   bind-method = "onBind"   
  4.             unbind-meth od = "onUnbind" >   
  5.             < bean   class = "org.fusesource.example.client.Listener"   />   
  6.         </ reference-listener >   
  7.     </ reference >   
  8.     < bean   id = "client"   class = "org.fusesource.example.client.Client" >   
  9.         < property   name = "savingsAcc"   ref = "savingsRef"   />   
  10.     </ bean >   
  11. </ blueprint >   

 

Java代码   收藏代码
  1. package  org.fusesource.example.client;  
  2.   
  3. import  org.osgi.framework.ServiceReference;  
  4.   
  5. public   class  Listener {  
  6.     public   void  onBind(ServiceReference ref) {  
  7.         System.out.println("Bound service: "  + ref);  
  8.     }  
  9.   
  10.     public   void  onUnbind(ServiceReference ref) {  
  11.         System.out.println("Unbound service: "  + ref);  
  12.     }  

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics