Annotation Support for Scheduling and Asynchronous Execution

In the previous post Task Execution and Scheduling in Spring, I explained basic interfaces for task execution and scheduling. In this post I shall demonstrate an example for the same using annotations @Scheduled and @Async. Let us have working Eclipse IDE in place and follow the steps to create a scheduled Spring application:

  1. Create a project: Create a project with a name SpringScheduler and create a package com.javabeat under the src directory in the created project.
  2. Add Libraries: Add required Spring libraries using Add External JARs option as explained in the article Customizing callback methods. Along with these libraries also add aopalliance-1.0.jar to the build path.
  3. Create source files: Create Java classes SchedulerSample,TaskSample and MainApp under the com.javabeat package.
  4. Create configuration file: Create XML based configuration file Beans.xml under src directory.

follow us on @twitter and @facebook

The content of file are:

package com.javabeat;

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

public class SchedulerSample {

	  private TaskSample task;

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

	  public TaskSample getTask() {
	    return task;

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

Here you can note that we have scheduled the task for a time of 3000 milliseconds.

The content of file are:

package com.javabeat;

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

public class TaskSample {
	public void execute() {
		.println("Executing asynchronous task for every fixedDelay of 3000millisecond.");

The execute() method will be called every 3000 milliseconds.

The content of Beans.xml file are:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

<context:component-scan base-package="com.javabeat" />

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

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

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


Here we need to note few things:

  • To enable the annotation driven scheduling we have added the annotation-driven element from the task namespace.
  • Schema,, are added to the above file.
  • task:executor – creates an instance of ThreadPoolTaskExecutor.
  • task:scheduler – creates an instance of ThreadPoolTaskScheduler.

To execute the application the is as follows:

package com.javabeat;


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

Execute the code

The final step is run the application. Once all the above code is ready execute and the below output appears on the console:

Executing asynchronous task for every fixedDelay of 3000millisecond.
Executing asynchronous task for every fixedDelay of 3000millisecond.

As you can see the message is printed at a delay of 3000millisecond. You need to stop the application to stop the printing of message.


In this section I demonstrated an example of scheduling the Spring application using the annotation.If you are interested in receiving the future articles, please subscribe here. follow us on @twitter and @facebook

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!