简体   繁体   English

跟踪OSGi中的特定捆绑包

[英]Tracking specific Bundles in OSGi

I'm new using OSGi, I have a doubt about tracking Bundles. 我是OSGi的新手,我对跟踪捆绑软件有疑问。 I'm using BundleTracker because I want specific bundles, and I want to know which bundle I am using by their IDs. 我之所以使用BundleTracker,是因为我想要特定的捆绑软件,并且想通过其ID知道我正在使用哪个捆绑软件。 Some Bundles (3) implement the same interface. 一些捆绑软件(3)实现了相同的接口。 I want to track just those bundles that implement that interface. 我只想跟踪那些实现该接口的捆绑软件。 How can I do this? 我怎样才能做到这一点?

If you want to select a specific service you can use service properties for that. 如果要选择特定服务,则可以使用该服务的属性。 This is completely agnostic of bundles. 这与束完全无关。 What that means is that you register your service-implementations with specific properties (this is what the Dictionary -argument in BundleContext.registerService is for). 这意味着您使用特定属性注册了服务实现(这是BundleContext.registerServiceDictionary -argument的目的)。

Then you can use BundleContext.getServiceReferences and specify a filter-expression (the syntax is described here ) that only selects the service with the correct property. 然后,您可以使用BundleContext.getServiceReferences并指定仅选择具有正确属性的服务的filter-expression( 此处描述了语法)。 You could set this up so that you always get an array or collection with at most one element. 您可以进行设置,以便始终获得最多包含一个元素的数组或集合。

This all becomes much easier if you use declarative services , because then you can just specify the property and the filter in the service-descriptor of the providing and consuming component respectively. 如果使用声明式服务 ,这一切将变得更加容易,因为您可以分别在提供组件和使用组件的服务描述符中分别指定属性和过滤器。 You can also make one or both ends of it configurable via the configuration admin (properties in the configuration of a service-component are propagated as properties of the exposed services or consumed references). 您还可以通过配置管理器使其一端或两端都可配置 (服务组件配置中的属性作为公开服务或使用的引用的属性传播)。 I would really suggest you check out declarative services before starting any serious work in OSGi. 我真的建议您在开始OSGi中的任何重要工作之前先检查声明式服务。

Note that you should be careful to not implement a hard-coded wiring this way, because that would kill modularity. 请注意,您应注意不要以这种方式实施硬编码接线,因为那样会破坏模块化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM