<document>
<header>
<product>resin</product>
<type>tutorial</type>
<title>CronResource</title>

<description>
<p>CronResource is a convenient resource which schedules application Work
tasks at configurable intervals.</p>
</description>
 <tutorial-startpage>index.jsp</tutorial-startpage>

</header>
<body>


<summary/>

<s1 title="Files in this tutorial">
<deftable>
<tr>
  <th>File</th>
  <th>Description</th>
</tr>
<tr><td><viewfile-link file="WEB-INF/web.xml"/>
    </td><td>Configures the CronResource
</td></tr><tr><td><viewfile-link file="WEB-INF/classes/example/WorkTask.java"/>
    </td><td>The work task executes the long task.
</td></tr><tr><td><viewfile-link file="index.jsp"/>
    </td><td>The starting page for the tutorial
</td></tr></deftable>
</s1>

<s1 title="CronResource">

<p>Resin provides a convenience resource,
<code>com.caucho.resources.CronResource</code> to
execute <code>javax.resource.Work</code> tasks at configurable intervals.
Essentially, it implements the Timer/Work resource combination described
in the <a href="../jca-timer">timer tutorial</a>.</p>

<p>The <code>Work</code> 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.</p>

<p>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
<a href="doc|soa-cron.xtp#CronResource">CronResource
documentation.</a></p>

<example title="sample web.xml">
&lt;resource type="com.caucho.resources.CronResource"&gt;
  &lt;init&gt;
    &lt;cron&gt;*/5&lt;/cron&gt;

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

</s1>

<s1 title="The work task">

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

<example title="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()
  {
  }
}

</example>

</s1>

<s1 title="JSP">

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

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

</s1>

</body>
</document>