Using Spring for Scheduling Tasks


One of the common requirements we face as developers is to do things in a scheduled manner. For example, check database for change every 10 mins and do something if a change has occurred or send emails at a specific time. The Spring documentation is a bit thin on this one although Spring does a fantastic job of scheduling things. So in this blog I am going explain how I used Spring for scheduling things. Please note I am using Spring 3.

First of all here is my Spring application context file to enable annotations etc.

<beans xmlns=""
    xmlns:xsi="" xmlns:p=""
    xmlns:context="" xmlns:tx=""

    <context:component-scan base-package="in.rockyj" />
    <context:property-placeholder location="classpath:/"/>
    <bean id="threadPoolTaskScheduler" class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
        <property name="poolSize" value="3" />


Here I am doing three things basically -

  1. Setting up a component scan for handling Spring annotations
  2. Informing Spring about a properties file
  3. Setting up a bean - ThreadPoolTaskScheduler which is a Spring provided class for scheduling stuff.

The ThreadPoolTaskScheduler a Spring provided class that handles the scheduling logic for us. All it needs is a thread pool size which defaults to 1. This class provides various methods to schedule tasks, for example scheduleWithFixedDelay method takes two parameters - a runnable task and the delay after which the task should be run. For example see code below -

__in my properties file there is only one entry - __


__My main Scheduler class - __

public class Scheduler {

    ThreadPoolTaskScheduler threadPoolTaskScheduler;
    Worker worker;
    Long servicePollTime;
     * Schedule the run of the worker
    public void schedule() {
        threadPoolTaskScheduler.scheduleWithFixedDelay(worker, servicePollTime);

__My Worker class - __

public class Worker implements Runnable {
    public void run() {
        //Main task goes here - For example just a console output
        System.out.println(Thread.currentThread().getName() + " working ... Time - " + new Date());

__And finally my main class - __

public class App {

     * Main method to run the application
    public static void main(String[] args) {
        ApplicationContext context = loadSpringContext();
        Scheduler scheduler = context.getBean(Scheduler.class);

    private static ApplicationContext loadSpringContext() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml");
        return context;

__Which gives me the output - __

threadPoolTaskScheduler-1 working ... Time - Wed Jun 29 13:02:17 IST 2011
threadPoolTaskScheduler-1 working ... Time - Wed Jun 29 13:02:27 IST 2011
threadPoolTaskScheduler-1 working ... Time - Wed Jun 29 13:02:37 IST 2011

This is pretty self explanatory stuff. The main advantage here is that I have not cluttered my code with any Scheduling logic, the classes are loosely coupled and everything is 100% testable. :)