简体   繁体   English

如何使用OSGi Apache Felix在应用程序中调用Service Bundle

[英]How to call service Bundle in your application with OSGi apache felix

I'm new with OSGi, my problem is simple, I have a project with 2 clients 我是OSGi的新手,我的问题很简单,我有一个包含2个客户端的项目

Client A -> ref lib http-v1.01, io-v2.04 客户端A-> ref lib http-v1.01,io-v2.04
Client B -> ref lib http-v1.03, io-v2.05 客户端B-> ref lib http-v1.03,io-v2.05

I build Project A as bundle A register MyService, Project B as bundle B register MyService, interface service with same name then build to jar. 我将项目A生成为捆绑软件A注册MyService,将项目B生成为捆绑软件B注册MyService,然后使用同名接口服务构建到jar。

In Project C, I load bundle using Felix installBundle , everything work fine but, when I search tut how to call service from bundle, everything example look like 在Project C中,我使用Felix installBundle加载包,一切正常,但是,当我搜索如何从包中调用服务时,一切示例如下所示

BundleContext context... BundleContext上下文...
ServiceReference ref = context.getServiceReferences(MyService.class.getName()); ServiceReference ref = context.getServiceReferences(MyService.class.getName());
MyService myService = (MyService)context.getService(ref); MyService myService =(MyService)context.getService(ref);

MyService not include in Project C, it belong bundle A and B, how it code work ?? MyService不包含在Project C中,它属于捆绑包A和B,它的代码工作方式 I have add ref jar build from bundle A, B to Project C like normal library ?? 我已经将捆绑包A,B的引用jar构建添加到Project C,就像普通库一样? What difference between MyService in bundle A and MyService in bundle B. 捆绑软件A中的MyService和捆绑软件B中的MyService有什么区别?

Question: How project C call exactly MyService in bundle A or B ??? 问题:项目C如何在捆绑软件A或B中准确地调用MyService?

Tks all, sorry for my poor English. 谢谢,我英语不好。

If you simply register each service it will be difficult to distinguish them. 如果仅注册每个服务,将很难区分它们。 The trick is to use the service properties. 诀窍是使用服务属性。 For example you could have a property name:a for the first and name:b for the second service. 例如,对于第name:a ,您可以具有属性name:a ,对于第二个服务,可以具有name:b

Then when going through the service references you can check these properties. 然后,在浏览服务引用时,您可以检查这些属性。 You can also use a filter in ldap syntax like (name=a) to select only the first service. 您还可以使用ldap语法(name=a)例如(name=a)的过滤器来仅选择第一个服务。

Another thing to consider is that you should better not directly use the OSGi APIs. 要考虑的另一件事是,您最好不要直接使用OSGi API。 They are very hard to use correctly when you code becomes a bit more complicated. 当您的代码变得更加复杂时,很难正确使用它们。 So instead you should look into declarative services. 因此,您应该研究声明式服务。 This spec is implemented by felix scr an allows you to declare your service as well as its dependencies using annotations. 该规范由felix scr实现,允许您使用批注声明服务及其依赖项。 This way you can offload a lot of the hard work to the felix scr framework and you own code stays simpler. 这样,您可以将许多繁重的工作分担给felix scr框架,并且您自己的代码保持简单。

I have created a small hello world example to get people started. 我创建了一个小的“ hello world”示例来使人们入门。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM