According to Spring\'s documentation the way to use the TaskExecutor is as follows:
import org.springframework.core.task.TaskExecutor;
public class TaskExecutor
You can also use the @Async annotation.
public class TaskExecutorExample {
@Autowired
private MessagePrinterTask task;
public void printMessages() {
for(int i = 0; i < 25; i++) {
task.printMessage();
}
}
}
@Component
public class MessagePrinterTask implements Runnable {
@Autowired
private String message;
@Async
public void printMessage() {
System.out.println(message);
}
}
Any call to printMessage() will be executed asynchronously using a default executor, which you can configure using the following in your Spring xml config.