如何更新OSGI @Reference列表,如果我的服务启动早于被引用的一半服务?

huangapple go评论85阅读模式
英文:

how to update OSGI @Reference List if my service starts earlier than half of referenced ones

问题

这是对主题“没有OSGI捆绑激活顺序”的又一次尝试。

我有4个实现相同接口 DataProvider 的服务(捆绑),这意味着它们从字面上实现了它,并且在它们的 @Component 注解中也将其定义为 service = { DataProvider.class }。这个接口提供了类似诊断数据的内容。我想要从这4个服务中收集这些数据,并在GUI上打印出来,该GUI也是我的大型框架中的一个OSGI捆绑。为了实现这一目标,我在我的GUI捆绑中创建了以下引用:

@Reference
private volatile List<DataProvider> dataProvider;

然后我想要遍历该列表,并将所需的一切追加到GUI的文本框中。

问题是,当GUI捆绑启动时,只有四个服务中的两个被激活,因此我的列表将只包含两个服务对象,而不是全部四个,然后只打印它们。最后两个服务在我的GUI捆绑已经激活之后加载,因为它们也在等待它们自己的引用完全满足时才激活(其中涉及一些网络操作,所以需要一些时间,大约10秒)。当然,我希望我的GUI显示来自所有4个服务的诊断数据。我该怎么办呢?

我尝试在 @Reference 注解中添加 policyOption = ReferencePolicyOption.GREEDY,我期望它会在 List<DataProvider> dataProvider 接收到新成员时强制重新激活GUI捆绑,但不,这并没有发生。

附言:当然,肯定有一个直接的解决方案:只需在GUI捆绑中添加 Thread.sleep(),并设置适当的值,以便在上述讨论的列表唤醒的时间之前将其填满。但这真的很糟糕,我不希望用户在GUI出现之前等待大约10秒,更不用说出现问题时的情况了。

英文:

That's one more attempt to make a dig at the theme "there is no order of OSGI bundles activation".

I have 4 services (bundles) which implement the same interface DataProvider meaning they implement it literally and also have it as service = { DataProvider.class } at their @Component annotations. This interface provides something like diagnostic data from these services. I want to collect this data from all 4 services and to print it on the GUI, which is also an OSGI bundle in my big framework. In order to do that I created such a reference in my GUI bundle:

@Reference
private volatile List&lt;DataProvider&gt; dataProvider;

and then I want to iterate over that list and append to the GUI's textframe everything I need.

The problem is that at the moment when GUI bundle starts, only two of four services are activated, so my list will contain only two service objects instead of all four and print only them. The last two services are loaded after my GUI Bundle has been already activated, because they also wait until their own references become fully satisfied (where some network operations are done, so it takes some time, around 10 seconds). Of course I want my GUI to show the diagnostic data from all 4 services. What could I do for that?

I tried to add policyOption = ReferencePolicyOption.GREEDY to the @Reference annotation, I expected it would force to reactivate GUI bundle each time this List&lt;DataProvider&gt; dataProvider receives a new member, but no, it didn't happen.

P.S. yes there is certainly a straightforward solution: just to add Thread.sleep() to the GUI Bundle with some appropriate value, so to the time of awakening the discussed above list will be full. But this is really bad thing, I don't want the user waits like 10 seconds before GUI appears, not to speak about the situations, when something goes wrong.

答案1

得分: 1

你可以在配置中指定最小基数。在您的情况下,这是通过配置您的组件的 dataProvider.minimum.cardinality 属性来指定的。(请参见第 112.6.2.2 节的最小基数属性。)

package com.example;
@Component
public class Diagnostics {
    @Reference
    List<DataProvider> dataProvider;
}

然后,您需要为 PID com.example.Diagnostics 添加一个配置记录:

dataProvider.minimum.cardinality = 4

使用这种配置方式非常适用于 Configurator 规范。通过这个规范,您可以在一个捆绑包中指定应用程序的配置。

或者,您可以创建 4 个引用,并通过属性来区分服务,使用 target 注解方法来指定过滤器。

英文:

You can specify the minimum cardinality in the configuration. In your case, this is specified with the dataProvider.minimum.cardinality property in the configuration for your component. (See section 112.6.2.2 Minimum Cardinality Property.)

package com.example;
@Component
public class Diagnostics {
    @Reference
    List&lt;DataProvider&gt; dataProvider;
}

So then you need to add a configuration record for PID com.example.Diagnostics:

dataProvider.minimum.cardinality = 4

This model of using configuration works very well with the Configurator Specification. With this specification, you can specify the configuration of an application in a bundle.

Alternatively, you can create 4 references and distinguish the services by a property, using the target annotation method to specify a filter.

huangapple
  • 本文由 发表于 2020年7月30日 16:43:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63169435.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定