LibraryLink ToToggle FramesPrintFeedback

OSGi Services

An OSGi service is a Java class or service interface, along with a number of service properties as name/value pairs. The service properties differentiate among different service providers that provide services with the same service interface.

An OSGi service is defined semantically by its service interface and implemented as a service object. A service's functionality is defined by the interfaces it implements. Thus, different applications can implement the same service. Service interfaces allow bundles to interact by binding interfaces, not implementations. A service interface should be specified with as few implementation details as possible.

In the OSGi Framework, the Service layer provides communication among modules and their contained components using the publish, find, and bind service model. The Service layer contains a service registry where services are registered with the Framework to be used by other applications and clients can look up available services. A bundle can register an implementation of a service with the service registry. Other bundles can look up and use the service. Lookup is performed via Java interface and service properties.

Bundles are built around a set of cooperating services available from a shared service registry. A service is a normal Java object that is registered under one or more Java interfaces with the service registry. The service object is owned by, and runs within, a bundle. This bundle must register the service object with the Framework service registry so that the service’s functionality is available to other bundles under the control of the Framework.

Service interfaces are implemented by objects created by the bundle. Bundles can register services, search for them, or receive notifications when their registration state changes.

Each bundle can register any number of services in the service registry using the fully qualified name of its interface and its properties. The name and properties are then used when the registry is queried using LDAP query language. Bundles can select an available implementation at runtime from the Framework service registry using queries formulated with LDAP syntax.

A bundle is responsible for runtime service dependency management activities including publication, discovery, and binding, as well as adapting to changes resulting from dynamic availability (arrival or departure) of services that are bound to the bundle. In OSGi, both service providers and service requestors are part of a bundle.

  • Service providers publish their services in a service registry

  • Service requestors use the service registry to find services and bind to service providers

The OSGi Framework maps services to their underlying service objects, and provides a simple but powerful query mechanism that enables a bundle to request the services it needs.

The OSGi Framework provides an event notification mechanism so service requesters can receive notification events signalling changes in the service registry. These changes include the publication or retrieval of a particular service and services that are registered, modified, or unregistered.

In OSGi a service is defined using a Java interface. Services in OSGi are Java objects, invoked using local method invocations.

OSGi services allow bundles to share Java objects. The OSGi bundle abstraction allows modules to share Java classes. This is a static form of reuse. The shared classes must be available when the dependent bundle is started.

When a bundle wants to use a service, it looks up the service and invokes the Java object as a normal Java call. Therefore, invocations on services are synchronous and occur in the same thread. You can use callbacks for more asynchronous processing. Parameters are passed as Java object references. No marshalling or intermediary canonical formats are required as with XML. OSGi also provides mechanisms to deal with services being unavailable.

The OSGi Framework provides optional services to manage the operation of the Framework:

The services are described in detail in separate chapters in the OSGi Service Platform Release 4 specification available from the release 4 download page on the OSGi Alliance web site.

In addition to the OSGi Framework services, the OSGi Alliance defines a set of optional, standardized compendium services. The OSGi compendium services provide APIs for tasks like logging and preferences. These services are described in the OSGi Service Platform, Service Compendium available from the specifications drafts page on the OSGi Alliance site.

A JAR file containing compendium services is located in your install directory in:

\system\org\apache\felix\org.osgi.compendium\

The Configuration Admin compendium service is like a central hub that persists configuration information and distributes it to interested parties. The Configuration Admin service specifies the configuration information for deployed bundles and ensures that the bundles receive that data when they are active. The configuration data for a bundle is a list of name-value pairs. See FUSE ESB Kernel.