April 27, 2020
Estimated Post Reading Time ~

Referencing the services between OSGI bundles

This post explains how to refer the services between OSGI bundles in Adobe Experience Manager.

Export-Package:
Bundles may export zero or more packages from the JAR to be consumable by other bundles. The export list is a comma-separated list of fully-qualified packages, often with a version attribute. If not specified, the version defaults so 0.0.0.

In the target bundle the packages contains the required services and the classes exposed to the other bundles should be exported.


Import-Package
The Import-Package header is used to declare dependencies at a package level from the bundle. At runtime, the bundle will be wired up with whatever (compatible) bundle offers the package.

In the source bundle the packages contains the required services and classes referred from other bundle should be imported.


Approaches for reference the following two approaches can be used to refer the services from other bundle.

Through Reference
@Reference
private CommerceServiceFactory sFactory;

Through bundle context
BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
CommerceServiceImpl factoryFromBundle=(CommerceServiceImpl)bundleContext.getService(bundleContext.getServiceReference(CommerceServiceFactory.class)).getCommerceService();


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.