❮ zur Übersicht

Scheduling and asynchronous execution with Spring

  • Author: Aljona Buchloh
  • Datum: 13.06.2012

You want to execute cron jobs or call your methods asynchronously? Thanks to Spring’s annotation support for scheduling and asynchronous execution you can achieve this in a few minutes.

Some xml magic

At first define your task executor and scheduler. The following lines will create an instance of ThreadPoolTaskExecutor and an instance of ThreadPoolTaskScheduler with the given pool sizes. The task element annotation-driven allows you to use Spring’s annotations for scheduling and asynchronous execution within the beans defined in your application context.

<bean id="myClass" class="my.project.path.myClass" />
<task:annotation-driven executor="myExecutor" scheduler="myScheduler" />
<task:executor id="myExecutor" pool-size="5" />
<task:scheduler id="myScheduler" pool-size="10" />

The @Scheduled annotation

With the @Scheduled annotation you can execute your method as a cron job. Using this annotation requires that the method to be scheduled must be of type void and must not expect any arguments. The following examples show you how to use the @Scheduled annotation.

If you want periodic scheduling you can use the property fixedRate. In this example the method would be executed every 42 seconds.

@Scheduled(fixedRate = 42000)
public void execute() {
    // do something
}

If you prefer cron expressions you can use them either. The following example is analogue to the example above only using cron expressions. The annotated method would be executed each full minute and every 7 seconds.

@Scheduled(cron = "*/7 * * * * *")
public void execute() {
    // do something
}

Without question you have much more possibilities with cron expressions than with periodic scheduling. In this example your method would be executed every weekday (Monday to Friday) on 9.45 am.

@Scheduled(cron = "0 45 9 * * MON-FRI")
public void execute() {
    // do something
}

A pretty cool feature is that you even can use placeholders for your cron expression which are resolved against the configured property-placeholder.

@Scheduled(cron = "${myclass.cron.execute.sth}")
public void execute() {
    // do something
}

Define where the properties are loaded from within your application context:

<context:property-placeholder location="classpath:application.properties"/>

Then the properties are loaded from the file which contains your cron-expressions like this:

myclass.cron.execute.sth=0 45 9 * * MON-FRI

The @Async annotation

The @Async annotation allows you to invoke your method asynchronously. The execution of the method will occur in a task that has been submitted to the TaskExecutor defined in your application context. Contrary to the methods annotated with the @Scheduled annotations the methods you annotate with @Async may be of other type than void and can expect arguments.

This is a simple example of a @Async annotated method without a return value.

@Async
void execute(String string) {
    // do something asynchronously
}

Like mentioned above your @Async annotated method may have a return value. However this return value must be of type Future. This means that first the other tasks are performed and then is called get() on that Future.

@Async
Future<String> execute(String string) {
    // do something asynchronously
}

Further information

Have a look at the Spring Framework Reference Documentation

Tags: @Async @Scheduled asynchronous scheduling spring Spring annotations