Differences

This shows you the differences between two versions of the page.

Link to this comparison view

osgi_task_scheduler [2014/03/13 13:25]
osgi_task_scheduler [2021/04/05 11:23] (current)
Line 1: Line 1:
 +====== 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 [[http://sling.apache.org | 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.
  
 +===== Requirements =====
 +  * 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.
 +
 +===== Tasks =====
 +Internally the Sling Scheduler uses the [[http://quartz-scheduler.org | 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.
 +
 +<sxh java; title: HelloTask.java>
 +import org.apache.log4j.Logger;
 +
 +public class HelloTask implements Runnable {
 +
 +    private static final Logger logger = Logger.getLogger(HelloTask.class);
 +    
 +    @Override
 +    public void run() {
 +        logger.info("Scheduled log output");
 +    }
 +
 +}
 +</sxh>
 +
 +<sxh xml; title: OSGI-INF/task-hello.xml>
 +<?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"/>
 +        <service>
 +            <provide interface="java.lang.Runnable"/>
 +        </service>
 +        <property name="scheduler.expression" value="0 * * * * ?"/>
 +        <property name="service.pid" value="test.scheduler.HelloTask"/>
 +    </scr:component>
 +</components>
 +</sxh>
 +
 +<sxh title: MANIFEST.MF>
 +Service-Component: OSGI-INF/task-hello.xml
 +</sxh>
 +
 +===== Links =====
 +  * [[http://sling.apache.org | Apache Sling]]
 +  * [[http://felix.apache.org | Apache Felix]]
 +  * [[http://felix.apache.org/documentation/subprojects/apache-felix-service-component-runtime.html | Apache Felix SCR]]
 +  * [[http://felix.apache.org/documentation/subprojects/apache-felix-maven-scr-plugin/scr-annotations.html |  Apache Felix SCR Annotations]]
 +
 +{{tag>devel java osgi}}