java - 如何获取所有当前注册的 OSGi 服务的列表?

标签 java osgi osgi-bundle

我的基于 Java 的项目当前正在 OSGi 容器中运行,其他项目在其中注册其 OSGi 服务以供其他项目使用。如果我知道当前已注册服务,我可以使用“BundleContext”内置方法简单地进行查询:

getContext().getServiceReference(LogService.class.getName());

但是,我希望能够查询当时在上下文中注册的所有 OSGi 服务。我研究了 BundleContext 的可用方法,但找不到任何适合我需求的方法。我不希望实际使用注册服务列表来使用服务对象,而只是获取注册服务的名称;我不认为这会违反 OSGi 原则。

编辑:我没有在我的开发环境中使用 Eclipse 或 Equinox。

编辑:如果使用 Java 不可能或不推荐访问 OSGi 服务,如果可能的话,我愿意通过命令行查看注册的服务。

最佳答案

根据API docs您可以调用 getAllServiceReferences(null, null) 方法来获取所有服务的列表:

getAllServiceReferences

ServiceReference<?>[] getAllServiceReferences(java.lang.String clazz,
                                             java.lang.String filter)
                                               throws InvalidSyntaxException

Returns an array of ServiceReference objects. The returned array of ServiceReference objects contains services that were registered under the specified class and match the specified filter expression.

Parameters:

  • clazz - The class name with which the service was registered or null for all services.
  • filter - The filter expression or null for all services.

关于java - 如何获取所有当前注册的 OSGi 服务的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45176211/

相关文章:

java - 如何将字符串转换为 fragment 文件名

java - Spring Integration 代理网关上的入站转换器

java - 类 FileReader 中的构造函数 FileReader 不能应用于给定类型;

java - JAXB 解码 XML 元素以映射

java - 如何在 WebSphere 上的 OSGi 企业应用程序中设置记录器?

osgi - 为什么吊带配置有两种不同的格式

java - 具有相同符号名称的 bundle 的 OSGi 包导出行为

compiler-errors - 使用OSGi服务时无法将获取服务解析为类型错误

java - 远程 OSGI 服务调用

java - 使用 javax.print 打印时选择 bin/tray