Spring MVC Annotation Scheduled Tasks/Cron

Spring MVC makes it easy to execute cron jobs within your application.

Step one:

Add the following to your spring-servlet.xml file:

<!-- Configures the @Scheduled programming model -->
<task:annotation-driven />

Also make sure this is in your beans tag:


Next create a component class with this annotation:

class Scheduler {


Tag methods with one of these annotations to scheduled it’s run time:

@Scheduled(cron="0 0 * * * *") // Every hour on the hour
public void runMe() { }

@Scheduled(fixedDelay = 3600000) // Hour increments from last stop
public void runRunRudolf() { }

The cron is a little different from Linux crons in that it has a seconds field which gives you fine grain control of when these run. Be sure to catch all exceptions in the scheduled tasks or the task could restart and cause unwanted behavior.


One thought on “Spring MVC Annotation Scheduled Tasks/Cron

  1. thanks for the post..
    but i am new to spring mvc… and i cant see spring-servlet.xml…. there is only spring-web.xml …so can i put this tag there only….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s