OSGI Services


What is OSGI service

An OSGi service is a java object instance, registered into an OSGi framework with a set of properties. Any java object can be registered as a service, but typically it implements a well-known interface.
The OSGi R3 specification, chapter 4.10 is highly recommended reading. Also, the javadoc for BundleContext contains lot of information.
The client of a service is always an OSGi bundle, i.e. a piece of java code possible to start via the BundleActivator interface.
Each bundle may register zero or more services. Each bundle may also use zero or more services. There exists no limit on the number of services, more than the ones given by memory limits or java security permissions.

Both publishing/registering and usage of services can be restricted by using java security permissions. A service can be dynamically started and stopped, and bundles which use services must be able to handle this dynamic behavior. The bundles can register listeners to be informed if a service is started or stopped.

What can services be used for?

The service concept is a very general-purpose tool, but some examples are:
Export functionality from a bundle to other bundles
  1. Import functionality from other bundles
  2. Register listeners for events from other bundles
  3. Expose external devices, such as UPnP devices or even hardware, to other OSGi bundles. See the Device and UPnP APIs
  4. Expose java code running in OSGI to an external network, e.g. via the UPnP or SOAP protocols.
  5. Bundle configuration, using the Configuration Manager
Generally, services is the preferred method bundles should use to communicate between each other.

Creating OSGI Service

A service in OSGi is defined by a standard Java class or interface. Typically a Java interface is used to define the service interface.
Create a new plug-in project and add the following service interface. this will be the model for your service.
now you can create the real service using the service interface that you created.

Register Service Using Declarative Services

you are now ready to declare your service. Eclipse contains tooling for declarative services that makes this a snap.


  1. Right-click on your project and select New -> Folder
  2. Create a folder called OSGI-INF
  3. Right-click on the OSGI-INF folder and select New -> Other
  4. Select Plug-in Development -> Component Declaration
  5. Click Next
  6. Choose a file name such as component.xml
  7. Give your component a globally unique name 
  8. For the Class, click Browse
  9. Choose class
  10. Click Finish
The component definition editor will appear allowing you to set additional parameters and options. Set the Configuration Policy to require. This policy tells the service manager to not start the service component until it has a configuration from the ConfigurationAdmin service.  Check the options: This component is enabled when started and This component is immediately activated.

 
On the Services tab of the component editor, add the service interface as a provided service:


Register Service Using Service API

when you using service api you can register your simple service using single java code

bundleContext.registerService();

Once the service is no longer used you must unregister the service with OSGi. OSGi counts the usage of services to enable the dynamic replacement of services.

context.ungetService();


Service API vs Declarative Services


OSGi services can be dynamically started and stopped. If you work with the OSGI low-level API you have to handle this dynamic in your code. This make the source code unnecessary complex. If you do not handle that correctly your service consumer can keep a reference to the service and the service cannot be removed via the OSGi framework.

To handle the dynamics automatically declarative services were developed. Prefer therefore the usage of OSGi declarative services over the low-level API.

0 comments :

Post a Comment