Monday, May 25, 2015

Spring Scheduler hello world example - run task in the future

Spring framework now support scheduler, so for most usage there is no need to involve other library, such as Quartz to do this.  The usage is very simple: add annotation @Scheduled(...) to any bean's method that need to be run in the future. The Spring context will create a thread to run it at the right time you specified in the future.

0. What you need

  • JDK 1.7
  • Maven 3.2.1
  • Spring 4.1.0.RELEASE

1. Configure the maven pom.xml

<project xmlns=""



<!-- Spring Context aritifact-->


<!-- Using JDK 1.7 for compiling -->

2. Define the Java Class

In the example there are 2 classes. The first one is a simple spring bean, but with annotation @Scheduled for method.

package com.shengwang.demo;

import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
public class Task {
* The method will run on 13:40:00 everyday.
* cron format: sec min hour day mon week
@Scheduled(cron="0 40 13 * * *")
public void runTask() {
System.out.println("Runing at " + new Date());

The second class is the main class.

package com.shengwang.demo;

import org.springframework.context.ApplicationContext;

public class DemoMain {

public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("app-context.xml");
The only line in the main is used to create the spring application context. If you run the main function, the program will not never finish since the spring context is not closed. so it will wait for the scheduled task to run in the future. The app-context.xml is the spring configuration file.


3. Spring configuration

The spring configuration file is named "app-context.xml" here. It stays in the main resources path /src/main/resources.

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

<!-- enable scheduling annotations -->
<task:annotation-driven scheduler="scheduler"/>
<task:scheduler id="scheduler" pool-size="15"/>

<!-- register spring beans -->
<context:component-scan base-package="com.shengwang.demo" />


There are 2 lines start with <task:... /> used to configure spring scheduling. One thing need to mention is that (from spring official document):  If you do not provide a pool-size attribute, the default thread pool will only have a single thread. So in real practical, the pool-size is should always be set  to a number bigger than one to prevent any long task delaying all other scheduled tasks.


  1. Let assume i schedule a job for a minute , My first process started but not completed in a minute. at this moment second schedular task starting no matter if first scheduled task completed or not. how can we resolve it?

    1. @Scheduled can have fixDelay and fixRate properties. fixRate count from job start, fixDelay count from job finish. So if you don't want 2 same job running at the same time, just use fixDelay.

  2. This comment has been removed by the author.

  3. This comment has been removed by the author.

  4. How can I do if there is a job doesn't executed for some reason ?

  5. Hi is possible run a @Scheduled method when running a web application?
    I want to create a web app build with Angular and Spring Data Rest - Mongodb by wich I can create some job that I want a scheduler inside the application every night will perform. How I can do that? I try to put @EnableScheduling in the main of my Spring Boot application but nothing happen


Powered by Blogger.

About The Author

My Photo

Has been a senior software developer, project manager for 10+ years. Dedicate himself to Alcatel-Lucent and China Telecom for delivering software solutions.


Unordered List