Scheduled tasks

From Resin 3.0

Revision as of 08:39, 17 December 2009 by Reza (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

<document>

 <header>
   <title>Scheduled Task</title>
   <type>contents</type>
   <description>

Resin's <resin:ScheduledTask> capability lets you schedule events using a flexible cron-style trigger. The task can be any Runnable bean, a method specified by EL, or a URL.

   </description>
 </header>
 <body>

<localtoc/>

<s1 title="<resin:ScheduledTask>"> version="Resin 4.0.0">

<resin:ScheduledTask> schedules a job to be executed at specific times or after specific delays. The times can be specified by a cron syntax or by a simple delay parameter. The job can be either a Runnable bean, a method specified by an EL expression, or a URL.

When specified as an Java Injection bean, the bean task has full IoC capabilities, including injection, @TransactionAttribute aspects, interception and @Observes.

<deftable title="<resin:ScheduledTask> Attributes"> <tr>

 <th>Attribute</th>
 <th>Description</th>

</tr> <tr>

 <td>class</td>
 <td>the classname of the singleton bean to create</td>

</tr> <tr>

 <td>cron</td>
 <td>a cron-style scheduling description</td>

</tr> <tr>

 <td>delay</td>
 <td>a simple delay-based execution</td>

</tr> <tr>

 <td>init</td>
 <td>IoC initialization for the bean</td>

</tr> <tr>

 <td>mbean-name</td>
 <td>optional MBean name for JMX registration</td>

</tr> <tr>

 <td>method</td>
 <td>EL expression for a method to be invoked as the task</td>

</tr> <tr>

 <td>name</td>
 <td>optional IoC name for registering the task</td>

</tr> <tr>

 <td>period</td>
 <td>how often the task should be invoked in simple mode</td>

</tr> <tr>

 <td>task</td>
 <td>alternate task assignment for predefined beans</td>

</tr> </deftable>

<s2 title="Java Injection bean job configuration">

The most common and flexible job configuration uses standard IoC bean-style configuration. The bean must implement Runnable. The <task> element specifies the bean, using standard Java injection syntax as described in <a href="candi.xtp">Java Injection</a> configuration.

<example title="Example: 5min cron bean task"> <web-app xmlns="http://caucho.com/ns/resin"

     xmlns:resin="urn:java:com.caucho.resin">
 <resin:ScheduledTask>
   <cron>*/5</cron>
   <task>
     <qa:MyTask xmlns:qa="urn:java:com.caucho.resin"/>
   </task>
 </ScheduledTask>

</web-app> </example>

</s2>

<s2 title="task reference job configuration">

The task bean can also be passed to the <scheduled-task> using a Resin-IoC EL reference. The name of the task bean would be defined previously, either in a <bean> or <component> or picked up by classpath scanning. Like the bean-style job configuration, the reference bean must implement Runnable.

<example title="Example: midnight cron bean task"> <web-app xmlns="http://caucho.com/ns/resin"

        xmlns:resin="urn:java:com.caucho.resin">
 <resin:ScheduledTask task="#{taskBean}">
   <cron>0 0 *</cron>
 </resin:ScheduledTask>

</web-app> </example>

</s2>

<s2 title="method reference job configuration">

<scheduled-task> can execute a method on a defined bean as the scheduler's task. The method is specified using EL reference syntax. At each trigger time, <scheduled-task> will invoke the EL method expression.

In the following example, the task invokes myMethod() on the myBean singleton every 1 hour.

<example title="Example: 1h period method task"> <web-app xmlns="http://caucho.com/ns/resin"

 xmlns:resin="urn:java:com.caucho.resin"
 xmlns:qa="urn:java:qa">
 <qa:MyBean>
   <Named>myBean</Named>
 </qa:MyBean>
 <resin:ScheduledTask method="#{myBean.myMethod}">
   <resin:delay>10m</resin:delay>
   <resin:period>1h</resin:period>
 </resin:ScheduledTask>

</web-app> </example>

</s2>

<s2 title="url job configuration">

In a <web-app>, the <scheduled-task> can invoke a servlet URL at the trigger times. The task uses the servlet RequestDispatcher and forwards to the specified URL. The URL is relative to the <web-app> which contains the <scheduled-task.

<example title="Example: sunday cron url task"> <web-app xmlns="http://caucho.com/ns/resin"

 xmlns:resin="urn:java:com.caucho.config">
 <resin:ScheduledTask url="/cron.php">
   <resin:cron>0 15 * * 0</resin:cron>
 </resin:ScheduledTask>

</web-app> </example>

</s2>

<s2 title="cron trigger syntax">

Some ascii art from the <a href="http://en.wikipedia.org/wiki/Crontab">wikipedia cron entry</a>

<def title="cron fields">

  1. +---------------- minute (0 - 59)
  2. | +------------- hour (0 - 23)
  3. | | +---------- day of month (1 - 31)
  4. | | | +------- month (1 - 12)
  5. | | | | +---- day of week (0 - 6) (Sunday=0 or 7)
  6. | | | | |
 *  *  *  *  *

</def>

<deftable title="cron patterns"> <tr>

 <th>Pattern</th>
 <th>Description</th>

</tr> <tr>

 <td>*</td>
 <td>matches all time periods</td>

</tr> <tr>

 <td>15</td>
 <td>matches the specific time, e.g. 15 for minutes</td>

</tr> <tr>

 <td>15,45</td>
 <td>matches a list of times, e.g. every :15 and :45</td>

</tr> <tr>

 <td>*/5</td>
 <td>matches every n times, e.g. every 5 minutes</td>

</tr> <tr>

 <td>1-5</td>
 <td>matches a range of times, e.g. mon, tue, wed, thu, fri (1-5)</td>

</tr> </deftable>

Each field specifies a range of times to be executed. The patterns allowed are:

<deftable title="example ranges"> <tr>

 <th>range</th>
 <th>explanation (using minutes as example)</th>

</tr> <tr>

 <td>*</td>
 <td>run every minute</td>

</tr> <tr>

 <td>*/5</td>
 <td>run every 5 minutes</td>

</tr> <tr>

 <td>0,5,50</td>
 <td>run at :00, :05, :50 every hour</td>

</tr>

 <tr><td>0-4</td>
 <td>run at :00, :01, :02, :03, :04</td>

</tr> <tr>

 <td>0-30/2</td>
 <td>run every 2 minutes for the first half hour</td>

</tr> </deftable>

The minutes field is always required, and the hours, days, and months fields are optional.

<deftable title="example times"> <tr>

 <th>range</th>
 <th>explanation</th>

</tr> <tr>

 <td>0 */3</td>
 <td>run every 3 hours</td>

</tr> <tr>

 <td>15 2 *</td>
 <td>run every day at 0215 local time</td>

</tr> <tr>

 <td>0 0 */3</td>
 <td>run every third day at midnight</td>

</tr> <tr>

 <td>15 0 * * 6</td>
 <td>run every Saturday at 0015</td>

</tr> </deftable>

</s2>

</s1>

 </body>

</document>

<document>

 <header>
   <title>Scheduled Task</title>
   <type>contents</type>
   <description>

Resin's <resin:ScheduledTask> capability lets you schedule events using a flexible cron-style trigger. The task can be any Runnable bean, a method specified by EL, or a URL.

   </description>
 </header>
 <body>

<localtoc/>

<s1 title="<resin:ScheduledTask>"> version="Resin 4.0.0">

<resin:ScheduledTask> schedules a job to be executed at specific times or after specific delays. The times can be specified by a cron syntax or by a simple delay parameter. The job can be either a Runnable bean, a method specified by an EL expression, or a URL.

When specified as an Java Injection bean, the bean task has full IoC capabilities, including injection, @TransactionAttribute aspects, interception and @Observes.

<deftable title="<resin:ScheduledTask> Attributes"> <tr>

 <th>Attribute</th>
 <th>Description</th>

</tr> <tr>

 <td>class</td>
 <td>the classname of the singleton bean to create</td>

</tr> <tr>

 <td>cron</td>
 <td>a cron-style scheduling description</td>

</tr> <tr>

 <td>delay</td>
 <td>a simple delay-based execution</td>

</tr> <tr>

 <td>init</td>
 <td>IoC initialization for the bean</td>

</tr> <tr>

 <td>mbean-name</td>
 <td>optional MBean name for JMX registration</td>

</tr> <tr>

 <td>method</td>
 <td>EL expression for a method to be invoked as the task</td>

</tr> <tr>

 <td>name</td>
 <td>optional IoC name for registering the task</td>

</tr> <tr>

 <td>period</td>
 <td>how often the task should be invoked in simple mode</td>

</tr> <tr>

 <td>task</td>
 <td>alternate task assignment for predefined beans</td>

</tr> </deftable>

<s2 title="Java Injection bean job configuration">

The most common and flexible job configuration uses standard IoC bean-style configuration. The bean must implement Runnable. The <task> element specifies the bean, using standard Java injection syntax as described in <a href="candi.xtp">Java Injection</a> configuration.

<example title="Example: 5min cron bean task"> <web-app xmlns="http://caucho.com/ns/resin"

     xmlns:resin="urn:java:com.caucho.resin">
 <resin:ScheduledTask>
   <cron>*/5</cron>
   <task>
     <qa:MyTask xmlns:qa="urn:java:com.caucho.resin"/>
   </task>
 </ScheduledTask>

</web-app> </example>

</s2>

<s2 title="task reference job configuration">

The task bean can also be passed to the <scheduled-task> using a Resin-IoC EL reference. The name of the task bean would be defined previously, either in a <bean> or <component> or picked up by classpath scanning. Like the bean-style job configuration, the reference bean must implement Runnable.

<example title="Example: midnight cron bean task"> <web-app xmlns="http://caucho.com/ns/resin"

        xmlns:resin="urn:java:com.caucho.resin">
 <resin:ScheduledTask task="#{taskBean}">
   <cron>0 0 *</cron>
 </resin:ScheduledTask>

</web-app> </example>

</s2>

<s2 title="method reference job configuration">

<scheduled-task> can execute a method on a defined bean as the scheduler's task. The method is specified using EL reference syntax. At each trigger time, <scheduled-task> will invoke the EL method expression.

In the following example, the task invokes myMethod() on the myBean singleton every 1 hour.

<example title="Example: 1h period method task"> <web-app xmlns="http://caucho.com/ns/resin"

 xmlns:resin="urn:java:com.caucho.resin"
 xmlns:qa="urn:java:qa">
 <qa:MyBean>
   <Named>myBean</Named>
 </qa:MyBean>
 <resin:ScheduledTask method="#{myBean.myMethod}">
   <resin:delay>10m</resin:delay>
   <resin:period>1h</resin:period>
 </resin:ScheduledTask>

</web-app> </example>

</s2>

<s2 title="url job configuration">

In a <web-app>, the <scheduled-task> can invoke a servlet URL at the trigger times. The task uses the servlet RequestDispatcher and forwards to the specified URL. The URL is relative to the <web-app> which contains the <scheduled-task.

<example title="Example: sunday cron url task"> <web-app xmlns="http://caucho.com/ns/resin"

 xmlns:resin="urn:java:com.caucho.config">
 <resin:ScheduledTask url="/cron.php">
   <resin:cron>0 15 * * 0</resin:cron>
 </resin:ScheduledTask>

</web-app> </example>

</s2>

<s2 title="cron trigger syntax">

Some ascii art from the <a href="http://en.wikipedia.org/wiki/Crontab">wikipedia cron entry</a>

<def title="cron fields">

  1. +---------------- minute (0 - 59)
  2. | +------------- hour (0 - 23)
  3. | | +---------- day of month (1 - 31)
  4. | | | +------- month (1 - 12)
  5. | | | | +---- day of week (0 - 6) (Sunday=0 or 7)
  6. | | | | |
 *  *  *  *  *

</def>

<deftable title="cron patterns"> <tr>

 <th>Pattern</th>
 <th>Description</th>

</tr> <tr>

 <td>*</td>
 <td>matches all time periods</td>

</tr> <tr>

 <td>15</td>
 <td>matches the specific time, e.g. 15 for minutes</td>

</tr> <tr>

 <td>15,45</td>
 <td>matches a list of times, e.g. every :15 and :45</td>

</tr> <tr>

 <td>*/5</td>
 <td>matches every n times, e.g. every 5 minutes</td>

</tr> <tr>

 <td>1-5</td>
 <td>matches a range of times, e.g. mon, tue, wed, thu, fri (1-5)</td>

</tr> </deftable>

Each field specifies a range of times to be executed. The patterns allowed are:

<deftable title="example ranges"> <tr>

 <th>range</th>
 <th>explanation (using minutes as example)</th>

</tr> <tr>

 <td>*</td>
 <td>run every minute</td>

</tr> <tr>

 <td>*/5</td>
 <td>run every 5 minutes</td>

</tr> <tr>

 <td>0,5,50</td>
 <td>run at :00, :05, :50 every hour</td>

</tr>

 <tr><td>0-4</td>
 <td>run at :00, :01, :02, :03, :04</td>

</tr> <tr>

 <td>0-30/2</td>
 <td>run every 2 minutes for the first half hour</td>

</tr> </deftable>

The minutes field is always required, and the hours, days, and months fields are optional.

<deftable title="example times"> <tr>

 <th>range</th>
 <th>explanation</th>

</tr> <tr>

 <td>0 */3</td>
 <td>run every 3 hours</td>

</tr> <tr>

 <td>15 2 *</td>
 <td>run every day at 0215 local time</td>

</tr> <tr>

 <td>0 0 */3</td>
 <td>run every third day at midnight</td>

</tr> <tr>

 <td>15 0 * * 6</td>
 <td>run every Saturday at 0015</td>

</tr> </deftable>

</s2>

</s1>

 </body>

</document>

Personal tools