Pablo Karlsson Pablo Karlsson - 1 year ago 108
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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download