java - Eclipse 中的 OSGi 声明式服务、多个服务接口(interface)和线程安全

标签 java osgi eclipse-rcp equinox declarative-services

我刚刚在我的代码中偶然发现了一个现象,归结为:

我有一个 OSGi 声明式服务,提供两个配置如下的服务接口(interface):

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="init" deactivate="dispose" enabled="true" name="redacted.redactedstore">
   <implementation class="redacted.RedactedStore"/>
   <service>
      <provide interface="redacted.IRedactedStore"/>
      <provide interface="redacted.IRedactedStoreControl"/>
   </service>
</scr:component>

在我的代码中,我有两个不同的线程,它们都打开一个 ServiceTracker 来获取服务实例,但是通过不同的接口(interface):

tracker = new ServiceTracker<>(getBundle().getBundleContext(), <serviceClass>.class, null);
tracker.open();
tracker.waitForService(1000l);

所以一个线程使用IRedactedStore作为服务类,另一个使用IRedactedStoreControl作为服务接口(interface)。

所以似乎发生的情况是,当两个线程在正确的时间并行运行时,Equinox SCR 将实例化组件实现类的两个实例而不是一个(如我所料)。

这种行为是否正确?或者这是 OSGi 的 Equinox 实现中的错误?

如果行为是正确的,我可以在我的代码中做些什么来通过另一种方式配置服务来防止这种情况发生吗? (当然我可以重组服务,让它只提供一个接口(interface),或者我可以同步服务跟踪器……)

最佳答案

对于非原型(prototype)范围组件,我希望它只创建一次。 参见 declarative services spec .

如果此问题仅在服务跟踪器并行运行时发生,那么我怀疑它可能是 equinox scr 中的并发问题。

关于java - Eclipse 中的 OSGi 声明式服务、多个服务接口(interface)和线程安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57076004/

相关文章:

java - Android:在 onCreate 内部启动 Intent 会导致无限循环/崩溃

java - 在父应用程序和 OSGi 框架之间传递参数

jpa - Eclipselink : How do you get the EntityManager in each bundle?

eclipse-rcp - Eclipse RCP 应用程序中的曲线选项卡

java - 在 Eclipse RCP 应用程序中拖放会导致屏幕闪烁

java - Android postDelayed 有效但不能将其置于循环中?

java - BigInteger::intValueExact() - 这有什么意义?

java - 合并两个不透明图像以获得透明图像

jersey - OSGi、Jersey 和 "No message body writer has been found for class"

java - 如何正确创建WizardPage(Eclipse平台)