OSGi DS和手动停用组件 [英] OSGi DS & deactivating a component manually

查看:82
本文介绍了OSGi DS和手动停用组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用声明式服务,手动停用OSGi中的组件是否有效?

Is it valid to deactivate a component in OSGi manually if I am using Declarative Services?

例如,假设我有一个具有实现的组件:

For example, let's say I have a component, with the implementation:

//component
class Overseer(){
       List<ServiceReference> serviceRefs = ...//populate

       private void doStuff(){
            serviceRef = serviceRefs[i];
            if(dontNeedThisAnymore){
               serviceRefs.remove(serviceRef);
               serviceRef.getBundle().stop();

            }
 }

推荐答案

做到这一点的最佳方法是使用ComponentContext API从同一捆绑软件中的另一个组件中获取.

The best way to do this is from another component in the same bundle, using the ComponentContext API.

您可以编写一个组件,以使其将ComponentContext用作其Activate方法的参数.该接口具有enableComponentdisableComponent方法,可用于启用/禁用同一包中的其他组件.

You can write a component so that it takes ComponentContext as a param to its activate method. That interface has enableComponent and disableComponent methods that can be used to enable/disable other components in the same bundle.

我将其称为关守"组件,因为它可以用于在启用其他组件之前设置其他组件所需的资源.例如,您可能有多个组件,需要先启动数据库才能执行其工作...网守将负责启动数据库,然后调用enableComponent(null)启用其他组件.同样,如果网守可以检测到数据库已关闭,然后禁用其他组件.为了使它起作用,捆绑包中除网闸之外的所有组件都需要首先设置为enabled="false".

I call this a "gatekeeper" component because it can be used to setup resources needed by the other components before enabling them. For example, you may have multiple components that need a database to be started up before they can do their job... the gatekeeper would take care of starting the database and then call enableComponent(null) to enable the other components. Similarly if the gatekeeper could detect that the database has shutdown and at that point disable the other components. In order for this to work, all components in the bundle except for the gatekeeper need to be set initially to enabled="false".

这篇关于OSGi DS和手动停用组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆