Contact me:
Contact me

java OSGI Declarative Services Component bundles Example

One of the best ways to get the whiteboard OSGI pattern is using components(Declarative services model). Declarative services are part of the OSGi specification, it is a declarative description of services with XML.

The best practices when using OSGI services are:

  • Use a ServiceTracker
    » Don’t do all the service getting manually
    » Service tracker help you with dynamically coming and going services
  • Better: Use declarative approaches!
    » Either DS or Spring DM
    » Both help you with service dependencies and dependency injection

Here I present a simple example of two bundles using declarative services, a service and a consumer which are component bundles.

Components (Declarative services) are a very good alternative of using Activator, registering the bundle in the framework and using the servicetracker. Instead of using the classical activator file where you register the service in the OSGI framework, you define the service in an extra XML file lotated in the directory OSGI-INF.

You will find a more detail information in the Declarative Services Specification of the OSGi Service Platform Service compendium of The OSGi Alliance.

Basically a bundle need four things to become a component:

  • XML file where you describe the service the bundle provides and the dependencies of the other services of the OSGI framework.
  • Modify the manifest file of the bundle adding a new header where it is said that the bundle behaves as a component.
  • Define Activate and Deactivate methods in the implementation class. (Or bind and unbind methods)
  • Service component runtime. A service of the OSGI framework to manage the components: Declarative service of equinox or apache felix SCR .

XML component file

Service component.xml

Here the bundle defines the service you provide to the OSGI framework in case the bundle provides one and the implementation class of the service.

<?xml version="1.0" encoding="UTF-8"?>
<scr:component name="helloworld.component"
xsi:schemaLocation=" ">
<implementation class="helloworld.impl3.HelloWorldImpl"/>

<provide interface="helloworld.service3.HelloWorld" />


Consumer component.xml

Here the bundle define the service it provides, the implementation and the dependencies of other components within the tag reference .

The tag reference has the folowing atributes:

  • Name: whatever you want to give to the service reference you want to retrieve.
  • Interface: Class of the service registered in the framework.

  • Cardinality:
    The cardinality is expressed with the following syntax:
    cardinality ::= optionality ’..’ multiplicity
    optionality ::= ’0’ | ’1’
    multiplicity ::= ’1’ | ’n’

    The cardinality for a reference can be specified as one of four choices:
    • 0..1 – Optional and unary.
    • 1..1 – Mandatory and unary (Default) .
    • 0..n – Optional and multiple.
    • 1..n – Mandatory and multiple.

    More details are given in the Declarative Services Specification of the OSGi Service Platform Service compendium of The OSGi Alliance.

  • Policy: Dynamic or static
    Components must specify a policy how to handle these changes in the set of bound services.
    The static policy is the most simple policy and is the default policy. A compo-
    nent instance never sees any of the dynamics

    The dynamic policy is slightly more complex since the component imple-
    mentation must properly handle changes in the set of bound services

<?xml version="1.0" encoding="UTF-8"?>
<scr:component name="helloworld.consumer.component"
xsi:schemaLocation=" ">
<implementation class="helloworld.consumer.impl.ConsumerImpl"/>

<provide interface="helloworld.consumer.service.Consumer" />
<reference name="HELLO"


Manifest file

You must add the following header to the manifest file:
Service-Component: OSGI-INF/activator.xml

Activate and deactivate methods

For example, the bundle entry /OSGI-INF/activator.xml contains:

   <?xml version="1.0" encoding="UTF-8"?>
   <component name="example.activator">
      <implementation class="com.acme.Activator"/>

The manifest header Service-Component must also be specified in the bun-
dle manifest. For example:

   Service-Component: OSGI-INF/activator.xml

An example class for this component could look like:

   public class Activator {

      public Activator() {...}
      protected void activate(ComponentContext ctxt) {...}
      protected void deactivate(ComponentContext ctxt) {...}

As I have commented bind/unbind methods can be used instead of activate/deactivate methods. Here you can find and example:

<?xml version="1.0" encoding="UTF-8"?>
<component name="example.listen">
   <implementation class="com.acme.HttpResourceImpl"/>
   <reference name="HTTP"

Service Component Runtime

You can use the declarative service bundle of equinox or the implementation of apache that you can download here: org.apache.felix.scr-1.0.8.jar

Here you can find the declarative services schema

Here you can find the simple example:

  • Hello worl Service
  • Hello world Consumer
  • And the target platform:

    id State Bundle
    0 ACTIVE org.eclipse.osgi_3.5.0.v20090520
    2 ACTIVE org.eclipse.osgi.services_3.2.0.v20090520-1800
    3 ACTIVE HelloWorldConsumer2_1.0.0
    4 ACTIVE org.apache.felix.scr_1.0.6
    5 ACTIVE HelloWorld_Service_1.0.0

    Share it!

    Twitter Gmail Delicious Google Bookmarks Hotmail Yahoo Mail Share/Bookmark