Caucho maker of Resin Server | Application Server (Java EE Certified) and Web Server


 

Resin Documentation

home company docs 
app server 
 Resin Server | Application Server (Java EE Certified) and Web Server
 

scheduled task


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

<resin:ScheduledTask>

<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.

<resin:ScheduledTask> Attributes
ATTRIBUTEDESCRIPTION
crona cron-style scheduling description
delaya simple delay-based execution
mbean-nameoptional MBean name for JMX registration
methodEL expression for a method to be invoked as the task
nameoptional IoC name for registering the task
periodhow often the task should be invoked in simple mode
taskalternate task assignment for predefined beans

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 Java Injection configuration.

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

  <resin:ScheduledTask>
    <resin:cron>*/5</resin:cron>

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

</web-app>

task reference job configuration

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

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

  <resin:ScheduledTask task="#{taskBean}">
    <resin:cron>0 0 *</resin:cron>
  </resin:ScheduledTask>

</web-app>

method reference job configuration

ScheduledTask 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, ScheduledTask will invoke the EL method expression.

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

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>

url job configuration

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

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

  <resin:ScheduledTask url="/cron.php">
    <resin:cron>0 15 * * 0</resin:cron>
  </resin:ScheduledTask>

</web-app>

cron trigger syntax

Some ascii art from the wikipedia cron entry

cron fields
# +---------------- minute (0 - 59)
# |  +------------- hour (0 - 23)
# |  |  +---------- day of month (1 - 31)
# |  |  |  +------- month (1 - 12)
# |  |  |  |  +---- day of week (0 - 6) (Sunday=0 or 7)
# |  |  |  |  |
  *  *  *  *  *
cron patterns
PATTERNDESCRIPTION
*matches all time periods
15matches the specific time, e.g. 15 for minutes
15,45matches a list of times, e.g. every :15 and :45
*/5matches every n times, e.g. every 5 minutes
1-5matches a range of times, e.g. mon, tue, wed, thu, fri (1-5)

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

example ranges
RANGEEXPLANATION (USING MINUTES AS EXAMPLE)
*run every minute
*/5run every 5 minutes
0,5,50run at :00, :05, :50 every hour
0-4run at :00, :01, :02, :03, :04
0-30/2run every 2 minutes for the first half hour

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

example times
RANGEEXPLANATION
0*/3run every 3 hours, on top of the hour
152*run every day at 0215 local time
00*/3run every third day at midnight
150**6run every Saturday at 0015

Copyright © 1998-2015 Caucho Technology, Inc. All rights reserved. Resin ® is a registered trademark. Quercustm, and Hessiantm are trademarks of Caucho Technology.