Why do we need scheduling?
Scheduling is needed if you want to automate the repetition of a task at specific intervals or particular date. You could of course manually watch the time and execute your task, albeit an inefficient task. Who wants to watch the computer every 3 seconds just to hit the Enter key? No one.
The work
We want to run the following sample class at specific intervals:
The task that we're interested is inside the work() method. This example is based on Mark Fisher's example at Task Scheduling Simplifications in Spring 3.0. This method retrieves the thread name, prints the starting and beginning of the method, simulates work by putting the thread in sleep for 10 seconds.
To schedule this using Spring's scheduling support via XML, we'll use the tag scheduled-tasks element.
The 'scheduled-tasks' element
The most powerful feature of Spring's task namespace is the support for configuring tasks to be scheduled within a Spring Application Context. This follows an approach similar to other "method-invokers" in Spring, such as that provided by the JMS namespace for configuring Message-driven POJOs. Basically a "ref" attribute can point to any Spring-managed object, and the "method" attribute provides the name of a method to be invoked on that object.We've prepared a separate XML file to contain all configurations relating to scheduling.
Source: Spring Framework 3 Reference, Chapter 25. Task Execution and Scheduling
spring-scheduler.xml
This config is actually shorter if we remove the comments.
Notice we declared three references to our tasks inside the scheduled-tasks element:
Each has task has their own triggers (the schedule when they will be executed). These tasks are managed by the taskScheduler
The id is used as the default thread name prefix. You can also configure the pool size.
We've declared three beans as tasks:
These are simple POJOs with no dependency over Spring's scheduling. This mean we can easily add scheduling to existing beans.
Here are the classes for these beans:
FixedDelayWorker
FixedRateWorker
FixedDelayWorker
Worker
Running the application, the logs show the following output:
Notice how the FixedRateWorker is executed multiple times. This doesn't look right. We've scheduled it using the fixed-rate attribute (an interval-based trigger where the interval is measured from the start time of the previous task). This means it will run every 5 seconds from the start time of the previous task.
Starting from 06:00:31 up to 06:01:01, there are five intervals of 5 seconds each.
06:00:36 06:00:41 06:00:46 06:00:51 06:00:56This means our FixedRateWorker has been queued 5 times. If we look at the logs from 06:01:01 to 06:01:41, FixedRateWorker has been executed 5 times. The point to take here is be careful when combining multiple schedules. We combined them here for the sake of completeness. Also, we've run the following example within a Spring MVC application just by adding an extra configuration spring-scheduler.xml
To see the output, please check the logs :) Feel free to modify the MVC app to fit your needs. You might wanna try integrating a web service with scheduling and show the results via MVC. The welcome page is accesible at
http://localhost:8080/spring-mvc-task-scheduling-xml/krams/main/welcomeThe best way to learn further is to try the actual application.
Download the project
You can access the project site at Google's Project Hosting at http://code.google.com/p/spring-mvc-scheduling/
You can download the project as a Maven build. Look for the spring-mvc-task-scheduling-xml.zip in the Download sections.
You can run the project directly using an embedded server via Maven.
For Tomcat: mvn tomcat:run
For Jetty: mvn jetty:run
If you need to know more about Task Scheduling in Spring 3.0, please visit the following links:
- Task Scheduling Simplifications in Spring 3.0
- Spring Framework 3 Reference, Chapter 25. Task Execution and Scheduling
Share the joy:
|
Subscribe by reader Subscribe by email Share
hi,
ReplyDeleteUseful post without doubt as usual.
But I also want to know about how can we set these delays and/or rates, let's say for example, to a value from database.
@Ravi, I'm not sure if this is possible yet via XML. You can try scheduling via annotations and inject the value from the database.
ReplyDeleteGreat post by the way.
ReplyDeleteFYI for anyone reading this.
This post on the springforums has a nice method to set the schedule in properties files and inject the values into the @Scheduled annotations
http://forum.springsource.org/showthread.php?83053-Feature-Scheduled-with-Value-cron-expression
Good link !!
Deletegreat tutorial Mark. i tried implementing this in my project and everything works fine except, my cronWorker is queued multiple times. sometime once, sometime twice, and sometime five times. What do i need to do so my method will only get called once.
ReplyDeleteThanks
nice and informative post.i really like this post.
ReplyDeleteGood Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeletecore java training in Electronic City
Hibernate Training in electronic city
spring training in electronic city
java j2ee training in electronic city
Happy to found this blog. Good Post!. It was so good to read and useful to improve my knowledge as updated one, keep blogging. Hibernate Training in Electronic City
ReplyDeleteJava Training in Electronic City