deactivate/disable 来自外部包的 OSGI 组件名称
deactivate/disable OSGI component by name from external bundle
有没有办法通过名称停用给定的 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();
}
}
您可以使用ServiceComponentRuntime服务。它允许内省和管理任何组件。
您可以enable/disable通过组件上下文:
@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);
}
}
有没有办法通过名称停用给定的 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();
}
}
您可以使用ServiceComponentRuntime服务。它允许内省和管理任何组件。
您可以enable/disable通过组件上下文:
@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);
}
}