Annotation-based
Spring 3.0 also adds annotation support for both task scheduling and asynchronous method execution. Below section explain in detail some of the well known annotations in this regard with appropriate code snippets for easy understanding.
@Scheduled
The @Scheduled annotation can be added to a method along with trigger metadata. The various attributes which can be used as metadata to this annotation are fixedDelay, fixedRate and cron. Usage of these attributes in action using code snippets as shown in Listing 20-15 below.
Listing 20-15. Usage of various attributes in @Scheduled annotation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class AnnotationTestClass{ @Scheduled(fixedDelay=3000) public void doAtFixedDelay(){ //…. } @Scheduled(fixedRate=3000) public void doAtFixedRate(){ //…. } @Scheduled(cron=”*/****MON-FRI”) public void doAccordingToCronExpression(){ //…. } } |
The methods to be scheduled must have void returns and must not expect any arguments. If the method needs to interact with other objects from the Application Context, then those would typically have been provided through dependency injection. Listing 20-16 below shows some “cron” expression which is handy in the form of code snippets.
Listing 20-16. Usage of cron expression in code snippets
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/* Method would be executed each full minute and every 7 seconds*/ @Scheduled(cron = "*/7 * * * * *") public void executeOne() { // do something } /*Method would be executed every weekday (Monday to Friday) on 9.45 am*/ @Scheduled(cron = "0 45 9 * * MON-FRI") public void executeTwo() { // do something } /*Usage of placeholders for your cron expression which are resolved against the configured property-placeholder.*/ @Scheduled(cron = "${myclass.cron.execute.value}") public void executeThree() { // do something } |
The properties are loaded from the file which contains your cron-expressions like this:
1 |
myclass.cron.execute.value=0 45 9 * * MON-FRI |
@Async
This is an annotation which marks a method as a candidate for asynchronous execution. Can also be used at the type level, in which case all of the type’s methods are considered as asynchronous. Listing 20-17 shows he annotation signature for @Async.
Listing 20-17. @Async annotation signature
1 2 3 4 |
@Target(value={TYPE,METHOD}) @Retention(value=RUNTIME) @Documented public @interface Async |
Any parameter types can be used for the target method signatures. However the return type of the target method can be either “void” or “java.util.concurrent.Future”. Proxy object of this method will return the “Future” handle will be the actual asynchronous “Future” which can be used to track the result of the asynchronous method execution. Also these methods can expect arguments and they will be invoked in the “normal” way by callers at runtime. Listing 20-18 shows using @Async annotation to a method having no return value.
Listing 20-18. Usage of @Async to a method having no return value
1 2 3 4 |
@Async void executeSomething(String string) { // do something asynchronously } |
If you would like your method to return a result, the return value should be of type Future, which is mandatory as shown in Listing 20-19 below.
Listing 20-19. Usage of @Async to a method having return value
1 2 3 4 |
@Async Future<String> executeSomething(String string) { // do something asynchronously } |
Page Visitors: 25269
Tomcy John
Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023