I am new to OSGi. Whatever tutorials i read i am unable to find how data is exchanged between a service and a bundle. I know that one bundle has to get registered itself with service registry so that other bundles can use them. However i am unable to get how the data between the service and bundle is exchanged. Like in web services the data is exchanged as XML format or like and using Http protocol. So how the data is exchanged between the service and the bundle using it. Is it also via some protocol or how? And also does it involves any overhead. Please Help
Thanks
No protocol is involved. You do standard Java method calls.
How it works is basically like this:
You define a Java interface for your service. Just a normal interface, nothing special has to be implemented. E.g.
interface TimeService {
public String getCurrentTime();
}
You implement the interface (in a separate package, which you don’t export from your bundle)
You register this interface in the OSGi Service Registry:
timeServReg = bc.registerService(TimeService.class.getName(),
new TimeServiceSimple(),
props);
In the second bundle – the one that wants to use it, you search for this service:
timeRef = bc.getServiceReference(TimeService.class.getName());
if (timeRef != null) {
timeService = (TimeService) bc.getService(timeRef);
}
You use the service by simply using the Java object you just got.
You call the methods:
System.out.println(“The current time is: ” + timeService.getCurrentTime());
Of course there are many details and good practices, like for example to use the ServiceTracker for finding the service etc. but this is the basics.
You can find many examples here.