<document>
<header>
<product>resin</product>
<title>PeriodicTask Tutorial</title>
<type>tutorial</type>
<tutorial-startpage>index.jsp</tutorial-startpage>
<description>
<p>
This tutorial demonstrates the creation of a PeriodicTask that performs a task
at intervals and collects statistics on it's performance.  An administration
interface to the task is provided, and while the task is active the user is
shown a "temporarily unavailable" page.  
</p>

<p>
The code for this tutorial provides a full featured example that can be used
as a cut-and-paste source for solving real problems. The PeriodicTask keeps
detailed statistics, an administration interface is provided with a servlet,
and the task can be executed either manually or automatically at timed
intervals.
</p>

</description>
</header>

<body>
<localtoc/>

<s1 title="Files in this tutorial">
<deftable>
  <tr><td><viewfile-link file="WEB-INF/classes/example/PeriodicTask.java"/>
    </td><td>The implementation of the class that performs the task
</td></tr>
  <tr><td><viewfile-link file="WEB-INF/classes/example/PeriodicTaskMBean.java"/>
    </td><td>The Jmx MBean interface to the class that performs the task
</td>
  </tr>
  <tr><td><viewfile-link file="WEB-INF/classes/example/PeriodicTaskServlet.java"/>
    </td><td>An administrators view of the PeriodicTask.
    </td>
  </tr>
  <tr><td><viewfile-link file="WEB-INF/classes/example/PeriodicTaskFilter.java"/>
    </td><td>A filter that responds with a "temporarily unavailable" page when the
        task is running.
</td>
  </tr>
  <tr><td><viewfile-link file="WEB-INF/web.xml"/>
    </td><td>web-app configuration
</td>
  </tr>
  <tr><td><viewfile-link file="index.jsp"/>
    </td><td>A page representative of any page in the application.
</td>
  </tr>
  <tr><td><viewfile-link file="unavailable.jsp"/>
    </td><td>A page to show when the web application is unavailable because 
        the task is active.
</td>
  </tr>
</deftable>
</s1>


<s1 title="bean Configuration">

<p>
The <a config-tag="bean"/> tag is used to configure an instance of
PeriodicTask, and store it with the Resin-IoC/WebBeans registry.
</p>

<p>
PeriodicTask is implemented as a regular Java object that follows the
JavaBeans convention for setters and getters. The configuration of the object  
occurs in the web.xml, using Resin's <a href="doc|resin-ioc.xtp">Bean-style
initialization</a>.  
</p>

<example title="WEB-INF/resin-web.xml" language="java">
&lt;web-app xmlns="http://caucho.com/ns/resin">

  &lt;bean class="example.PeriodicTask">
    &lt;init&gt;
      &lt;estimated-average-time&gt;5&lt;/estimated-average-time&gt;
    &lt;/init&gt;
  &lt;/bean&gt;

&lt;/web-app>
</example>

<p>
If only this step were performed, the object would sit in the JVM memory,
performing no function.  The rest of this tutorial details various ways that
the PeriodicTask can be utilized.  
</p>

</s1>

<s1 title="Using PeriodicTask for real work">
<p>
This tutorial performs no real work in it's task, it sleeps for 10 seconds to
imitate a task that takes ten seconds.  The code can be used without
modification to perform real tasks for your web application.  All of the
statistics gathering and other functionality is available for the derived
class.
</p>

<s2 title="Extend PeriodicTask">

<p>
The following example extends PeriodicTask to create a task that
vacuum's a postgress database.
</p>

<example title="WEB-INF/classes/example/VacuumTask.java">
package example;

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

import java.sql.*;
import javax.sql.*;

public class VacuumTask extends PeriodicTask {
  static protected final Logger log = 
    Logger.getLogger(VacuumTask.class.getName());

  private DataSource _dataSource;

  public void setDataSource(DataSource dataSource)
  { 
    _dataSource = dataSource;
  }

  public void init()
    throws Exception
  {
    if (_dataSource == null)
      throw new Exception("'data-source' is required.");
  }

  protected void performTask()
    throws Exception
  {
    Connection conn = null;
    try {
      conn = _dataSource.getConnection();

      Statement stmt = conn.createStatement();
      stmt.executeUpdate("VACUUM FULL ANALYZE;");
      stmt.close();
    } finally {
      try {
        if (conn != null)
          conn.close();
      } catch (SQLException ex) {
        log.warning(ex.toString());
      }
    }
  }
}
</example>

<p>
The VacuumTask is configured to run automatically every night at midnight.
Since the database is integral to the application, the PeriodicTaskFilter is
used to redirect users to an "unavailable" page while the task active.
The PeriodicTaskServlet is used to provide an administration interface to the
task at the url <code>/admin/example/vacuum</code>.  
</p>

<p>
A <a config-tag="security-constraint"/> is used to limit access to the
<code>/admin/</code> portion of the website.  An <a config-tag="authenticator"/> is used to specify the user names and passwords.
</p>


<example title="WEB-INF/web.xml">
&lt;web-app xmlns="http://caucho.com/ns/resin"&gt;
  &lt;bean class="example.VacuumTask">
    &lt;init&gt;
      &lt;estimated-average-time&gt;30&lt;/estimated-average-time&gt;
    &lt;/init&gt;
  &lt;/bean&gt;

  &lt;servlet-mapping url-pattern='/admin/example/vacuum'&gt;
    &lt;servlet-class&gt;example.PeriodicTaskServlet&lt;/servlet-class&gt;
    &lt;init&gt;
      &lt;periodic-task&gt;${vacuumTask}&lt;/periodic-task&gt;
    &lt;/init&gt;
  &lt;/servlet&gt;

  &lt;bean class="com.caucho.resources.CronResource"&gt;
    &lt;init&gt;
      &lt;!-- run every day at 0215 local time --&gt;
      &lt;cron&gt;15 2 *&lt;/cron&gt;
      &lt;work&gt;${vacuumTask}&lt;/work&gt;
    &lt;/init&gt;
  &lt;/bean&gt;

  &lt;filter url-regexp="^(?!/admin)+"&gt;
    &lt;filter-class&gt;example.PeriodicTaskFilter&lt;/filter-class&gt;
    &lt;init&gt;
      &lt;periodic-task&gt;${VacuumTask}&lt;/periodic-task&gt;
      &lt;url&gt;/unavailable.jsp&lt;/url&gt;
    &lt;/init&gt;
  &lt;/filter&gt;

  &lt;!-- require 'admin' role --&gt;
  &lt;security-constraint&gt;
    &lt;web-resource-collection&gt;
      &lt;web-resource-name&gt;Admin&lt;/web-resource-name&gt;
      &lt;url-pattern&gt;/admin/*&lt;/url-pattern&gt;
    &lt;/web-resource-collection&gt;
    &lt;auth-constraint&gt;
      &lt;role-name&gt;admin&lt;/role-name&gt;
    &lt;/auth-constraint&gt;
  &lt;/security-constraint&gt;

 &lt;authenticator uri="xml:"&gt;
    &lt;init&gt;
      &lt;!-- user 'admin' with password 'excellent' is in admin role --&gt;
      &lt;user&gt;admin:FKT/gZPYJ5TIA5uA434mgA==:admin&lt;/user&gt;
    &lt;/init&gt;
  &lt;/authenticator&gt;
&lt;/web-app&gt;
</example>
</s2>
</s1>

<s1 title="Jmx: a management interface for the resource">
<p>
<a href="doc|jmx.xtp">Jmx</a> provides a mechanism for management of objects
on servers.  The PeriodicTask implements the PeriodicTaskMBean interface, the 
PeriodicTaskMBean interface is the Jmx agent's view of the resource.
</p>

<p>
Jmx uses a naming scheme to store objects for later retrieval.  The Jmx name
to store the object under is specified with the <a config-tag="mbean-name"/>
child of <a config-tag="bean"/>.  
</p>

<example title="Example: WEB-INF/resin-web.xml" language="java">
&lt;web-app xmlns="http://caucho.com/ns/resin">

  &lt;bean class="example.PeriodicTask" name="PeriodicTask"
            mbean-interface="example.PeriodicTaskMBean"
            <b>mbean-name="type=PeriodicTask,name=PeriodicTask"&gt;</b>
    &lt;init&gt;
      &lt;estimated-average-time&gt;5&lt;/estimated-average-time&gt;
    &lt;/init&gt;
  &lt;/bean&gt;

&lt;/web-app>
</example>

<p>
A simple Jmx lookup of all <code>type=PeriodicTask</code> objects is
done in admin/mbean.jmx: 
</p>

<example title="Example: admin/mbean.jsp">
  ObjectName query = new ObjectName("resin:type=PeriodicTask,*");
  pageContext.setAttribute("mbeans", Jmx.query(query));

  ...

&lt;c:forEach var="mbean" items="${mbeans}"&gt;

  estimatedAverageTime ${mbean.estimatedAverageTime}
  ...
</example>

</s1>


<s1 title="CronResource: timed execution of the PeriodicTask">
<p>
Depending on the nature of the PeriodicTask, it may be appropriate to configure
the task to automatically run at a specified time, or at a specified interval.
</p>

<p>
The <a href="doc|resources.xtp">CronResource</a> is used to configure
the timed execution of the PeriodicTask.
</p>

<example title="WEB-INF/web.xml" language="java">
  &lt;bean class="com.caucho.resources.CronResource"&gt;
    &lt;init&gt;
      &lt;cron&gt;*&lt;/cron&gt;
      &lt;work&gt;${PeriodicTask}&lt;/work&gt;
    &lt;/init&gt;
  &lt;/bean&gt;
</example>

</s1>

<s1 title="Filter: redirecting users when the task is running">

<p>
Again depending on the nature of the PeriodicTask, it may be appropriate for
access to the web application to be limited while the task is performed.  A
filter is used to provide this functionality, the filter intercepts 
requests and if the task is active, redirects to a page giving a "temporarily
unavailable" message.
</p>

<example title="WEB-INF/web.xml" language="java">
  &lt;filter&gt;
    &lt;filter-name&gt;PeriodicTaskFilter&lt;/filter-name&gt;
    &lt;filter-class&gt;example.PeriodicTaskFilter&lt;/filter-class&gt;
    &lt;init&gt;
      &lt;periodic-task&gt;${PeriodicTask}&lt;/periodic-task&gt;
      &lt;!-- optional url, if not specified a 503 response is sent. --&gt;
      &lt;url&gt;/unavailable.jsp&lt;/url&gt;
    &lt;/init&gt;
  &lt;/filter&gt;

  &lt;filter-mapping&gt;
    &lt;!-- regexp to match all urls except /admin and /index.xtp--&gt;
    &lt;url-regexp&gt;^(?!/admin|/index.xtp)+&lt;/url-regexp&gt;
    &lt;filter-name&gt;PeriodicTaskFilter&lt;/filter-name&gt;
  &lt;/filter-mapping&gt;
</example>

<example title="WEB-INF/classes/example/PeriodicTaskFilter.java" language="java">
  public void setPeriodicTask(PeriodicTask periodicTask)
  {
    _periodicTask = periodicTask;
  }
</example>

<example title="WEB-INF/classes/example/PeriodicTaskFilter.java" language="java">
  public void doFilter(ServletRequest request,
      ServletResponse response,
      FilterChain chain)
    throws ServletException, IOException
  {
    if (_periodicTask.isActive()) {
      dispatch( (HttpServletRequest) request, (HttpServletResponse) response);
    }
    else {
      chain.doFilter(request,response);
    }
  }
</example>
</s1>

<s1 title="PeriodicTaskServlet: an administration interface">
<p>
The PeriodicTaskServlet provides an html interface to the PeriodicTask.  It
shows the current status of the task, and provides a button to "Run" the task.
</p>

<p>
The PeriodicTaskServlet needs an instance of PeriodicTask to operate on.  
The Servlet provides a setter:
</p>

<example title="WEB-INF/classes/example/PeriodicTaskServlet.java" 
         language="java">
  public void setPeriodicTask(PeriodicTask periodicTask)
  {
    _periodicTask = periodicTask;
  }
</example>

<p>
The configuration of the servlet in web.xml uses the <code>${...}</code>
function to get a reference to the PeriodicTask resource previously stored in
with the <a config-tag="bean"/> configuration.  
</p>

<example title="WEB-INF/web.xml" language="java">
  &lt;servlet&gt;
    &lt;servlet-name&gt;PeriodicTaskServlet&lt;/servlet-name&gt;
    &lt;servlet-class&gt;example.PeriodicTaskServlet&lt;/servlet-class&gt;
    &lt;init&gt;
      &lt;periodic-task&gt;${PeriodicTask}&lt;/periodic-task&gt;
    &lt;/init&gt;
  &lt;/servlet&gt;

  &lt;servlet-mapping&gt;
    &lt;url-pattern&gt;/admin/periodictask&lt;/url-pattern&gt;
    &lt;servlet-name&gt;PeriodicTaskServlet&lt;/servlet-name&gt;
  &lt;/servlet-mapping&gt;

</example>
 
</s1>


<s1 name="dependency-injection" title="Dependency Injection/Inversion of Control">

<p>
<i>Dependency injection</i> is a term used to describe a separation between the
implementation of an object and the construction of an object it depends on, and
the ability for a container (like Resin) to resolve the dependency.
</p>
 
<p>
In this tutorial, many components including the administration servlet, the
filter, and the CronResource, depend upon a <code>PeriodicTask</code>.
Each of these components that depend upon the PeriodidicTask provide a setter:
</p>

<example title="setter">
  PeriodicTask _periodTask;

  public void setPeriodicTask(PeriodicTask periodicTask)
  {
    _periodicTask = periodicTask;
  }
</example>

<p>The container (Resin), injects the object.</p>

<example title="container injection">
  &lt;init&gt;
    &lt;periodic-task&gt;${PeriodicTask}&lt;/periodic-task&gt;
  &lt;/init&gt;
</example>

<p>
The simplicity of the code shows immediate benefits.  There is no dependency
on the environment (needing an <i>application</i> object for example), and no
need for cumbersome or error prone code in each component.

</p><p>
There are other benefits as well.   Since the container instantiates and sets
the object, there is more flexibility in the configuration.  The following
example shows the use of two distinct periodic tasks.
</p>

<p>An example illustrates some of the flexibility of dependency injection.
</p>

<p>
The first task (Foo) is only run manually, it is not run at a timed interval so
the CronResource is not used for it.  Neither task causes the application to
become unavailable, so the PeriodicTaskFilter is not used.  
</p>

<example title="two distinct periodic tasks">
<b>WEB-INF/classes/example/PeriodicTaskFoo.java</b>

public class PeriodicTaskFoo extends PeriodicTask {
  protected void performTask()
    throws Exception
  {
   ...
  }

}

<b>WEB-INF/classes/example/PeriodicTaskBar.java</b>

public class PeriodicTaskBar extends PeriodicTask {
  protected void performTask()
    throws Exception
  {
   ...
  }

}

<b>WEB-INF/web.xml</b>

  &lt;!-- TASK FOO --&gt;

  &lt;bean class="example.PeriodicTaskFoo" name="PeriodicTaskFoo"&gt;
    &lt;init&gt;
      &lt;estimated-average-time&gt;15&lt;/estimated-average-time&gt;
    &lt;/init&gt;
  &lt;/bean&gt;

  &lt;servlet&gt;
    &lt;servlet-name&gt;PeriodicTaskFooServlet&lt;/servlet-name&gt;
    &lt;servlet-class&gt;example.PeriodicTaskServlet&lt;/servlet-class&gt;
    &lt;init&gt;
      &lt;periodic-task&gt;${PeriodicTaskFoo}&lt;/periodic-task&gt;
    &lt;/init&gt;
  &lt;/servlet&gt;

  &lt;servlet-mapping&gt;
    &lt;url-pattern&gt;/admin/example/foo&lt;/url-pattern&gt;
    &lt;servlet-name&gt;PeriodicTaskFooServlet&lt;/servlet-name&gt;
  &lt;/servlet-mapping&gt;

  &lt;!-- TASK BAR --&gt;

  &lt;bean class="example.PeriodicTaskBar" name="PeriodicTaskBar"&gt;
    &lt;init&gt;
      &lt;estimated-average-time&gt;15&lt;/estimated-average-time&gt;
    &lt;/init&gt;
  &lt;/bean&gt;

  &lt;bean class="example.PeriodicTaskBar" name="PeriodicTaskBar"&gt;
    &lt;init&gt;
      &lt;estimated-average-time&gt;15&lt;/estimated-average-time&gt;
    &lt;/init&gt;
  &lt;/bean&gt;

  &lt;servlet&gt;
    &lt;servlet-name&gt;PeriodicTaskBarServlet&lt;/servlet-name&gt;
    &lt;servlet-class&gt;example.PeriodicTaskServlet&lt;/servlet-class&gt;
    &lt;init&gt;
      &lt;periodic-task&gt;${PeriodicTaskBar}&lt;/periodic-task&gt;
    &lt;/init&gt;
  &lt;/servlet&gt;

  &lt;servlet-mapping&gt;
    &lt;url-pattern&gt;/admin/example/bar&lt;/url-pattern&gt;
    &lt;servlet-name&gt;PeriodicTaskBarServlet&lt;/servlet-name&gt;
  &lt;/servlet-mapping&gt;

  &lt;!-- bar runs every minute --&gt;
  &lt;bean class="com.caucho.resources.CronResource"&gt;
    &lt;init&gt;
      &lt;cron&gt;*&lt;/cron&gt;
      &lt;work&gt;${PeriodicTaskBar}&lt;/work&gt;
    &lt;/init&gt;
  &lt;/bean&gt;
</example>

</s1> <!-- dependency-injection -->

<s1 title="See Also">
<dl>
<dt><a href="doc|resources.xtp">CronResource</a></dt>
<dd>Documentation for the usage of CronResource
</dd></dl>
</s1>

</body></document>
