我的基于 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/