OSGi Task Scheduler

Java EE introduced the Timer and Scheduling API and support some time ago. At the moment there is no specification for OSGi. But some projects have made an implementation on their own, like the Apache Sling Framework. Apache Sling is an OSGi based framework and has a very modular design. Some components can be installed in any OSGi environment on their own without pulling in the whole Sling framework, much to our advantage.


  • OSGi container
  • Apache Felix SCR
  • Apache Sling Commons Threads bundle
  • Apache Sling Commons Scheduler bundle

Test Environment

This has been tested with Apache Karaf 3.1.0 and Sling 6.

  • org.apache.felix.scr 1.8.2
  • org.apache.felix.scr.ds-annotations 1.2.4
  • org.apache.felix.scr.annotations 1.9.6
  • org.apache.sling.commons.threads 3.2.0
  • org.apache.sling.commons.scheduler 2.4.2

The bundles have been dropped into the deploy folder. The log file karaf/data/log/karaf.log shows the log message from the task.


Internally the Sling Scheduler uses the Quartz library. The Sling Scheduler uses the declarative service approach and registers all OSGi Declarative Service components which implements java.lang.Runnable and has a component property scheduler.expression as a task at the embedded Quartz scheduler.

Task Example

For a task to run every minute declared with a crontab style scheduling expression the following Java class needs to be created.

import org.apache.log4j.Logger;

public class HelloTask implements Runnable {

    private static final Logger logger = Logger.getLogger(HelloTask.class);
    public void run() {
        logger.info("Scheduled log output");


<?xml version="1.0" encoding="UTF-8"?>
<components xmlns:scr="http://www.osgi.org/xmlns/scr/v1.0.0">
    <scr:component name="task.hello">
        <implementation class="test.scheduler.HelloTask"/>
            <provide interface="java.lang.Runnable"/>
        <property name="scheduler.expression" value="0 * * * * ?"/>
        <property name="service.pid" value="test.scheduler.HelloTask"/>

Service-Component: OSGI-INF/task-hello.xml