Spring scheduling using annotations

To achieve the scheduling of asynchronous tasks, Spring provides the @Scheduled and @Async annotations.

  • @Scheduled: Used for task scheduling. The trigger information needs to be provided along with this annotation. You use the properties fixedDelay/fixedRate/cron to provide the triggering information.
    @Scheduled(fixedDelay =30000)
    public void processScheduledJob () {... }
    public void processScheduledJob () {... }
    @Scheduled(cron="0 0 * * * *")
    public void processScheduledJob () {... }

    The time period in milliseconds.

    With fixedDelay the time specified will be measured from the completion of the job, whereas with fixedRate the time specified will be measured from the start time of the job.

    Note that the methods annotated with @Scheduled should be void and cannot have any parameters.

  • @Async: Supports the asynchronous task execution
    void execute() {.... }

    The method with @Async annotation can accept parameters

    void execute(String s) {.... }

    Or it can have the Future type return values

    Future execute(String s) {.... }

To enable the annotation driven scheduling you need to add the “annotation-driven” element from the task namespace.


Following example shows how to schedule a job in Spring using annotations.

Spring Context Xml

<!--?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans

<context:component-scan base-package="javadigest.spring.scheduler" />

<task:annotation-driven executor="executor" scheduler="scheduler" />

<task:executor id="executor" pool-size="5" />

<task:scheduler id="scheduler" pool-size="5" />


The Scheduler

package javadigest.spring.scheduler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

public class MyScheduler {

  private MyTask task;

  @Scheduled(fixedDelay = 30000)
  public void processScheduledJob() {

  public MyTask getTask() {
    return task;

  public void setTask(MyTask task) {
    this.task = task;

The Asynchronous Task

package javadigest.spring.scheduler;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

public class MyTask {

  public void execute() {
    System.out.println("Executing asynchronous task.");

The main() method

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringScheduler {

  public static void main(String[] args) {
    new ClassPathXmlApplicationContext("context.xml");

Reference: Spring Manual


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s