Skip to main content

Day 04: Developing first OSGi bundle

AEM Developer Series

After setting up our development environment in the previous post, it's time for us to get our hands dirty with some coding.
In this post, we will be creating and deploying our custom bundle into AEM.
While doing so, we will be learning concepts of OSGi Components and OSGi Services.

Software Modularity

In modern times, complex software can be thought of as a collection of various modules or components. These modules are normally independent of each other and modification in one module does not affect the other modules.

These modules interact with each other via an API. The API is defined as a set of classes and methods which can be used from other components.

If a module uses an API from another module, it has a dependency on the other module, i.e., it requires the other module exists and works correctly.

A module which is used by other components should try to keep its API stable. This avoids that a change affects other modules. But it should be free to change its internal implementation.

What is OSGi?

OSGi stands for Open Service Gateway initiative. It is a Java framework for developing and deploying modular software programs and libraries.

OSGi has two parts. 
  • The first part is a specification for modular components called bundles, which are commonly referred to as plug-ins. The specification defines an infrastructure for a bundle's life cycle and determines how bundles will interact.  
  • The second part of OSGi is a Java Virtual Machine (JVM)-level service registry that bundles can use to publish, discover and bind to services in a service-oriented architecture (SOA). 
The components and services can be dynamically installed, activated, de-activated, updated and uninstalled.

The OSGi specification has several implementations, for example, Equinox, Knopflerfish, and Apache Felix. AEM uses Apache Felix implementation. A bundle is the smallest unit of the modularization which means in OSGi, a software component is a bundle. 

In AEM, along with out of the box bundles, we can also install our custom bundles very easily. So without further ado, let us start creating an OSGi bundle.

Creating a Bundle

We will now create an OSGi bundle which has OSGi service which reads JSON data using HTTP GET request from the URL - Following are the steps to create an OSGi bundle - 

  • Create an AEM Multimodule Project in Eclipse. If you don't know how to do that, you can see here.
  • Create an interface ReadJsonService in the package This will be exposed as our service.
  • Now we need to create an implementation class for this service which will have the business logic. Hence, create a class ReadJsonDataImpl in the package
  • Now create constants class for storing project constants and Network class for the HTTP requests
  • Let us understand this code. If you see the closely, you will see there is a @Component annotation. This annotation signifies that the given class is a component in AEM and the property service = ReadJsonService.class signifies that this is a service which is exposed via the ReadJsonService interface.

OSGi Components and Services

A service is an object that is registered in the OSGi Service Registry and can be looked up using its interface name(s). The only prerequisite is that a service should implement some interface. For example, I could register a runnable object under the java.lang.Runnable interface and clients could look it up using that interface name.

A "component" tends to be an object whose lifecycle is managed, usually by a component framework such as Declarative Services (DS), Blueprint or iPOJO.

A component may have any of the following features, in combination or alone:
  1. A component may be started and stopped; this would be considered an "active" component, though that is also an informal term. A component that doesn't need to be started or stopped is called passive.
  2. A component may publish itself as an OSGi service.
  3. A component may bind to or consume OSGi services.
  4. In general, using a component framework is the easiest way to work with OSGi services because the framework will manage the binding to the services that you want to consume. For example, you could say that your component "depends on" a particular service, in which case the component will only be created and activated when that service is available -- and also it will be destroyed when the service becomes unavailable.
  • Once the Java code is done, we can now deploy the code using eclipse and maven (see this post for the steps).
  • Now go to the AEM instance and in CRXDE, you should be seeing your project under the /apps folder.
Demo project
  • Right-click on /apps/<your-project>/content/, then Create... and then Create Component... 
  • In the dialog, fill the values as shown in the below screenshot 
Create a Component
  • Click Next, then OK
  • Go to /apps/<your-project>/components/content/sampleComponent and open the sampleComponent.jsp file and add the below code and save. Note: We are using JSP only for simplicity. In the latest versions of AEM, we should use HTL (formerly called Sightly) and the use of JSP is discouraged.
  • Add the sample component on a page and you should be able to see the JSON string returned on that page as follows
    { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false }, 
    { "userId": 1, "id": 2, "title": "quis ut nam facilis et officia qui", "completed": false }, 
    { "userId": 1, "id": 3, "title": "fugiat veniam minus", "completed": false }, 
    { "userId": 1, "id": 4, "title": "et porro tempora", "completed": true },... 


Congratulations!! 🙋you have created your first bundle with a component and service and deployed it in the AEM server. We have battled with the custom code in AEM and on the same lines we can create multiple components and services in one bundle. 

You can find the complete code of this project on my GitHub. Feel free to fork or open issues, if any.

I would love to hear your thoughts on this and would like have suggestions from you to make it better. 

Feel free to befriend me on Facebook, Twitter or Linked In or say Hi by email.

Happy Coding 😊


  1. Great work , waiting for your content every day :)

    1. Hi Anirudh ,

      Thanks for your hard work , your tutorials helps to understand the aem concept in easy way.. I tried 4th tutorial of your post but i didn't get the output , i am not getting any error i dont know where i did mistakes.. i thought to share the screen but here there is no options.

      Thank you

  2. Can you please share error.log trace when you make the request?

    1. This comment has been removed by the author.

  3. If I create a component in CRDXE like you mentioned ,I am unable to see in TouchUI.
    How do I make this happen is there any configuration required?

    1. Hi, you need to add the component by editing your template. You can then allow your component group on your page and your components should be visible.

  4. In sampleComponent.jsp,
    <%=response%> should be <%=result%>

  5. hi, where should I add these things
    { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false },
    { "userId": 1, "id": 2, "title": "quis ut nam facilis et officia qui", "completed": false },
    { "userId": 1, "id": 3, "title": "fugiat veniam minus", "completed": false },
    { "userId": 1, "id": 4, "title": "et porro tempora", "completed": true },...

    Can u please give a brief description about it?

  6. Hi Anirudh,

    add the policy in Template but when i am trying to drag-drop the components in page getting the below error. could you please help me on this. Thanks!!

    Error Message: Unable to compile class for JSP:

    An error occurred at line: 5 in the jsp file: /apps/first/components/content/samplecomponent/samplecomponent.jsp cannot be resolved to a type
    3: <%
    5: service = sling.getService(;
    7: String result = service.getData();

  7. Hi Anirudh,

    Issue got resolved. Please ignore that. Thanks!!

  8. Hi,
    How did you resolve the issue?


Post a Comment

Popular posts from this blog

Day 00: AEM Developer Series

Hello everyone! Welcome to this AEM development series. We can all see the revolution of Digital Marketing today. Companies are dying to be a part of it and they have made this a war for the Digital Marketing tools.
Adobe is way ahead in this war and has gained a lot of market capture. They are leaders in the Digital Marketing platforms since 2014-15. One of the flagship product in Adobe's Digital Marketing suite is Adobe Experience Manager (AEM).
Since AEM is in huge demand, the people who know how to develop on AEM are also in huge demand. But developing on AEM is not easy as it is made up of various open-source technologies such as Apache Felix (OSGi), Apache Sling, Apache Oak and Adobe's own technologies like Granite, HTL etc. Learning all these technologies in conjunction can sometimes become confusing and frustrating 😫.
When I first started learning AEM in 2016, I was dumbfounded to see there is so much going on under the hood. I then spent months to gather all the res…

Day 05: Working with Sling Servlets in AEM

Day 03: Setting up AEM Development Environment