我正在使用 Felix Configuration Admin用于读取和应用 OSGi 服务配置文件的库。我配置的许多服务都是第三方的(例如 org.ops4j.pax.web.pax-web-jetty 和 org.ops4j.pax.url.mvn) 并使用简单的 BundleActivator
而不是声明式服务。我发现这些服务每个都初始化两次因为
- 在激活时调用
ManagedService#updated(null)
,并且 - 不久之后,Felix
ConfigurationManager.UpdateThread
异步调用ManagedService#update(non-null)
。
我讨厌延迟应用我的配置。由于固有的竞争条件,它会导致不稳定的故障。是否有替代的 CM 实现可以同步应用配置来避免此问题?或者我可以让 Felix 同步吗? (从源代码和 ManagedService javadoc 的检查来看,似乎没有。)
最佳答案
实际上,从另一个线程回调到 update()
是 Config Admin 规范的要求。请参阅 R4 纲要规范的第 104.5.3 节:
The updated(Dictionary) callback from the Configuration Admin service to the Managed Service must take place asynchronously. This requirement allows the Managed Service to finish its initialization in a synchronized method without interference from the Configuration Admin service call- back.
不幸的是,这意味着您需要对 ManagedService 进行编码以不出现不稳定的故障或固有的竞争条件。例如,如果在 ManagedService
之外的另一个接口(interface)下注册为服务,请等待直到收到非空 update
,然后再在该接口(interface)下注册它。
关于java - 如何立即获取 ManagedService 配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7616295/