Caucho maker of Resin Server | Application Server (Java EE Certified) and Web Server


 

Resin Documentation

home company blog wiki docs 
app server web server 
health cloud java ee pro 
 Resin Server | Application Server (Java EE Certified) and Web Server
 

periodictask tutorial


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.

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.

Files in this tutorial

WEB-INF/classes/example/PeriodicTask.javaThe implementation of the class that performs the task
WEB-INF/classes/example/PeriodicTaskMBean.javaThe Jmx MBean interface to the class that performs the task
WEB-INF/classes/example/PeriodicTaskServlet.javaAn administrators view of the PeriodicTask.
WEB-INF/classes/example/PeriodicTaskFilter.javaA filter that responds with a "temporarily unavailable" page when the task is running.
WEB-INF/web.xmlweb-app configuration
index.jspA page representative of any page in the application.
unavailable.jspA page to show when the web application is unavailable because the task is active.

bean Configuration

The bean tag is used to configure an instance of PeriodicTask, and store it with the Resin-IoC/WebBeans registry.

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 Bean-style initialization.

WEB-INF/resin-web.xml
<web-app xmlns="http://caucho.com/ns/resin">

  <bean class="example.PeriodicTask">
    <init>
      <estimated-average-time>5</estimated-average-time>
    </init>
  </bean>

</web-app>

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.

Using PeriodicTask for real work

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.

Extend PeriodicTask

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

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());
      }
    }
  }
}

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 /admin/example/vacuum.

A security-constraint is used to limit access to the /admin/ portion of the website. An authenticator is used to specify the user names and passwords.

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

  <servlet-mapping url-pattern='/admin/example/vacuum'>
    <servlet-class>example.PeriodicTaskServlet</servlet-class>
    <init>
      <periodic-task>${vacuumTask}</periodic-task>
    </init>
  </servlet>

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

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

  <!-- require 'admin' role -->
  <security-constraint>
    <web-resource-collection>
      <web-resource-name>Admin</web-resource-name>
      <url-pattern>/admin/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
      <role-name>admin</role-name>
    </auth-constraint>
  </security-constraint>

 <authenticator uri="xml:">
    <init>
      <!-- user 'admin' with password 'excellent' is in admin role -->
      <user>admin:FKT/gZPYJ5TIA5uA434mgA==:admin</user>
    </init>
  </authenticator>
</web-app>

Jmx: a management interface for the resource

Jmx 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.

Jmx uses a naming scheme to store objects for later retrieval. The Jmx name to store the object under is specified with the mbean-name child of bean.

Example: WEB-INF/resin-web.xml
<web-app xmlns="http://caucho.com/ns/resin">

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

</web-app>

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

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

  ...

<c:forEach var="mbean" items="${mbeans}">

  estimatedAverageTime ${mbean.estimatedAverageTime}
  ...

CronResource: timed execution of the PeriodicTask

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.

The CronResource is used to configure the timed execution of the PeriodicTask.

WEB-INF/web.xml
  <bean class="com.caucho.resources.CronResource">
    <init>
      <cron>*</cron>
      <work>${PeriodicTask}</work>
    </init>
  </bean>

Filter: redirecting users when the task is running

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.

WEB-INF/web.xml
  <filter>
    <filter-name>PeriodicTaskFilter</filter-name>
    <filter-class>example.PeriodicTaskFilter</filter-class>
    <init>
      <periodic-task>${PeriodicTask}</periodic-task>
      <!-- optional url, if not specified a 503 response is sent. -->
      <url>/unavailable.jsp</url>
    </init>
  </filter>

  <filter-mapping>
    <!-- regexp to match all urls except /admin and /index.xtp-->
    <url-regexp>^(?!/admin|/index.xtp)+</url-regexp>
    <filter-name>PeriodicTaskFilter</filter-name>
  </filter-mapping>
WEB-INF/classes/example/PeriodicTaskFilter.java
  public void setPeriodicTask(PeriodicTask periodicTask)
  {
    _periodicTask = periodicTask;
  }
WEB-INF/classes/example/PeriodicTaskFilter.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);
    }
  }

PeriodicTaskServlet: an administration interface

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.

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

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

The configuration of the servlet in web.xml uses the ${...} function to get a reference to the PeriodicTask resource previously stored in with the bean configuration.

WEB-INF/web.xml
  <servlet>
    <servlet-name>PeriodicTaskServlet</servlet-name>
    <servlet-class>example.PeriodicTaskServlet</servlet-class>
    <init>
      <periodic-task>${PeriodicTask}</periodic-task>
    </init>
  </servlet>

  <servlet-mapping>
    <url-pattern>/admin/periodictask</url-pattern>
    <servlet-name>PeriodicTaskServlet</servlet-name>
  </servlet-mapping>

Dependency Injection/Inversion of Control

Dependency injection 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.

In this tutorial, many components including the administration servlet, the filter, and the CronResource, depend upon a PeriodicTask. Each of these components that depend upon the PeriodidicTask provide a setter:

setter
  PeriodicTask _periodTask;

  public void setPeriodicTask(PeriodicTask periodicTask)
  {
    _periodicTask = periodicTask;
  }

The container (Resin), injects the object.

container injection
  <init>
    <periodic-task>${PeriodicTask}</periodic-task>
  </init>

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

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.

An example illustrates some of the flexibility of dependency injection.

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.

two distinct periodic tasks
WEB-INF/classes/example/PeriodicTaskFoo.java

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

}

WEB-INF/classes/example/PeriodicTaskBar.java

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

}

WEB-INF/web.xml

  <!-- TASK FOO -->

  <bean class="example.PeriodicTaskFoo" name="PeriodicTaskFoo">
    <init>
      <estimated-average-time>15</estimated-average-time>
    </init>
  </bean>

  <servlet>
    <servlet-name>PeriodicTaskFooServlet</servlet-name>
    <servlet-class>example.PeriodicTaskServlet</servlet-class>
    <init>
      <periodic-task>${PeriodicTaskFoo}</periodic-task>
    </init>
  </servlet>

  <servlet-mapping>
    <url-pattern>/admin/example/foo</url-pattern>
    <servlet-name>PeriodicTaskFooServlet</servlet-name>
  </servlet-mapping>

  <!-- TASK BAR -->

  <bean class="example.PeriodicTaskBar" name="PeriodicTaskBar">
    <init>
      <estimated-average-time>15</estimated-average-time>
    </init>
  </bean>

  <bean class="example.PeriodicTaskBar" name="PeriodicTaskBar">
    <init>
      <estimated-average-time>15</estimated-average-time>
    </init>
  </bean>

  <servlet>
    <servlet-name>PeriodicTaskBarServlet</servlet-name>
    <servlet-class>example.PeriodicTaskServlet</servlet-class>
    <init>
      <periodic-task>${PeriodicTaskBar}</periodic-task>
    </init>
  </servlet>

  <servlet-mapping>
    <url-pattern>/admin/example/bar</url-pattern>
    <servlet-name>PeriodicTaskBarServlet</servlet-name>
  </servlet-mapping>

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

See Also

CronResource
Documentation for the usage of CronResource

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

Cloud-optimized Resin Server is a Java EE certified Java Application Server, and Web Server, and Distributed Cache Server (Memcached).
Leading companies worldwide with demand for reliability and high performance web applications including SalesForce.com, CNET, DZone and many more are powered by Resin.

home company blog wiki docs 
app server web server 
health cloud java ee pro