【问题标题】:deactivate/disable OSGI component by name from external bundle从外部包中按名称停用/禁用 OSGI 组件
【发布时间】:2017-04-16 01:44:10
【问题描述】:

有没有办法通过名称来停用给定的 OSGI 组件?

componentContext.disableComponent(componentName) 方法 - 但它只适用于同一个包的组件。

在不向给定捆绑包添加新服务以停用组件的情况下执行此操作的最佳实践解决方案是什么?

解决方案:

当使用例如菲利克斯这将是:

import org.apache.felix.scr.ScrService;

@Reference
private ScrService serviceComponentRuntime;

  public void stopByName(final String componentName)
{
    final org.apache.felix.scr.Component[] components = serviceComponentRuntime.getComponents(componentName);

    for (final org.apache.felix.scr.Component component : components)
    {
        component.disable();
    }
}

【问题讨论】:

    标签: java osgi apache-felix


    【解决方案1】:

    您可以使用ServiceComponentRuntime 服务。它允许内省和管理任何组件。

    【讨论】:

      【解决方案2】:

      您可以通过组件上下文启用/禁用:

      @Component(service=ComponentEnabler.class)
      public class ComponentEnabler {
      
        ComponentContext context;
      
        @Activate
        void activate(ComponentContext context) {
           this.context = context;
        }
      
        public void enable( String name) {
          this.context.enableComponent(name);
        }
        public void disable( String name) {
          this.context.disableComponent(name);
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-26
        • 2012-10-31
        • 2015-09-04
        • 1970-01-01
        • 1970-01-01
        • 2021-10-29
        • 2016-02-03
        • 2023-04-02
        相关资源
        最近更新 更多