How to Implement a Task Scheduler/ Job Scheduler in Spring Framework using Annotations?
I had a chance to analyze how the task scheduler works in Spring framework today!! The recent addition of the @Scheduled and @Async into Spring framework 3.0 forced me to tgive them a try! Emphatically, spring framework is remarkable. With a few tweaks in code, everything gets in place as expected. In this article, let me give you a very brief overview on how to implement a scheduled task in spring using @Schedule annotation! This would be a nice tutorial for a newbie and an intermediate Spring programmer! If you intend to have a scheduled job run periodically and if you are using Spring, please read further and get to know the nuances involved.Let us dive into action right away!.
- First and foremost, let us decide on incorporating the task:annotation-driven in the applicationContext.xml file. This is the configuration file that would be loaded by the ContextLoaderListener within your spring application. I have decided to have the task:annotation-driven tag within the application since this is where I would be including the beans that would form a part of my service layer and I am going to include the TaskScheduler class a service in my application.
Next, go ahead and add the xmlns details to your configuration. The following code needs to be added to the applicationContext.xml file in your application.1xmlns:task=http://www.springframework.org/schema/task
- Further, you have to add the schema location details for the task-annotation driven tag in your applicationContextxml. This is as follows:
- Note: If you do not add the configurations given in point # 1 and #2 in your applicationContext.xml where we would be defining the services used in our application (I am conidering that I would have a class containing the scheduled jobs as a service and this would be a part of my services package), you are likely to get the "The prefix 'task' for element '
' is not bound" error. - Next, you have to make your Spring container understand that you would be having some scheduled jobs within your application and those tasks are driven by annotations. You can do so by adding the following line to the applicationContext.xml file.12
<
task:annotation-driven
>
- Now, it is important that the Spring framework should be aware of the class details where you would be using your @Scheduled annotation. For this, let us consider we have a package or.webapp.services.tasks. In this package, let us consider that we have a class MyScheduler.java. This is the class that would contain all the methods which would be invoked as scheduled tasks/jobs in your application. It is important that you add this package in the component-scan tag within your applicationContext.xml as follows:12
- Next, we need to code the details of the scheduled job within the class MyScheduler. As a sample, we will have one method testTask. This task would be annotated as @Scheduled with the required attributes. This would in turn make Spring container understand that the method underneath this annotation would be run as a job that is scheduled by the Spring Framework. This can be done as follows:1234567891011121314
package org.test.common.utils.Tasks;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class MyScheduler {
@Scheduled(fixedRate = 5000)
public void process() {
System.out.println("Invoking testTask at " + new Date());
}
}
- Attributes for @Scheduled Annotation:If you want a job or a task to be run every 5 secondsm then you need to modify the @Scheduled annotation sued above as: @Scheduled(fixedRate = 5000). If you want a time gap of 5 seconds between the end of previous execution and the start of the succeeding execution then you need to modify the @Scheduled annotation as @Scheduled(fixedDelay= 5000). You can also trigger a cron job and define your cron parameters say try running a job at 6.30pm everyday by using @Scheduled(cron="30 18 * * * ")
- And that is it! You deploy the application. You would see the 'Invoking testTask at " + current date' getting printed every 5 seconds in your console!
- Further, Even if you want to update the db, you can go ahead and get the current session and process this method within a transaction to make db updates on a periodical basis.
- Note: Remember that the methods annotated with @Scheduled should not have parameters passed to them. They should not return any values too.If ever you want the external objects to be used within your @Scheduled methods, you should inject them into the MyScheduler class using autowiring rather than passing them as parameters to the @Scheduled methods
8 comments:
thank you for this great tutorial, you're a life saver
Can you comment out the part 5 text? Thank you!
My @Schedule annotation also does not work
www0626
polo ralph lauren
christian louboutin outlet
magic jerseys
true religion jeans
jerseys from china
canada goose jackets
houston texans jerseys
alexander mcqueen shoes
mcm outlet
ugg boots
nice and easy, thanks. Just a comment this line
xmlns:task=http://www.springframework.org/schema/task
the url part needs to be in quato
xmlns:task="http://www.springframework.org/schema/task"
kate spade handbags
michael kors outlet online
nike air max 270
nike cortez men
air max 2019
christian louboutin outlet
adidas zx flux
ralph lauren uk
nfl jerseys
cheap nfl jerseys
yeezy shoes
supreme shirt
stone island
jordans
kobe byrant shoes
kd shoes
yeezy shoes
off white shoes
pandora
pandora bracelet
golden goose sneaker
curry 6
pg 1
spongebob kyrie 5
fear of god
off white
kyrie irving shoes
bape outlet
palm angels
supreme official
Post a Comment