Pablo Karlsson Pablo Karlsson - 8 months ago 67
Java Question

How to run scheduled tasks in Spring?

Im looking for examples on how to run scheduled tasks in spring. Im currently using xml configuration and spring 3.2. I have found their documentation page for scheduled tasks but would like a more simple example to get started. Here is a link to the docs

Where do I configure the scheduling beans? I want them to run daily and only one task at a time.


Add following to your spring xml :


Create a class :

import javax.inject.Named;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;

public class FunBean {

        private static final Logger slf4jLogger = LoggerFactory.getLogger(FunBean.class);

         @Scheduled(fixedDelay = 5000)
        public void doSomething() {
  "I am working");


This should get you going.You can configure the fun bean definition in your xml file just like any spring bean.