`
zqc_0101
  • 浏览: 227100 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Declarative Services中的服务引用

    博客分类:
  • OSGI
阅读更多

当某个Component依赖某一服务的时候,这个服务很有可能会被在任何时候注销,框架对这种情况只是做了简化处理,开发人员必须关心这个事情,做出相应的处理。

 

target services:写入到reference元素中的服务。这个时候Component configuration并没有真正引用这个服务。

bound service:被绑定到Component configuration的target services。

 

1、Accessing Services

Component实例取得bound service有两种方式:

(1)Event strategy

当你在reference元素中指定policy属性为dynamic时,也应该同时在reference元素中指定bind和unbind属性,这两个属性指定绑定和取消绑定的方法。

 

(2)Lookup strategy

Component实例可以通过ComponentContext的locateService方法获取bound service。

 

在实际使用中,两个方法可以用其一,也可以都使用。

如果使用Event strategy,bind和unbind指定的方法应该具有下面的形式:

1 void <method-name>(ServiceReference);
2 void <method-name>(<parameter-type>);
3 void <method-name>(<parameter-type>, Map);

 

如果bind和unbind方法具有第一种形式,ServiceReference参数将被传递给locateService(String,ServiceReference)方法,以获取bound service。如果需要在访问bound service之前检查service properties,这个方法是很有用的。Event strategy允许bound service的延迟激活。

 

如果是第二种形式,则bound service的实例对象直接被传递给方法。

 

如果是第三种形式,则bound service的实例对象直接被传递给方法作为第一个参数,第二个参数Map包含了bound service的properties,并且这个Map是不能修改的。

 

对于每一个bound service,bind和unbind方法必须要被调用一次。这意味着如果Reference元素中的基数属性指定是N多个的,那么这两个方法可能会被调用很多次。

 

例如,一个Component需要调用Log Service,并且使用Lookup strategy,那么Reference元素就无须指定bind和unbind方法。

<?xml version="1.0" encoding="UTF-8"?>
<scr:component name="example.listen"
xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0">
<implementation class="com.acme.LogLookupImpl"/>
<reference name="LOG"
interface="org.osgi.service.log.LogService"/>
</scr:component>

 

Component的实现类必须去搜索服务,如下:

public class LogLookupImpl {
private void activate(ComponentContext ctxt) {
LogService log = (LogService)
ctxt.locateService("LOG");
log.log(LogService.LOG_INFO, "Hello Components!"));
}
}

 

 

另一种方法是使用Event strategy,如下:

<?xml version="1.0" encoding="UTF-8"?>
<scr:component name="example.listen"
xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0">
<implementation class="com.acme.LogEventImpl"/>
<reference name="LOG"
interface="org.osgi.service.log.LogService"
bind="setLog"
unbind="unsetLog"
/>
</scr:component>

Component的实现类,如下:

public class LogEventImpl {
private LogService log;
private void setLog( LogService l ) { log = l; }
private void unsetLog( LogService l ) { log = null; }
private void activate() {
log.log(LogService.LOG_INFO, "Hello Components!"));
}
}

 

 

2、Reference Cardinality

一个Component通常要指定引用服务的基数范围,这个基数有两方面的意思:

(1)Multiplicity(多重,多个)

例如,一个Component只需要使用到一个Log Service就可以了。但是,当the Configuration Admin使用Configuration Listener services时,它必须绑定所有在服务注册表里的此类服务,只有这样,它才可以正确分发事件。

 

(2)Optionality(可选择的)

如果bound service不存在,Component也可以正常工作吗?对于有些来说,这是可以的,但有些就不行,比如,有个程序存在一个Servlet,但是如果Http Service如果不存在,显然它是无法正常工作的。

 

cardinality可以被指定成下面四种:

• 0..1 – Optional and unary.(0个或1个)
• 1..1 – Mandatory and unary (Default) (强制必须有一个)
• 0..n – Optional and multiple.(0个或多个)
• 1..n – Mandatory and multiple.(1个或多个)

 

对于2和4两种情况,当激活Component Configuration时,必须至少存在一个可以引用的bound service。

如果cardinality只指定最多1个,但是存在多个target service情况,那么Component Configuration将去绑定某个service.ranking值最大的target service,如果service.ranking也一样,那么就去比较service.id(启动号),谁最低,就绑定谁(谁先启动先绑定谁)。

 

 

 

3、Reference Policy

如果Component的所有references都已具备,Component Configuration可以被激活并绑定这些服务。但是,OSGI的动态性可能会使得某个被绑定的target service已经又被修改、注册或者取消注册了,因此,Component应该指定一个策略来处理这些变化。

 

static策略是最简单的策略也是默认的策略。如果有一个target service可以去替换一个失效的target service,那么Component Configuration应该要被重新激活并绑定到替代的target service。如果Component引用的服务频繁地取笑注册和重新注册,或者激活和取笑激活一个Component Configuration花销很大,static策略的的开销将是很大的。如果cardinality指定的是多个services,static策略是不适用的。

 

dynamic策略就有点复杂了,因为Component必须正确处理变化。SCR可以修改bound services的设置而无须注销Component Configuration,如果Component使用event strategy访问服务,那么通过bind和unbind方法,Component实例将会被通知到bound service的变化。

 

像前面注册资源的例子,使用的是static策略,这表明如果Http Services的设置有所改变,那么Component Configuration必须要被注销。

 

选择Target Services

 

Circular References

最好不要去循环引用,即ComponentA和ComponentB不要互相引用。

分享到:
评论
1 楼 李嘉铭 2015-03-16  
你知道 Component可以当注解使用吗?像这样@Component(service=HTTPService.class,name="respon")?

相关推荐

    declarative

    declarative services

    Pure Declarative Programming in Swift, Among Other Things.zip

    Pure Declarative Programming in Swift, Among Other Things.zip,swift中的纯声明式编程

    Declarative Chrome Extension-crx插件

    -使用像这样的记录器服务logger.declarativeLog('DECLARATIVE TRACE-Some String%O',Object);-示例-`logger.declarativeLog('DECLARATIVE TRACE-Event:'+ event.name +'EventData:%O 3.声明式查看器以树格式...

    Declarative dependency management for Matlab.zip

    Declarative dependency management for Matlab.zip

    python SQLAlchemy的Mapping与Declarative详解

    主要介绍了python SQLAlchemy的Mapping与Declarative详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    declarative-shadow-dom:自定义元素以声明方式创建Shadow DOM

    &lt;declarative&gt; 自定义元素以声明方式创建Shadow DOM 它应该与给出的建议紧密合作演示版安装使用安装组件:$ bower install declarative-shadow-dom --save 或 。用法如果需要,内置的导入自定义元素可扩展polyfill&...

    Learning GraphQL: Declarative Data Fetching for Modern Web Apps

    Why is GraphQL the most innovative technology for fetching data since Ajax? By providing a query language for your APIs and a runtime for fulfilling queries with your data, GraphQL presents a clear ...

    Qt5Declarative_jll.jl

    Qt5Declarative_jll.jl (v5.15.2 + 0) 这是使用构造的自动生成的包。 原始的脚本可以在社区构建树上找到。 如果您有任何问题,请向Yggdrasil报告。 有关JLL软件包以及如何使用它们的更多详细信息,请参见...

    declarative-parser-0.1.zip

    declarative-parser.zip,将bean定义组成正则表达式的解析器,用于

    OSGI in Action

    with Declarative Services 357 ■ Consuming services with Declarative Services 359 ■ Declarative Services component lifecycle 364 11.4 Summary 371 12 Advanced component frameworks 373 12.1 Blueprint ...

    Python库 | wavestate.declarative-1.3.4.tar.gz

    python库。 资源全名:wavestate.declarative-1.3.4.tar.gz

    declarative_demo_f5

    这是一个通过Ansible部署F5声明性入职(DO)和应用程序服务3(AS3)扩展以声明方式提供BIG-IP的示例。 输出 高级剧本流程: 安装声明性入职 安装App Services 3 开机自检(板载BIG-IP) POST到AS3(LTM Config) ...

    declarative-demos:尝试以声明方式解决编程难题

    它使用作为辅助库,其思想源于以及Kevlin Henney关于的精彩演讲怎么跑git clone https://github.com/dbagia/declarative-demos.git yarn yarn test文献资料演示下的每个文件夹都有其自己的自述文件,以解释问题,...

    jquery-declarative:从 code.google.compjquery-declarative 自动导出

    jquery-declarative 与每个 jQuery 插件一起工作,而无需使用任何额外的 javascript。 在 jQuery 中,您可以使用类似的方法将控件分配给 HTML 元素: $ ( document ) . ready ( function ( ) { $ ( '#portfoio'...

    Declarative Chrome Extension (v2.2)-crx插件

    1.支持在声明树中的“数据解析器”上附加断点(可访问“组件”标签)2.支持在声明树中查看“数据提供程序”细节:扩展开发人员工具以进行声明性UI。声明性Chrome扩展用于调试基于Siemens Web框架的应用程序。 (注意...

    Declarative Chrome Extension (v2.1)-crx插件

    1.视图日志以下选项卡帮助您跟踪关键声明事件的执行流程(按时间顺序):-ViewModel Tracer:列出应用程序中加载的视图模型-Commands:列出执行的命令。 -事件:列出在应用程序生命周期中发生的事件。 -条件:列出...

    OSGi in Practice 完整版

    Neil Bartlett 2008年9月出炉的最新OSGi in Practice 的完整版,目录如下: Introduction ...Declarative Services The Extender Model Integrating Third-party Libraries Building Web Applications

    swift-declarative-configuration:对象的声明式配置

    Swift声明式配置 Swift声明式配置(简称SDC)是一个很小的库,使您能够以符合人体工程学的方式以声明式,一致且易于理解的方式配置对象。 它可用于配置任何平台上的任何对象,包括服务器端swift。...

Global site tag (gtag.js) - Google Analytics