3

假设我使用 OSGi 声明式服务并且我有一个服务,该服务有一个带有 policy = dynamic 的数字引用...

A - 强制一元。

B - 强制一元。

C - 强制倍数。

D - 可选的一元。

E - 可选倍数。

当我的服务启动时,所有参考资料都可用。有什么方法可以控制调用绑定的顺序吗?

我想让 B 先绑定并对进来的每个 E 做一些事情,但我无法确保 B 在 E 之前绑定。

是的,更合乎逻辑的做法是让代表 B 的服务也绑定到 E 并做它应该做的任何事情,但我不能修改 B,我只能使用它。如果我创建一个只绑定到 B 和 E 的新服务,我仍然会遇到同样的问题。

当一切都被绑定时,我可以在activate方法中做任何我需要做的事情,然后在绑定额外的(动态)E时做它,但我想知道是否还有另一种方法......

4

1 回答 1

4

Make reference B use the "static" policy, which ensures it will be bound before the activate method is invoked.

If reference E is declared as multiple/optional with the "dynamic" policy -- which is pretty much the only reasonable choice when you have a multiple reference -- then it will be bound/unbound whenever the services are published/unpublished. This can happen from any thread, and can even happen (multiple times!) during the invocation of the activate method.

于 2013-07-10T18:56:03.103 回答