Monday, June 14, 2010

Spring 3: Scheduling... Take 2

In the previous blog entry I used Spring annotation scheduling to fire an event, but there is also another way through the application context xml.

Annotation:
In the application context.




Then within the code annotate the method with @Scheduled(fixedDelay = 10000)

XML:
Now if you annotate your class with @Service or @Component (with component-scan), you can use the scheduled-tasks tag, and schedule a method on a service and specify a cron expression, which does give you all the control you'll need.




    



Since I personally can never remember the exact format for cron I am adding the next little bit for easy reference.

* * * * * *
| | | | | |
| | | | | +-- Year (range: 1900-3000)
| | | | +---- Day of the Week (range: 1-7, 1 standing for Monday)
| | | +------ Month of the Year (range: 1-12)
| | +-------- Day of the Month (range: 1-31)
| +---------- Hour (range: 0-23)
+------------ Minute (range: 0-59)


0 23 * * 1-5 * At 11:00PM on weekdays Mon-Fri
0 0 1 * * * At midnight, on the first day of each month
* * * * * ? Every Second
0 * * * * ? Every Minute

No comments:

Post a Comment

Popular Posts

Followers