创建服务并且只允许一个捆绑包随时保留该服务

时间:2017-08-13 09:42:04

标签: osgi apache-karaf apache-felix blueprint-osgi

我正在尝试创建一个服务,一旦创建它,​​它只允许自己在任何时候由一个使用者/包保持。 (如果这与OSGi的哲学/规范相反,那么显然提供了一个快速的答案,但参考了OSGi规范。说明这一点将不胜感激。)

为了实现这样的要求,我实现了ServiceFactory接口,认为每当需要服务时,都会调用getService(Bundle bundle, ServiceRegistration<S> registration)方法,并且我可以确定{{1}无论是否是新消费者并采取相应行动。

在我测试过的场景中似乎并非如此。

使用Apache Karaf并通过Blueprint实例化服务的使用者,似乎永远不会调用Bundle方法。相反,直接调用消费者对服务的绑定方法,但是注入代理服务对象。

虽然我理解蓝图确实使用代理,但getService合同仍然有义务履行,即使它是使用服务的代理对象吗?

为什么我要这样做? 我试图包装JavaFX和ServiceFactory类,因为JavaFX不是OSGi友好的我试图协调对Stage对象的访问。我知道有一些像Drombler这样的框架但是对它们的简要介绍让我觉得它不适合我的用例。它们对我的需求似乎过于严格,例如:我不一定希望以Drombler使用的方式布局应用程序。

2 个答案:

答案 0 :(得分:0)

It depends what you mean by a consumer. ServiceFactory does give you the chance to create a separate instance of a service per bundle that calls getService on your service. It's not clear from your question but I suspect you weren't seeing the getService invoked multiple times because you were fetching the service from the same consumer bundle. In this case, ServiceFactory simply returns the same object repeatedly.

As for your general question about restricting access to a single consumer, no that really goes against the OSGi philosophy. I'm sorry I don't have a spec reference for you but the clue is in the name: it's a service that is available to all.

答案 1 :(得分:0)

  

我知道有一些像Drombler这样的框架但是简单地看一下这些框架让我觉得它不适合我的用例。它们对我的需求似乎过于严格,例如:我不一定希望以Drombler使用的方式布局应用程序。

请注意, Drombler FX 应用程序的布局是可插入的,因此您可以根据自己的需要提供自己的实施方案。这使您可以充分利用 Drombler FX JavaFX

虽然此功能已有一段时间了,但现在有一个new tutorial trail更详细地解释了它。