这里是对上一篇blog: http://winseclone.iteye.com/blog/1786637 的补充。说明DS声明服务XML中reference节点属性的作用。

 

这里reference的几个属性cardinality和policy是比较难理解的。

    cardinality(基数),该属性是DS为啥这么牛逼的真正所在之一。该属性控制Service数量的上下限,达到这个下限后该Component才可用。

    这里说一下“上限”,假设选的是0..1或1..1,就算你的运行时注册了n个Service,框架也只会选择其中一个!至于真正使用那个,不是很确定!所以最好不要这样用!

  • 0..1: optional and singular, "zero or one"
  • 1..1: mandatory and singular, "exactly one"
  • 0..n: optional and multiple, "zero to many"
  • 1..n: mandatory and multiple, "one to many" or "at least one"

上限是1的情况下,找到一个Service后就被break了!所以,在这种“基数”的配置下,不推荐注册多个Service。如果需要运行时替换服务,首先stop原来的服务插件,然后再start新服务的插件。

在0..1或1..1的策略下,也不推荐在Component(Helper)的插件同时绑定一个Service实现,不利于服务的替代!

 

DS的reference属性解析
 

 

    policy(策略)可选值为:staticdynamic, 静态和动态的意思。如果是静态的话,如果Service重新加载,Component也会重新加载。而dynamic只会调用相应的setService()和unsetService()方法。

 

动态dynamic策略 写道
osgi> register user-service : [email protected]
activate [email protected]

osgi> ls
All Components:
ID State Component Name Located in bundle
1 Active sample.http.ds.UserHelper sample.http(bid=41)
2 Active sample.http.en.userService sample.http.ds.user.en(bid=44)
3 Active sample.http.cn.userService sample.http.ds.user.cn(bid=45)

osgi> stop 45
register user-service : [email protected]
unregister user-service : [email protected]

 注意: 这里的顺序!先register后再unregister!所以原来的UserHelper需要进行修改才行,不然,动态变更后,service就变成null咯!

 

静态static策略 写道
osgi> register user-service : [email protected]
activate [email protected]

osgi> ls
All Components:
ID State Component Name Located in bundle
1 Active sample.http.ds.UserHelper sample.http(bid=41)
2 Active sample.http.en.userService sample.http.ds.user.en(bid=44)
3 Active sample.http.cn.userService sample.http.ds.user.cn(bid=45)

osgi> stop 45
deactivate [email protected]
unregister user-service : [email protected]
register user-service : [email protected]
activate [email protected]

   

从上面两个从Console命令窗口中的可以看到,dynamic策略在Service注册后,不会deactivate钝化Component。而static策略会先钝化Component后在unregsiter注销Service。

 

相关文章:

  • 2022-12-23
  • 2021-08-22
  • 2021-07-07
  • 2021-07-08
  • 2022-01-16
  • 2021-07-19
  • 2021-08-05
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-05-17
  • 2022-02-21
  • 2021-10-29
相关资源
相似解决方案