Spring scheduling using annotations

To achieve the scheduling of asynchronous tasks, Spring provides the @Scheduled and @Async annotations.

  • @Scheduled: Used for task scheduling. The trigger information needs to be provided along with this annotation. You use the properties fixedDelay/fixedRate/cron to provide the triggering information.
    @Scheduled(fixedDelay =30000)
    public void processScheduledJob () {... }
    @Scheduled(fixedRate=30000)
    public void processScheduledJob () {... }
    @Scheduled(cron="0 0 * * * *")
    public void processScheduledJob () {... }

    The time period in milliseconds.

    With fixedDelay the time specified will be measured from the completion of the job, whereas with fixedRate the time specified will be measured from the start time of the job.

    Note that the methods annotated with @Scheduled should be void and cannot have any parameters.

  • @Async: Supports the asynchronous task execution
    @Async
    void execute() {.... }

    The method with @Async annotation can accept parameters

    @Async
    void execute(String s) {.... }

    Or it can have the Future type return values

    @Async
    Future execute(String s) {.... }

To enable the annotation driven scheduling you need to add the “annotation-driven” element from the task namespace.

Example:

Following example shows how to schedule a job in Spring using annotations.

Spring Context Xml

<!--?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:task="http://www.springframework.org/schema/task"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/task
   http://www.springframework.org/schema/task/spring-task-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="javadigest.spring.scheduler" />

<task:annotation-driven executor="executor" scheduler="scheduler" />

<task:executor id="executor" pool-size="5" />

<task:scheduler id="scheduler" pool-size="5" />

</beans>

The Scheduler

package javadigest.spring.scheduler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class MyScheduler {

  @Autowired
  private MyTask task;

  @Scheduled(fixedDelay = 30000)
  public void processScheduledJob() {
    task.execute();
  }

  public MyTask getTask() {
    return task;
  }

  public void setTask(MyTask task) {
    this.task = task;
  }
}

The Asynchronous Task

package javadigest.spring.scheduler;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class MyTask {

  @Async
  public void execute() {
    System.out.println("Executing asynchronous task.");
  }
}

The main() method

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringScheduler {

  public static void main(String[] args) {
    new ClassPathXmlApplicationContext("context.xml");
  }
}

Reference: Spring Manual

Advertisements

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 )

w

Connecting to %s