EQUIP2 WebApp Scheduled Tasks

Chris Greenhalgh, 2007-02-01

Introduction

Many interaction with a web application are driven by client (typically browser) HTTP requests. However, some actions need to occur within the server independently of client interaction, e.g. time-related game behaviours (e.g. time-based accumulation of credit), time-outs or sources of server push. This document gives an example of using the Quartz open source scheduler to trigger timed methods in the web application.

Scheduled tasks

The Spring servlet 'timer' is configured in WEB-INF/timer-servlet.xml.

A task, i.e. a method to be executed, is specified using a "Job" bean (i.e. what should be done):

	<bean id="exampleJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="timeHandler"/>
<property name="targetMethod" value="tick"/>
<property name="concurrent" value="false"/>
</bean>

This will call the method 'tick' on the bean 'timeHandler', which is an instance of class equip2.webapptutorial/timer.TimeHandler (also configured in timer-servlet.xml). In this case the tick method just adds one to the property 'tickCount' and set the property 'lastTickDate' to the current system time.

When this should be done is specified in a matching 'Tigger' bean:

	<bean id="exampleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="exampleJob"/>
<property name="startDelay" value="1000"/>
<property name="repeatInterval" value="1000"/>
</bean>

In this case, the job is to be scheduled after 1000ms, every 1000ms.

The Quartz scheduler itself is created and its triggers/job specified as follows:

	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="exampleTrigger"/>
</list>
</property>
</bean>

More triggers can be added to the 'triggers' property list value.

See the Quartz and Spring documentation for more trigger and job configuration options. E.g. a CRON-style trigger is also available to schedule jobs at particular times of day, days of week, etc. for less regular timed jobs.

The state of the TimeHandler bean can be checked via the page http://.../timer/get_time.html, which is backed by the controller 'getTimeController', an instance of class equip2.webapptutorial.timer.GetTimeController, which gets the tick count and last tick time and places them into the Model before passing on the to view 'timer/get_time' which is mapped to the JSP timer/get_time.jsp.

Changes

2007-02-01