Resource Timers
Resin 3.0

Features
Installation
Configuration
Web Applications
IOC/AOP
Resources
JSP
Servlets and Filters
Portlets
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 Work
Tutorials
JCA Cron

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

Resources can use the JCA timer capability to manage periodic tasks. The timers use the familiar java.util.Timer, providing extra support for the environment lifecycle.

Timers start short tasks. Longer timed tasks will use the timer in combination with the JCA work management API.

  1. Files in this tutorial
  2. The Timer task
  3. The work task
  4. The Resource
  5. Configuration and JSP

Files in this tutorial

WEB-INF/web.xml Configures the TimerResource
WEB-INF/classes/example/TimerResource.java The resource implementation registers a launching task with the Timer and provides common state.
WEB-INF/classes/example/WorkScheduleTimerTask.java The timer task which launches the work task.
WEB-INF/classes/example/WorkTask.java The work task executes the long task.
index.jsp The starting page for the tutorial

The Timer task

The java.util.Timer provides an API to launch periodic tasks. Because the timer tasks are expected to be short, the timer needs to launch a longer-lived Work task for the actual work. The work tutorial gives more information on the JCA Work API.

Resin provides the Timer to the resource's start and shuts it down properly when the resource environment closes (web-app, host, server, etc.) In other words, resources should not create java.util.Timer objects directly, but should use the BootstrapContext to create the timers.

The TimerTask API resembles the familiar Runnable API for threads. Most application will just need to implement the run() method for the task's code.

WorkScheduleTimerTask.java
public class WorkScheduleTimerTask extends java.util.TimerTask {
  private static final Logger log =
    Logger.getLogger(TimerTask.class.getName());

  private WorkManager _workManager;
  private Work _work;

  WorkScheduleTimerTask(WorkManager workManager, Work work)
  {
    _workManager = workManager;
    _work = work;
  }

  public void run()
  {
    try {
      _workManager.scheduleWork(_work);
    } catch (WorkException e) {
      log.log(Level.WARNING, e.toString(), e);
    }
  }
}

The work task

For this example, the work task is trivial. It just increments a counter in the TimerResource.

WorkTask.java
public class WorkTask implements Work {
  private TimerResource _resource;

  WorkTask(TimerResource resource)
  {
    _resource = resource;
  }

  public void run()
  {
    _resource.addCount();
  }

  public void release()
  {
  }
}

The Resource

The TimerResource in this example just registers the timer tasks and exits. As before, the resource extends com.caucho.jca.AbstractResourceAdapter to simplify the example.

TimerResource.java
public class TimerResource extends AbstractResourceAdapter {
  private int _count;
  
  /**
   * Adds to the count.
   */
  public void addCount()
  {
    _count++;
  }

  public void start(BootstrapContext ctx)
    throws ResourceAdapterInternalException
  {
    WorkManager workManager = ctx.getWorkManager();
    
    Work work = new WorkTask(this);

    TimerTask timerTask = new WorkScheduleTimerTask(workManager, work);

    Timer timer = ctx.createTimer();

    long initialDelay = 0;
    long period = 10000L;

    timer.schedule(timerTask, initialDelay, period);
  }
  
  public void stop()
    throws ResourceAdapterInternalException
  {
  }

  public String toString()
  {
    return "TimerResource[" + _count + "]";
  }
}

Configuration and JSP

The configuration for this resource is trivial since it has no attributes.

web.xml
<resource name="jca/timer" type="example.TimerResource"/>

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/jca/timer") %>

Try the Tutorial


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