Reputation: 2139
I am new to osgi and have very little experience with it. I have an osgi bundle which exports a bean as osgi service using in the config xml file. I want to use that service in another osgi bundle. Can anybody tell me how to do that? I am using maven and felix osgi.
Upvotes: 0
Views: 673
Reputation: 662
It doesn't matter how the OSGi service is exported, using blueprint or smth else - since it is an OSGi service, your bundle can get it from the OSGi service registry. For this you must:
Import its package in your bundle manifest, in order to declare the dependency. You have to add Import-Package: com.acme.theservicepackage in META-INF/Manifest.mf
in your Activator class, you have to get the service from the service registry using your bundle context:
timeRef = bc.getServiceReference(TimeService.class.getName()); if (timeRef != null) { timeService = (TimeService) bc.getService(timeRef); }
Then you simply use the timeService (or whatever interface your service is using) by calling its methods, whatever they are.
There are many details here, you can use the helper ServiceTracker class instead, or blueprint, or declarative services... But since you seem to be new to OSGi, I have left it as simple as possible and have shown the most basic way to do it.
There are demos here for the basic OSGi services.
Upvotes: 1
Reputation: 19606
Do you want to use the service with blueprint? Your description "config xml" sounds a bit like it might be blueprint. In this case you can use
I have a full example on my website: http://www.liquid-reality.de/x/DIBZ
The example shows how to export and import a service using blueprint.
Upvotes: 1