Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.14.3. OSGi Services Extension
Overview Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
Pax CDI also provides an integration with OSGi services, enabling you to reference an OSGi service or to publish an OSGi service using CDI annotations. This capability is provided by the Pax CDI OSGi Services Extension, which is not enabled by default.
Enabling the OSGi Services Extension Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
To enable the Pax CDI OSGi Services Extension, you must include the following bundle header in the manifest file:
Require-Capability : org.ops4j.pax.cdi.extension; filter:="(extension=pax-cdi-extension)"
Require-Capability : org.ops4j.pax.cdi.extension; filter:="(extension=pax-cdi-extension)"
In a Maven project, you would normally add a
Require-Capability
element to the configuration of the Maven bundle plug-in. For example, to add both the Pax CDI Extender and the Pax CDI OSGi Services Extension to the bundle, configure your project's POM file, pom.xml
, as follows:
Maven dependency for the OSGi Services extensions API Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
To access the OSGi Services annotations in your Java code, you need to add a dependency on the
pax-cdi-api
package. Edit your bundle's POM file, pom.xml
, to add the Pax CDI API package as a Maven dependency:
Injecting an OSGi Service Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
You can inject an OSGi service into a field using the following annotation:
@Inject @OsgiService private IceCreamService iceCream;
@Inject @OsgiService
private IceCreamService iceCream;
Pax CDI finds the OSGi service to inject by matching the type of the OSGi service to the type of the field.
Disambiguating OSGi Services Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
If there exists more than one OSGi service of a particular type, you can disambiguate the match by filtering on the OSGi service properties—for example:
@Inject @OsgiService(filter = "(&(flavour=chocolate)(lactose=false))") private IceCreamService iceCream;
@Inject @OsgiService(filter = "(&(flavour=chocolate)(lactose=false))")
private IceCreamService iceCream;
As usual for OSGi services, the properties filter is defined using LDAP filter syntax (see the section called “Matching service properties with a filter” for more details). For an example of how to set properties on an OSGi service, see the section called “Setting OSGi Service properties”.
Selecting OSGi Services at run time Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
You can reference an OSGi service dynamically by injecting it as follows:
@Inject @OsgiService(dynamic = true) private Instance<IceCreamService> iceCreamServices;
@Inject
@OsgiService(dynamic = true)
private Instance<IceCreamService> iceCreamServices;
Calling
iceCreamServices.get()
will return an instance of the IceCreamService
service at run time. With this approach, it is possible to reference an OSGi service that is created after your bean is created.
Publishing a bean as OSGi Service with singleton scope Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
You can publish an OSGi service with OSGi singleton scope (which is the default), as follows:
@OsgiServiceProvider public class ChocolateService implements IceCreamService { ... }
@OsgiServiceProvider
public class ChocolateService implements IceCreamService {
...
}
OSGi singleton scope means that the bean manager creates a single instance of the bean and returns that instance every time a bean instance is requested.
Publishing a bean as OSGi Service with prototype scope Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
You can publish an OSGi service with OSGi prototype scope, as follows:
@OsgiServiceProvider @PrototypeScoped public class ChocolateService implements IceCreamService { ... }
@OsgiServiceProvider
@PrototypeScoped
public class ChocolateService implements IceCreamService {
...
}
OSGi prototype scope means that the bean manager creates a new bean instance every time a bean instance is requested.
Publishing a bean as OSGi Service with bundle scope Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
You can publish an OSGi service with bundle scope, as follows:
@OsgiServiceProvider @BundleScoped public class ChocolateService implements IceCreamService { ... }
@OsgiServiceProvider
@BundleScoped
public class ChocolateService implements IceCreamService {
...
}
Bundle scope means that the bean manger creates a new bean instance for every client bundle. That is, the
@BundleScoped
beans are registered with an org.osgi.framework.ServiceFactory
.
Setting OSGi Service properties Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
You can set properties on an OSGi service by annotating the service bean as follows:
Publishing an OSGi Service with explicit interfaces Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
You can explicitly specify the Java interfaces supported by an OSGi Service bean, as follows:
@OsgiServiceProvider(classes = {ChocolateService.class, IceCreamService.class}) public class ChocolateService implements IceCreamService { ... }
@OsgiServiceProvider(classes = {ChocolateService.class, IceCreamService.class})
public class ChocolateService implements IceCreamService {
...
}