OSGi Sling Service Example

I’m posting a simple example of an OSGi Service for Sling. I believe this is enough to get you started with a simple service. First, I want to share a great blog by Moritz Havelock called “In the Sling”. I used this example but changed the file structure of the POM to fit my needs. I’ll be using this as a base for some of my services development. This example is taken from lesson 5.

For my project I’m using Maven 3.0.4, Java 1.6.0_31 and I have tested both CQ 5.4 and CQ 5.5. This should also work fine within Apache Sling but I haven’t tested it.

If you’re like me you just want to get the POM and run it to see if it works for you. First create the file folders based on the structure below. Create 3 Java classes the jsp and a json file. You’ll need to have Maven and Java installed on your system and then just execute the POM.xml with >mvn install

Once the build is completed execute the following URL http://localhost:4502/content/osgitest/test to verify the test. You should see that the service returns “Hello World!”.

I also included a unit test within this example because it’s really easy to create a few tests for the services and if I add it into the example I’m more apt to write a few. To verify that the Unit test works simply change the “Hello World!” to something else and see the failure.

If you’re new to Maven I suggest reading “In the Sling” to really understand what is happening.

│   pom.xml
    │   ├───java
    │   │   └───com
    │   │       └───adobe
    │   │           └───support
    │   │               └───examples
    │   │                   └───osgi
    │   │                       └───service
    │   │                               SampleService.java
    │   │                               SampleServiceImpl.java
    │   │
    │   └───resources
    │       ├───META-INF
    │       └───SLING-INF
    │           └───initial-content
    │               ├───apps
    │               │   └───samples
    │               │       └───osgitest
    │               │               GET.esp
    │               │
    │               └───content
    │                   └───osgitest
    │                           test.json

<project 	xmlns="http://maven.apache.org/POM/4.0.0" 
			xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<name>OSGi Service Example</name>
	<description>Adobe Developer Support OSGi Service Example</description>
	Credit: This example is based on Moritz Havelock's http://in-the-sling.blogspot.com/ 
	I suggest you use the most current versions of the plugin's. 
	Search the Central Repository for the most current versions http://search.maven.org/
	Use the search strings below and update the appropriate versions
	// Felix
	g:"org.apache.felix" a:"org.apache.felix.framework"
	g:"org.apache.felix" a:"maven-bundle-plugin"
	g:"org.apache.felix" a:"maven-scr-plugin"
	g:"org.apache.felix" a:"org.osgi.core"
	// Maven
	g:"org.apache.maven.plugins" a:"maven-compiler-plugin"	
	// Sling
	g:"org.apache.sling" a:"maven-sling-plugin"
		<!-- Felix -->
    	<!-- Maven -->
    	<!-- Sling -->
			<!--  Maven will compile our source java classes using
			 the "project.build.java.version" specified -->
			<!--  this will install the OSGi bundle into Sling for us 
			we now upload the jar file automatically when we build with this plug-in -->
				<!-- This will create the OSGI-INF for us that handles the Activator Class for us
				we now auto-generate the details in our bundle with this plug-in-->
				<!-- This will create the OSGi /META-INF/MANIFEST.MF for us
				we now auto-generated the file for us with this plug-in -->
						<Bundle-Vendor>Kurt Mossman</Bundle-Vendor>
							Inserting content into the JCR and installing some files on the server with your bundle. 
								The first line will overwrite the contents of the node at content/osgitest with test.json 
								NOTE: uninstall:=false says that it will not remove the content when I remove the package. 
									This could be set to true to also remove the content when the package is removed the choice is yours. 
								The second line will overwrite the path will install the files and overwrite them if you re-install. 
			<!--  use the surefire plugin to run the test cases
			we use a thread count of 4 to increase the performance of the test time. -->


package com.adobe.support.examples.osgi.service;

public interface SampleService {
	public String sayHello();


package com.adobe.support.examples.osgi.service;

// src.component
// declares the class component. This will provide a wrapped ManagedService component in the OSGI container. 

// src.service interface="SampleService" 
// declares the service we are offering. Actually, the interface attribute is superflous, as by default, all implemented Interfaces are used. 

  @scr.service interface="SampleService"

public class SampleServiceImpl implements SampleService {
	public String sayHello() {
		return "Hello World!";


var service = sling.getService(Packages.com.adobe.support.examples.osgi.service.SampleService);
The service says: "<%= service.sayHello() %>"


	"jcr:primaryType" : "nt:unstructured",
	"sling:resourceType" : "samples:osgitest"


import com.adobe.support.examples.osgi.service.SampleService;  
import com.adobe.support.examples.osgi.service.SampleServiceImpl;

import junit.framework.Assert;

import org.junit.Test;

public class SampleServiceTest {
	public void test() { 
		SampleService ss = new SampleServiceImpl(); 
		String msg = ss.sayHello(); 
		Assert.assertEquals("Hello World!", msg); 
Share on Facebook

2 Responses to OSGi Sling Service Example

  1. david says:

    @Kurt, I’ve used this approach and like it – what are the pros/cons vs using packages for deploying apps to CQ?

    • Kurt Mossman says:

      You may have developers that don’t want to deal with CQ at all and it’s not really necessary. You can simply build the OSGI service as a bundle and deploy it. Using package manager is much easier but if you have developers just writing Java services they may not want to deal with the package manager or CQ.