Scheduled Jobs in Spring 3

Spring has made scheduling a Java method as easy as creating a UNIX cron job. Here’s a quick introduction.

Those of us who got our start writing web applications in the early days were used to having cron available to us to run PERL scripts when we needed a periodic task executed. Over time, the evolution of web applications into the application server space has made it a little less convenient to run your business logic at an appropriate interval.

Within the Java world, Quartz is a commonly used technology introduced to solve the problem.

But the people behind Spring have, as with most things, boiled the process down to something very simple.

Spring 2 had a useful task executor and task scheduler, but Spring 3 has evolved the process into easy annotation-based awesomeness. Java methods that you’ve already written to generate reports, do maintenance, or check status can all be run with common cron interval definitions.

Here’s a quick set of steps you can use to set up this kind of scheduled task within your Spring-based Java application:

Step 1: Define a task executor and task scheduler in an application context file:

<?xml version="1.0" encoding="UTF-8"?>
  <beans xmlns="" xmlns:xsi="" xmlns:task="" xsi:schemaLocation="">
    <task:scheduler id="taskScheduler"/>
    <task:executor id="taskExecutor" pool-size="1"/>
    <task:annotation-driven executor="taskExecutor" scheduler="taskScheduler"/>

The “task” namespace is used here to declare the scheduler and executor. The third line of the XML asks Spring to scan classes for annotations related to scheduled tasks. If you have multiple tasks to run at the same time, you can get them to run in parallel by increasing the pool size.

Step 2: Annotate the method you want to run on a schedule

  public class ReportingServiceImpl implements ReportingService {
    private static final Logger logger = Logger.getLogger(ReportingServiceImpl.class);
      private MailService ms;
    // Every night at 1 AM
    @Scheduled(cron = "* * 1 * * ?")
    public void generateAndEmailReport() {"Starting report at " + new Date(System.currentTimeMillis()));
    Report r = generateReport();
    ms.sendReport(r);"Report sent at " + new Date(System.currentTimeMillis()));


There are some other useful variants of the annotations. Instead of supplying a ‘cron’ pattern, you could just define an interval.


This annotation would have a method run every 60 seconds (FixedRate defines a time in milliseconds). You can also use the


annotation to mark a scheduled method as asynchronous.

Two things to be aware of:

At the time of writing, there’s no way to control if or how often a method is retried if an exception occurs during scheduled method execution. So, if an exception happens during execution of a scheduled method, Spring will re-try the method automatically. If the exception continues to happen, this can result in large logs and possibly a lot of resource consumption. I’ve submitted an enhancement request to Spring to make this behavior configurable.

Spring 3.0.0 Release had a bug where web apps with a task scheduler would end up executing scheduled methods twice. This has been resolved in Spring 3.0.1.

You can read more about scheduling tasks with Spring here:

Subscribe to the Vodori blog

Back to Blog