Skip to main content

Command Palette

Search for a command to run...

โš™๏ธ OSGi Framework in AEM Explained (Apache Felix) โ€” Complete Beginner Guide

Published
โ€ข4 min read

Chapter 2 of AEM Learning Series โ€” Understanding the engine that runs AEM.

๐Ÿš€ What is OSGi?

OSGi = The engine that runs AEM

AEM is not a single huge Java application.
It is made of hundreds of small modules called Bundles.

OSGi is the framework that:

  • Loads bundles

  • Starts bundles

  • Stops bundles

  • Connects bundles together

AEM uses the Apache Felix implementation of OSGi.

๐Ÿ“ฑ Smartphone Analogy

Phone OS        โ†’ OSGi
Mobile Apps     โ†’ Bundles

โ€ข Install/uninstall apps without restart
โ€ข Apps talk to each other
โ€ข OS manages lifecycle

๐Ÿคฏ Why AEM Uses OSGi

Without OSGi (Traditional Java App)

ONE BIG APPLICATION
Change one thing โ†’ Redeploy everything
Restart โ†’ 15+ min downtime ๐Ÿ˜ฌ

With OSGi (AEM)

Bundle A (Assets)
Bundle B (Workflow)
Bundle C (Search)
Bundle D (Auth)
Bundle E (Editor)
Your Custom Bundle

๐Ÿ‘‰ Update one bundle โ†’ No restart required

This is called Hot Deployment ๐Ÿ”ฅ

๐Ÿงฉ Key OSGi Concepts


1๏ธโƒฃ Bundle (The Deployment Unit)

A Bundle = JAR file + OSGi metadata

Each bundle contains:

META-INF/MANIFEST.MF

This file tells OSGi:

  • Bundle name

  • Version

  • Exported packages

  • Imported packages

  • Dependencies

๐Ÿ‘‰ Smallest deployable unit in AEM.

2๏ธโƒฃ Bundle Lifecycle

INSTALLED โ†’ RESOLVED โ†’ STARTING โ†’ ACTIVE
                                     โ”‚
                                     โ–ผ
                                  STOPPING โ†’ UNINSTALLED
StateMeaning
INSTALLEDBundle added but dependencies not checked
RESOLVEDDependencies satisfied
ACTIVERunning and providing services
STOPPINGShutting down
UNINSTALLEDRemoved

๐Ÿ’ก Most common AEM issue:
Bundle stuck in INSTALLED โ†’ Missing dependencies.


3๏ธโƒฃ OSGi Services (Plug & Socket Model)

Service = Interface + Implementation

Interface  โ†’ Socket
Implementation โ†’ Plug

Any bundle can request a service, and OSGi injects it automatically.

Example Code

// Interface
public interface GreetingService {
    String getGreeting(String name);
}

// Implementation
@Component(service = GreetingService.class)
public class GreetingServiceImpl implements GreetingService {
    public String getGreeting(String name) {
        return "Hello " + name;
    }
}

// Injecting in Sling Model
@Model(adaptables = Resource.class)
public class HeaderModel {
    @OSGiService
    private GreetingService greetingService;
}

4๏ธโƒฃ OSGi Configurations (No Hardcoding!)

Instead of hardcoding values:

smtp.gmail.com

Use configuration:

mail.smtp.host = smtp.gmail.com
mail.smtp.port = 587

Different environments can have different configs:

๐Ÿ‘‰ No code changes required.


๐Ÿ”Ž Where to See OSGi in AEM

Console URLPurpose
/system/console/bundlesAll bundles
/system/console/componentsAll services
/system/console/configMgrOSGi configs
/system/console/status-slingmodelsSling Models

This is called the Felix Web Console.


๐Ÿง  OSGi in AEM Architecture

AEM
 โ””โ”€โ”€ OSGi (Felix)
      โ”œโ”€โ”€ Sling Bundles
      โ”œโ”€โ”€ Oak Bundles
      โ”œโ”€โ”€ Workflow Bundles
      โ””โ”€โ”€ Your Custom Bundles

All communicate via OSGi Service Registry.


๐Ÿ“ Things to Remember

  • OSGi = modular runtime used by AEM

  • Bundle = deployable JAR

  • Service = interface + implementation

  • Hot deployment = no restart required

  • Felix Console = /system/console

  • Bundle not ACTIVE? โ†’ Dependency issue


๐ŸŽฏ AEM Interview Questions โ€” OSGi (30 Q&A)


๐ŸŸข Section A โ€” Basics

Q1. What is OSGi?
Modular Java framework used by AEM.

Q2. OSGi implementation in AEM?
Apache Felix.

Q3. Why OSGi?
Modularity โ€ข Hot deployment โ€ข Dependency management.

Q4. What is a Bundle?
JAR with OSGi metadata.

Q5. What is MANIFEST.MF?
Bundle metadata file.

Q6. Felix Web Console?
Admin UI at /system/console.

Q7. Default credentials?
admin/admin (dev only).

Q8. Bundles console URL?
/system/console/bundles

Q9. Configurations console URL?
/system/console/configMgr

Q10. Components console URL?
/system/console/components


๐Ÿ”ต Section B โ€” Lifecycle

Q11. Bundle states?
Installed โ†’ Resolved โ†’ Active โ†’ Stopping โ†’ Uninstalled.

Q12. Bundle stuck in INSTALLED?
Missing dependencies.

Q13. Debug steps?
Check bundle โ†’ imported packages โ†’ install dependency.

Q14. ACTIVE โ†’ RESOLVED?
Yes, when stopped or dependency removed.

Q15. Fragment bundle?
Extends another bundle.


๐ŸŸฃ Section C โ€” Services

Q16. OSGi Service?
Java object registered in Service Registry.

Q17. Component vs Service?
Service = component exposed via interface.

Q18. Register service annotation?
@Component(service=MyInterface.class)

Q19. @Reference?
Inject service in OSGi component.

Q20. @Reference vs @OSGiService?
Component vs Sling Model injection.

Q21. Service Registry?
Runtime service lookup system.

Q22. service.ranking?
Priority for multiple implementations.


๐ŸŸ  Section D โ€” Configuration

Q23. OSGi Config?
Externalized properties.

Q24. Create configs?
Console or repository config files.

Q25. Run-mode configs?
config.author, config.publish.

Q26. Factory config?
Multiple instances of same component.

Q27. PID?
Unique config identifier.


๐Ÿ”ด Section E โ€” Scenarios

Q28. Sling Model not working?
Check bundle, components, Sling Models console.

Q29. DS vs SCR?
DS = modern, SCR = deprecated.

Q30. Two bundles exporting same package?
Yes โ€” version resolution.


๐ŸŽ‰ Final Summary

Understanding OSGi = Understanding how AEM actually runs.

Everything you build in AEM becomes an OSGi bundle.

More from this blog

AEM From Scratch: Complete Roadmap

8 posts