incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Sling Website > Scheduler Service (commons scheduler)
Date Mon, 08 Feb 2010 08:50:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=SLINGxSITE&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="http://cwiki.apache.org/confluence/display/SLINGxSITE/Scheduler+Service+%28commons+scheduler%29">Scheduler
Service (commons scheduler)</a></h2>
    <h4>Page  <b>added</b> by             <a href="http://cwiki.apache.org/confluence/display/~cziegeler@apache.org">Carsten
Ziegeler</a>
    </h4>
         <br/>
    <div class="notificationGreySide">
         <h1><a name="SchedulerService%28commonsscheduler%29-TheCommonsScheduler"></a>The
Commons Scheduler</h1>

<p>The scheduler is a service for scheduling other services/jobs (it uses the open source
Quartz library). The scheduler can be used in two ways, by registering the job through the
scheduler API and by leveraging the whiteboard pattern that is supported by the scheduler.</p>

<h2><a name="SchedulerService%28commonsscheduler%29-TheSchedulerAPI"></a>The
Scheduler API</h2>

<p>The scheduler has methods to execute jobs periodically, based on a cron expression
or at a given time. For more details please refer to the <a href="http://sling.apache.org/apidocs/sling5/index.html?org/apache/sling/commons/scheduler/Scheduler.html"
rel="nofollow">javadocs</a>.</p>

<h2><a name="SchedulerService%28commonsscheduler%29-ExamplesofscheduledjobsregisteredthroughtheschedulerAPI"></a>Examples
of scheduled jobs registered through the scheduler API</h2>

<p>The following examples show you how to define and schedule a job that is registered
through the scheduler api.</p>

<h3><a name="SchedulerService%28commonsscheduler%29-Definingthejob"></a>Defining
the job</h3>

<p>The following code sample defines a <b>job</b> object that writes a message
in the logs:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"> 
<span class="code-keyword">final</span> <span class="code-object">Runnable</span>
job = <span class="code-keyword">new</span> <span class="code-object">Runnable</span>()
{
	<span class="code-keyword">public</span> void run() {
		log.info(<span class="code-quote">"Executing the job"</span>);
	}
};
</pre>
</div></div> 

<p>h3 Scheduling with a cron expression</p>

<p>To execute the job as defined above at 10:15am every Monday, Tuesday, Wednesday,
Thursday and Friday, you can use the <b>addJob()</b> method with the following
parameters:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"> 
<span class="code-object">String</span> schedulingExpression = <span class="code-quote">"0
15 10 ? * MON-FRI"</span>;
<span class="code-keyword">this</span>.scheduler.addJob(<span class="code-quote">"myJob"</span>,
job, <span class="code-keyword">null</span>, schedulingExpression, <span class="code-keyword">true</span>);
</pre>
</div></div>

<p>Refer to <a href="http://www.docjar.com/docs/api/org/quartz/CronTrigger.html"
rel="nofollow">http://www.docjar.com/docs/api/org/quartz/CronTrigger.html</a><br/>
 to define more scheduling expressions.</p>

<h3><a name="SchedulerService%28commonsscheduler%29-Schedulingatperiodictimes"></a>Scheduling
at periodic times</h3>

<p>To execute the job as defined above every 3 minutes (180 seconds), you can use the
<b>addPeriodicJob()</b> method with the following parameters:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-object">long</span> period = 3*60; <span class="code-comment">//the
period is expressed in seconds
</span><span class="code-keyword">this</span>.scheduler.addPeriodicJob(<span
class="code-quote">"myJob"</span>, job, <span class="code-keyword">null</span>,
period, <span class="code-keyword">true</span>);
</pre>
</div></div>

<h3><a name="SchedulerService%28commonsscheduler%29-Schedulingatagiventime"></a>Scheduling
at a given time</h3>

<p>To execute the job as defined above at a specific date (on January 10th 2020), you
can use the <b>fireJobAt()</b> method with the following parameters:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
SimpleDateFormat formatter = <span class="code-keyword">new</span> SimpleDateFormat(<span
class="code-quote">"yyyy/MM/dd"</span>);
<span class="code-object">String</span> date = <span class="code-quote">"2020/01/10"</span>;
java.util.Date fireDate = formatter.parse(date);
<span class="code-keyword">this</span>.scheduler.fireJobAt(<span class="code-quote">"myJob"</span>,
job, <span class="code-keyword">null</span>, fireDate);
</pre>
</div></div>


<h3><a name="SchedulerService%28commonsscheduler%29-Aserviceschedulingthejobbasedon3differentkindsofscheduling"></a>A
service scheduling the job based on 3 different kinds of scheduling</h3>

<p>The code implementing a service that simultaneously executes the job based on 3 different
kinds of scheduling can look as follows:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">package</span> sling.docu.examples;

<span class="code-keyword">import</span> java.io.Serializable;
<span class="code-keyword">import</span> java.util.Date;
<span class="code-keyword">import</span> java.util.HashMap;
<span class="code-keyword">import</span> java.util.Map;

<span class="code-keyword">import</span> org.apache.sling.commons.scheduler.Scheduler;
<span class="code-keyword">import</span> org.osgi.service.component.ComponentContext;
<span class="code-keyword">import</span> org.slf4j.Logger;
<span class="code-keyword">import</span> org.slf4j.LoggerFactory;

/**
 *  This service executes scheduled jobs
 *  
 *	@scr.component immediate=<span class="code-quote">"<span class="code-keyword">true</span>"</span>
metatype=<span class="code-quote">"<span class="code-keyword">false</span>"</span>
 *
 */
<span class="code-keyword">public</span> class HelloWorldScheduledService {
	
    /** Default log. */
    <span class="code-keyword">protected</span> <span class="code-keyword">final</span>
Logger log = LoggerFactory.getLogger(<span class="code-keyword">this</span>.getClass());
    
    /** The scheduler <span class="code-keyword">for</span> rescheduling jobs.
     * @scr.reference */
    <span class="code-keyword">private</span> Scheduler scheduler;
    

    <span class="code-keyword">protected</span> void activate(ComponentContext
componentContext) <span class="code-keyword">throws</span> Exception {
        <span class="code-comment">//<span class="code-keyword">case</span>
1: with addJob() method: executes the job every minute
</span>    	<span class="code-object">String</span> schedulingExpression
= <span class="code-quote">"0 * * * * ?"</span>;
    	<span class="code-object">String</span> jobName1 = <span class="code-quote">"case1"</span>;
    	Map&lt;<span class="code-object">String</span>, Serializable&gt;
config1 = <span class="code-keyword">new</span> HashMap&lt;<span class="code-object">String</span>,
Serializable&gt;();
    	<span class="code-object">boolean</span> canRunConcurrently = <span class="code-keyword">true</span>;
        <span class="code-keyword">final</span> <span class="code-object">Runnable</span>
job1 = <span class="code-keyword">new</span> <span class="code-object">Runnable</span>()
{
            <span class="code-keyword">public</span> void run() {
            	log.info(<span class="code-quote">"Executing job1"</span>);
            }
        };
        <span class="code-keyword">try</span> {
        	<span class="code-keyword">this</span>.scheduler.addJob(jobName1, job1,
config1, schedulingExpression, canRunConcurrently);
        } <span class="code-keyword">catch</span> (Exception e) {
            job1.run();
        }
    	
        <span class="code-comment">//<span class="code-keyword">case</span>
2: with addPeriodicJob(): executes the job every 3 minutes
</span>        <span class="code-object">String</span> jobName2 = <span
class="code-quote">"case2"</span>;
    	<span class="code-object">long</span> period = 180;
    	Map&lt;<span class="code-object">String</span>, Serializable&gt;
config2 = <span class="code-keyword">new</span> HashMap&lt;<span class="code-object">String</span>,
Serializable&gt;();
        <span class="code-keyword">final</span> <span class="code-object">Runnable</span>
job2 = <span class="code-keyword">new</span> <span class="code-object">Runnable</span>()
{
            <span class="code-keyword">public</span> void run() {
            	log.info(<span class="code-quote">"Executing job2"</span>);
            }
        };
        <span class="code-keyword">try</span> {
        	<span class="code-keyword">this</span>.scheduler.addPeriodicJob(jobName2,
job2, config2, period, canRunConcurrently);
        } <span class="code-keyword">catch</span> (Exception e) {
            job2.run();
        }

        <span class="code-comment">//<span class="code-keyword">case</span>
3: with fireJobAt(): executes the job at a specific date (date of deployment + delay of 30
seconds)
</span>        <span class="code-object">String</span> jobName3 = <span
class="code-quote">"case3"</span>;
    	<span class="code-keyword">final</span> <span class="code-object">long</span>
delay = 30*1000;
    	<span class="code-keyword">final</span> Date fireDate = <span class="code-keyword">new</span>
Date();
        fireDate.setTime(<span class="code-object">System</span>.currentTimeMillis()
+ delay);
    	Map&lt;<span class="code-object">String</span>, Serializable&gt;
config3 = <span class="code-keyword">new</span> HashMap&lt;<span class="code-object">String</span>,
Serializable&gt;();
        <span class="code-keyword">final</span> <span class="code-object">Runnable</span>
job3 = <span class="code-keyword">new</span> <span class="code-object">Runnable</span>()
{
            <span class="code-keyword">public</span> void run() {
            	log.info(<span class="code-quote">"Executing job3 at date: {} with a delay
of: {} seconds"</span>, fireDate, delay/1000);
            }
        };
        <span class="code-keyword">try</span> {
        	<span class="code-keyword">this</span>.scheduler.fireJobAt(jobName3,
job3, config3, fireDate);
        } <span class="code-keyword">catch</span> (Exception e) {
            job3.run();
        }
    }

    <span class="code-keyword">protected</span> void deactivate(ComponentContext
componentContext) {
        log.info(<span class="code-quote">"Deactivated, goodbye!"</span>);
    }

}
</pre>
</div></div>

<h2><a name="SchedulerService%28commonsscheduler%29-Examplesofjobsthatarescheduledbyleveragingthewhiteboardpattern"></a>Examples
of jobs that are scheduled by leveraging the whiteboard pattern </h2>

<p>The following examples show you how to define and schedule a job by leveraging the
whiteboard pattern.</p>

<h3><a name="SchedulerService%28commonsscheduler%29-Schedulingwithacronexpression"></a>Scheduling
with a cron expression </h3>

<p>The following job is executed every minute by setting <b>scheduler.expression</b>
to the cron expression <b>"0 * * * * ?"</b>:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">package</span> sling.docu.examples;

<span class="code-keyword">import</span> org.slf4j.Logger;
<span class="code-keyword">import</span> org.slf4j.LoggerFactory;

/**
 * @scr.component
 * @scr.service <span class="code-keyword">interface</span>=<span class="code-quote">"java.lang.<span
class="code-object">Runnable</span>"</span>
 * @scr.property name=<span class="code-quote">"scheduler.expression"</span> value=<span
class="code-quote">"0 * * * * ?"</span>
 */
<span class="code-keyword">public</span> class ScheduledCronJob <span class="code-keyword">implements</span>
<span class="code-object">Runnable</span> {

	/** Default log. */
    <span class="code-keyword">protected</span> <span class="code-keyword">final</span>
Logger log = LoggerFactory.getLogger(<span class="code-keyword">this</span>.getClass());
    
    <span class="code-keyword">public</span> void run() {
    	log.info(<span class="code-quote">"Executing a cron job (job#1) through the whiteboard
pattern"</span>);
    }
<span class="code-comment">//
</span>}
</pre>
</div></div>

<h3><a name="SchedulerService%28commonsscheduler%29-Schedulingatperiodictimes"></a>Scheduling
at periodic times </h3>

<p>The following job is executed every ten seconds by setting <b>scheduler.period</b>
to <b>10</b>:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">package</span> sling.docu.examples;

<span class="code-keyword">import</span> org.slf4j.Logger;
<span class="code-keyword">import</span> org.slf4j.LoggerFactory;

/**
 * @scr.component
 * @scr.service <span class="code-keyword">interface</span>=<span class="code-quote">"java.lang.<span
class="code-object">Runnable</span>"</span>
 * @scr.property name=<span class="code-quote">"scheduler.period"</span> value=<span
class="code-quote">"10"</span> type=<span class="code-quote">"<span class="code-object">Long</span>"</span>
 */
<span class="code-keyword">public</span> class ScheduledPeriodicJob <span class="code-keyword">implements</span>
<span class="code-object">Runnable</span> {

	/** Default log. */
    <span class="code-keyword">protected</span> <span class="code-keyword">final</span>
Logger log = LoggerFactory.getLogger(<span class="code-keyword">this</span>.getClass());
    
    <span class="code-keyword">public</span> void run() {
    	log.info(<span class="code-quote">"Executing a perodic job (job#2) through the
whiteboard pattern"</span>);
    }
<span class="code-comment">//
</span>}
</pre>
</div></div>
    </div>
    <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>
       <a href="http://cwiki.apache.org/confluence/display/SLINGxSITE/Scheduler+Service+%28commons+scheduler%29">View
Online</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/SLINGxSITE/Scheduler+Service+%28commons+scheduler%29?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
           </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message