An OSGi bundle is a self-contained, reusable module in an OSGi-based application. It is essentially a JAR file with additional metadata in its META-INF/MANIFEST.MF
file, which defines dependencies, versioning, and lifecycle instructions.
Each bundle has its own classloader and can dynamically interact with other bundles through the OSGi Service Registry.
An OSGi bundle follows a specific structure inside a JAR file:
my-bundle.jar
??? META-INF/
? ??? MANIFEST.MF (Contains OSGi metadata)
??? com/example/
? ??? MyService.class (Java class)
? ??? MyServiceImpl.class
??? resources/
MANIFEST.MF
FileThis file is crucial in an OSGi bundle and defines its properties:
Bundle-Name: My OSGi Bundle
Bundle-SymbolicName: com.example.mybundle
Bundle-Version: 1.0.0
Bundle-Activator: com.example.MyActivator
Import-Package: org.osgi.framework
Export-Package: com.example.api
Each OSGi bundle has a well-defined lifecycle, managed by the OSGi framework:
To manage a bundle’s lifecycle, you can create a Bundle Activator class:
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class MyActivator implements BundleActivator {
@Override
public void start(BundleContext context) {
System.out.println("Bundle Started!");
}
@Override
public void stop(BundleContext context) {
System.out.println("Bundle Stopped!");
}
}
META-INF/MANIFEST.MF
file with OSGi metadata.* Modularization – Bundles enable better code organization.
* Dynamic Updates – Can be installed/uninstalled at runtime without restarting the application.
* Dependency Management – Avoids classloading conflicts.
* Reusability – Bundles can be reused across different applications.