Spring 3.0 also adds annotation support for both task scheduling and asynchronous method execution.
The @Scheduled Annotation
The @Scheduled annotation can be added to a method along with trigger metadata. For example, the following method would be invoked every 5 seconds with a fixed delay, meaning that the period will be measured from the completion time of each preceding invocation.@Scheduled(fixedDelay=5000)
If a fixed rate execution is desired, simply change the property name specified within the annotation. The following would be executed every 5 seconds measured between the successive start times of each invocation.@Scheduled(fixedRate=5000)
If simple periodic scheduling is not expressive enough, then a cron expression may be provided. For example, the following will only execute on weekdays.@Scheduled(cron="*/5 * * * * MON-FRI")
Notice that 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.
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. In the simplest case, the annotation may be applied to a void-returning method.@Async
Unlike the methods annotated with the @Scheduled annotation, these methods can expect arguments, because they will be invoked in the "normal" way by callers at runtime rather than from a scheduled task being managed by the container. For example, the following is a legitimate application of the @Async annotation.@Async
Even methods that return a value can be invoked asynchronously. However, such methods are required to have a Future typed return value. This still provides the benefit of asynchronous execution so that the caller can perform other tasks prior to calling 'get()' on that Future.@Async
The <annotation-driven> Element
To enable both @Scheduled and @Async annotations, simply include the 'annotation-driven' element from the task namespace in your configuration.<task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>
Notice that an executor reference is provided for handling those tasks that correspond to methods with the @Async annotation, and the scheduler reference is provided for managing those methods annotated with @Scheduled.
Java-Springs Related Interview Questions
|Adv Java Interview Questions||J2EE Interview Questions|
|Core Java Interview Questions||JSP Interview Questions|
|Java-Springs Interview Questions||Java Servlets Interview Questions|
|JMS(Java Message Service) Interview Questions||Hibernate Interview Questions|
|Java applet Interview Questions||Java Interview Questions|
|Framework7 Interview Questions||Java collections framework Interview Questions|
|Java 8 Interview Questions||Java Programmer Interview Questions|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.