OSGi 在注入列表时是否强制执行服务排名顺序
Does OSGi enforce service ranking order when injecting a list
当在服务中使用 @Reference
来注入对多个服务 (ReferenceCardinality.MULTIPLE
) 实现的引用时,OSGi 对注入服务的排序方式有何保证?它们总是按 service.ranking
.
排序吗
目前我在做以下事情:
@Reference(
service = SomeInterface.class,
cardinality = ReferenceCardinality.MULTIPLE,
policy = ReferencePolicy.DYNAMIC,
policyOption = ReferencePolicyOption.GREEDY,
fieldOption = FieldOption.UPDATE
)
private final List<SomeInterface> refs = new CopyOnWriteArrayList<>();
(注释为 OSGi R7 注释)
测试这个时,我总是看到列表 refs
中的元素按其 service.ranking
的降序排列,但我不知道这是设计使然还是意外,以及 OSGi 是否保证了这一点或不。我还没有在规范或任何博客中找到明确的答案 post。
此外,当 policy
、policyOption
或 fieldOption
使用不同的值时,这是否成立?
由于您提供自己的列表(而不是让声明式服务 (DS) 提供列表),DS 只能在您的列表对象上调用 add
和 remove
。参见 https://docs.osgi.org/specification/osgi.cmpn/7.0.0/service.component.html#d0e37871。
由于 DS 仅调用您的列表对象,因此您的列表实现必须负责任何所需的排序。
如果您让 DS 提供列表,则列表将始终按顺序排列:“根据服务排名和服务 ID 使用与 ServiceReference.compareTo
相同的顺序排序”。参见 https://docs.osgi.org/specification/osgi.cmpn/7.0.0/service.component.html#d0e37828。
当在服务中使用 @Reference
来注入对多个服务 (ReferenceCardinality.MULTIPLE
) 实现的引用时,OSGi 对注入服务的排序方式有何保证?它们总是按 service.ranking
.
目前我在做以下事情:
@Reference(
service = SomeInterface.class,
cardinality = ReferenceCardinality.MULTIPLE,
policy = ReferencePolicy.DYNAMIC,
policyOption = ReferencePolicyOption.GREEDY,
fieldOption = FieldOption.UPDATE
)
private final List<SomeInterface> refs = new CopyOnWriteArrayList<>();
(注释为 OSGi R7 注释)
测试这个时,我总是看到列表 refs
中的元素按其 service.ranking
的降序排列,但我不知道这是设计使然还是意外,以及 OSGi 是否保证了这一点或不。我还没有在规范或任何博客中找到明确的答案 post。
此外,当 policy
、policyOption
或 fieldOption
使用不同的值时,这是否成立?
由于您提供自己的列表(而不是让声明式服务 (DS) 提供列表),DS 只能在您的列表对象上调用 add
和 remove
。参见 https://docs.osgi.org/specification/osgi.cmpn/7.0.0/service.component.html#d0e37871。
由于 DS 仅调用您的列表对象,因此您的列表实现必须负责任何所需的排序。
如果您让 DS 提供列表,则列表将始终按顺序排列:“根据服务排名和服务 ID 使用与 ServiceReference.compareTo
相同的顺序排序”。参见 https://docs.osgi.org/specification/osgi.cmpn/7.0.0/service.component.html#d0e37828。