CronResource
Resin 3.1

Documentation
Examples
Changes

Overview
Installation
Configuration
Quercus
SOA/IoC
JSP
Servlets and Filters
Admin (JMX)
EJB
Amber
Security
Performance
Hessian
XML and XSLT
Third-party
Troubleshooting/FAQ

JAXB Annotations
IoC Annotations
JAX-WS Annotations
JMS Configuration
init
bean
CronResource
RMI
Resource FAQ
bean
SOA/IoC
RMI

CronResource executes application Work tasks at configured intervals.

com.caucho.resources.CronResource

Often, applications need to run a task at specific times. The CronResource provides a standard way of doing that. Applications need only create a standard java.lang.Runnable task and configure the CronResource. Resin configure's the work task with bean-style configuration.

Task running every 15 minutes
<web-app xmlns="http://caucho.com/ns/resin">
  <resource type="com.caucho.resources.CronResource">
    <init>
      <cron>*/15</cron>
      <work resin:type="example.PeriodicWork">
        <foo>Custom Config</foo>
      </work>
    </init>
  </resource>
</web-app>
cronSpecifies the times the task should be runrequired
workSpecifies application's work beanrequired

The cron specification follows the Unix crontab format. The cron is composed of 5 fields: minutes, hours, day of month, month, and day of week.

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
15 2 *run every day at 0215 local time
0 0 */3run every third day at midnight
15 0 * * 6run every Saturday at 0015

Hello, World example

WEB-INF/web.xml

<web-app xmlns="http://caucho.com/ns/resin">
  <resource type="com.caucho.resources.CronResource">
    <init>
      <!-- every minute -->
      <cron>*</cron>
      <work resin:type="example.PeriodicWork"/>
    </init>
  </resource>
</web-app>
WEB-INF/classes/example/PeriodicWork.java

package example;

import java.util.logging.Level;
import java.util.logging.Logger;

public class PeriodicWork implements Runnable {
  static protected final Logger log = 
    Logger.getLogger(PeriodicWork.class.getName());

  public PeriodicWork()
  {
    log.info("PeriodicWork: constructor");
  }

  /**
   * Required implementation of java.lang.Runnable.run()
   */
  public void run()
  {
    log.info("PeriodicWork: run() Hello, World");
  }
}
[13:04:27.429] PeriodicWork: constructor
[13:05:00.095] PeriodicWork: run() Hello, World
[13:06:00.182] PeriodicWork: run() Hello, World

bean-style configuration example

WEB-INF/web.xml

<web-app xmlns="http://caucho.com/ns/resin">
  <resource type="com.caucho.resources.CronResource">
    <init>
      <!-- every minute -->
      <cron>*</cron>
      <work resin:type="example.PeriodicWork">
        <message>Goodybye, World</message>
      </work>
    </init>
  </resource>
</web-app>
WEB-INF/classes/example/PeriodicWork.java

package example;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.resource.spi.work.Work;

public class PeriodicWork implements Work {
  static protected final Logger log = 
    Logger.getLogger(PeriodicWork.class.getName());

  String _message;

  public PeriodicWork()
  {
    log.info("PeriodicWork: constructor");
  }

  /**
   * Optional, called in response to presence of <message>
   * configuration tag. 
   */
  public void setMessage(String message)
  {
    log.info("PeriodicWork: setMessage");
    _message = message;
  }

  /**
   * Optional, called after bean is created and any setters
   * from configuration are called.
   */
  @PostConstruct
  public void init()
    throws Exception
  {
    log.info("PeriodicWork: init()");

    if (_message == null)
      throw new Exception("`message' is required");
  }

  /**
   * Required implementation of java.lang.Runnable.run()
   */
  public void run()
  {
    log.info("PeriodicWork: run() " + _message);
  }

  /**
   * Implementation of javax.resource.spi.work.Work.release()
   */
  public void release()
  {
    log.info("PeriodicWork: release()");
  }
}
[13:04:27.429] PeriodicWork: constructor
[13:04:27.429] PeriodicWork: setMessage
[13:04:27.429] PeriodicWork: init()
[13:05:00.095] PeriodicWork: run() Goodbye, World
[13:06:00.182] PeriodicWork: run() Goodbye, World
(close Resin)
[13:06:00.345] PeriodicWork: release()

bean
SOA/IoC
RMI
Copyright © 1998-2006 Caucho Technology, Inc. All rights reserved.
Resin ® is a registered trademark, and Quercustm, Ambertm, and Hessiantm are trademarks of Caucho Technology.