Wednesday 5 June 2013

Spring 3 - Task Scheduling

In this tutorial we will explore Spring 3's task scheduling support using annotations. We will be using @Scheduled and @Async annotations. Spring also provides scheduling support using the Quartz Scheduler, and via XML configuration . We will build our application on top of a simple Spring MVC 3 application. Although MVC is not required, I would like to show how easy it is to integrate.

@Scheduled Annotation
@Scheduled annotation  add to a method along with trigger metadata.

To enable this annotation we need to add the annotation-driven element:

You also need to add the component-scan element. We didn't enable it here since it's already added in the applicationContext.xml .

 
 
 
 
 
 
 
 
  
 
 
 
 



spring-scheduler

 
 

 
 
 
 
 
 
  
 
  
                
                

Worker

SyncWorker

This worker is synchronous which means if we have to call this worker 10 times, it will block the other workers. They cannot start immediately until the first one is finished. We didn't do anything to make this implementation synchronous. It's the default.

The class that calls this SyncWorker is a scheduler service.
Notice the @Scheduled annotation in the doSchedule() method. This tells Spring to mark this method for task scheduling. Inside the @Scheduled, there's a metadata that describes when the method should be triggered. The following metadata all have the same value (5 seconds) but they are interpreted differently:

fixedDelay=5000
fixedRate=5000
cron="*/5 * * * * ?"

fixedDelay: An interval-based trigger where the interval is measured from the completion time of the previous task. fixedRate: An interval-based trigger where the interval is measured from the start time of the previous task. cron: A cron-based trigger Running the application gives us the following logs:
Notice how the tasks are run sequentially every 5 seconds.

What if we want to run the workers asynchronously, meaning we don't want to wait for worker 1 to finish before we start worker 2, or worker 3, and so forth? There are valid reasons like efficient use of physical resources and time.

To make a worker asychronous, we add the @Async annotation in the method that needs to be asychronous.

The @Async Annotation

    The @Async annotation can be provided on a method so that invocation of that method will occur asynchronously. In other words, the caller will return immediately upon invocation and the actual execution of the method will occur in a task that has been submitted to a Spring TaskExecutor.

    Source: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/scheduling.html

To enable this annotation we use the same annotation-driven element.

Let's examine an actual example. We'll create a new class AsyncWorker that implements the Worker interface earlier.

AsyncWorker

This worker is asychronous. The caller (the scheduler service) will return immediately upon invocation.

The class that calls this AsyncWorker is the same scheduler service we had earlier. We
just need to change the value of @Qualifier Use Of @Scheduled Annotation
//@Scheduled(fixedDelay=5000)
 //@Scheduled(fixedRate=5000)
 @Scheduled(cron="*/5 * * * * ?")
 public void doSchedule() {
  logger.debug("Start schedule");
  
  for (int i = 0; i < 5; i++) {
   logger.debug("Delegate to worker " + i);
   worker.work();
        }
  
  logger.debug("End schedule");
 }

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...