CronResource
Resin 3.0

Features
Installation
Configuration
Web Applications
IOC/AOP
Resources
JSP
Quercus
Servlets and Filters
Databases
Admin (JMX)
CMP
EJB
Amber
EJB 3.0
Security
XML and XSLT
XTP
JMS
Performance
Protocols
Third-party
Troubleshooting/FAQ

Library
Tutorials
Resource FAQ

Basic Resource
Injection
JCA Lifecycle
JCA Work
JCA Timer
JCA Cron
JCA Connector
JNDI appconfig
JCA Timer
Tutorials
JCA Connector

Find this tutorial in: /usr/local/resin/webapps/resin-doc/resource/tutorial/jca-cron
Try the Tutorial

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

  1. Files in this tutorial
  2. CronResource
  3. The work task
  4. JSP

Files in this tutorial

WEB-INF/web.xml Configures the CronResource
WEB-INF/classes/example/WorkTask.java The work task executes the long task.
index.jsp The 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") %>

Try the Tutorial


JCA Timer
Tutorials
JCA Connector
Copyright © 1998-2006 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.