本文介绍了存在多个实现时,在容器内对OSGi服务选择进行优先级排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OSGi,并且有一些捆绑包.捆绑软件A和B都包含实现单个接口的注册服务.第三束C包括用于查找实现前述接口的服务的代码. A和B捆绑软件具有不同的版本号,但是C似乎从第一个启动的捆绑软件中提取了服务.我已经更改了开始级别以执行正确的操作,但版本仅用于接受而不是返回服务的顺序.

I am playing with OSGi and have a few bundles. Bundle A and B both contain a registered service which implements a single interface. The third bundle C includes code to lookup a service implementing the previously mentioned interface. The A and B bundles have different version numbers, however it appears that C picks up the service from the first started bundle. I have changed the start level to do the right thing, but version is only used to accept rather than order which service is returned.

   A version 1.0 start level 1
   B version 1.1 start level 2
   C requires both bundles, start level 3

在上面的示例中,即使B具有更高的捆绑软件版本,C也会始终从A获得服务.但是,如果我切换启动级别,那么B在A之前启动,C看到了B服务.

In the above example C always gets the service from A even though B has a higher bundle version. However, if I switch the start level, so B starts before A, C sees the B service.

我已经搜索过OSGi网站,但没有明确说明是否使用捆绑软件的版本确定服务的优先级.我的理解似乎表明,应该使用启动级别来命令捆绑包启动,以便可以正确满足依赖关系.但是,它似乎过载,因此也优先考虑服务优先级.鉴于以上所有内容,我认为不要在选择中使用捆绑软件版本是有道理的,因为版本号只是相对于零的数字.

I have searched the OSGi website and there is no clear explaination of whether versioning of a bundle is used to prioritise a service over another. My understanding seems to indicate that start level is supposed to be used to order bundle startup so that dependencies can be satisified correctly. However it appears to be overloaded so that it also prioritises service priority. Given all the above, I guess it makes sense not to use the bundle version in selection because the version number is just a number relative to nothing.

除了启动级别之外,将一项服务优先于另一项服务的最佳方法是什么?

What is the best way to prioritise one service over another, besides start level?

推荐答案

确定OSGi服务的优先级以使用 SERVICE_RANKING 服务属性.可以在传递给的properties对象中提供此属性. BundleContext.registerService()方法.

The best way of prioritizing OSGi services to use SERVICE_RANKING service property. This property may be supplied in the properties object passed to the BundleContext.registerService() method.

根据 BundleContext.getServiceReference()方法:

如果排名并列,则该服务具有最低的服务ID(在其Constants.SERVICE_ID属性中指定);即,返回首先注册的服务.

If there is a tie in ranking, the service with the lowest service ID (as specified in its Constants.SERVICE_ID property); that is, the service that was registered first is returned.

这篇关于存在多个实现时,在容器内对OSGi服务选择进行优先级排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 05:26