CronResource

CronResource is a convenient resource which schedules application Work tasks at configurable intervals.

Demo

Files in this tutorial

WEB-INF/web.xmlConfigures the CronResource
WEB-INF/classes/example/WorkTask.javaThe work task executes the long task.
index.jspThe starting page for the tutorial

CronResource

Resin provides a convenience resource, com.caucho.resources.CronResource to execute javax.resource.Work tasks at configurable intervals. Essentially, it implements the Timer/Work resource combination described in the timer tutorial.

The Work task follows the standard JCA API; no Resin-specific code is required. The tutorial example stores a string in JNDI to be retrieved by the example JSP.

The cron times follow the Unix crontab format. The tutorial schedules the work task every 5 minutes, using the "*/5" pattern. More details for the cron specificiations is available in the CronResource documentation.

sample web.xml
<resource type="com.caucho.resources.CronResource">
  <init>
    <cron>*/5</cron>

    <work resin:type="example.WorkTask">
      <value>Example</value>
      <jndi>java:comp/env/example</jndi>
    </work>
  </init>
</resource>

The work task

For this example, the work task is trivial. It just sets a string in JNDI.

WorkTask.java
public class WorkTask implements Work {
  private String _value = "default";
  private String _jndi = "java:comp/env/test";

  public void setValue(String value)
  {
    _value = value;
  }

  public void setJNDI(String value)
  {
    _jndi = jndi;
  }

  public void run()
  {
    try {
      new InitialContext().rebind(_jndi, _value + ": " + new Date());
    } catch (Throwable e) {
      e.printStackTrace();
    }
  }

  public void release()
  {
  }
}

JSP

The demo JSP is also trivial. It looks up the resource through JNDI and prints it to the page.

index.jsp
<%@ page import="javax.naming.*" %>
<%= new InitialContext().lookup("java:comp/env/example") %>

Demo


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