Wednesday 9 January 2013

Spring scheduled annotation

 Spring has nice feature called scheduler. Just simply create class and annotate method with @Scheduled(...) annotation and it's all. You have scheduled task, that wil be executed based on your time expression. Example:

package com.test.jobs;

@Component("testTask")
public class TestTask {

       //Fire at 8:30am every day
        @Scheduled(cron="0 30 8 ? * *")
        public void doSmth(){
          //do it...
        }
}

Of course spring config:


 


 



Possible time format :
http://quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger

2 comments:

  1. hi thanks for the post Sergey. My question is when the schedule will fire, in method call or it will fire automatically

    ReplyDelete
    Replies
    1. Scheduler will fire when spring context initializing, so in most cases, when application starts

      Delete