Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is data exchanged between a service and a bundle in OSGi?

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

like image 962
user1147070 Avatar asked Dec 15 '22 22:12

user1147070


2 Answers

No protocol is involved. You do standard Java method calls.

How it works is basically like this:

  1. 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(); }

  2. You implement the interface (in a separate package, which you don't export from your bundle)

  3. You register this interface in the OSGi Service Registry:

    timeServReg = bc.registerService(TimeService.class.getName(), new TimeServiceSimple(), props);

  4. 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); }

  5. 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.

like image 157
pooh Avatar answered Dec 30 '22 10:12

pooh


The OSGi service registry is like an object registry. Oversimplified, it's like a map with string keys and object values. Any bundle can put a service object into that map using a name of an interface implemented by the service object. Other bundles can then lookup the service object using the interface name. It all runs within the same Java VM process. Thus, there is no other communication than working with objects directly. The OSGi service registry added new API to work with Java Generics and class literals. This made working with the OSGi service registry more type safe.

An additional feature is OSGi Remote Services. It allows to expose services to other OSGi frameworks running in different Java processes (typically on different hosts). There are multiple implementations for the communication available. Some use web services (xml/http) and others use direct RPC.

like image 42
Gunnar Avatar answered Dec 30 '22 09:12

Gunnar