0
votes

I'm new with OSGi, my problem is simple, I have a project with 2 clients

Client A -> ref lib http-v1.01, io-v2.04
Client 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.

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

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

MyService not include in Project C, it belong bundle A and B, how it code work ?? I have add ref jar build from bundle A, B to Project C like normal library ?? What difference between MyService in bundle A and MyService in bundle B.

Question: How project C call exactly MyService in bundle A or B ???

Tks all, sorry for my poor English.

1

1 Answers

0
votes

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.

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.

Another thing to consider is that you should better not directly use the OSGi APIs. 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. This way you can offload a lot of the hard work to the felix scr framework and you own code stays simpler.

I have created a small hello world example to get people started.