Square CSS

Monday, May 25, 2020

@EnableScheduling : Create Scheduler In Spring

Introduction


We can create scheduler in spring using @EnableScheduling and @Scheduler in Spring based application.


Refer Video Tutorial For Detail :





@EnableScheduling


@EnableScheduling is annotated on class level to configure class for scheduling jobs in Spring.
This annotation is used along with @configuration for configuring scheduler inside the class as method. 


  @Configuration
  @EnableScheduling //to enable scheduling in class
public class MyScheduler{
        //Custom Code
  



@Scheduler


@Scheduler is used on the method level to run method at scheduled time. The annotation takes parameter to configure time for scheduling. This can be done using cron expression or using delay functions provided in spring.



  //annotate with scheduler
  @Scheduled(cron="*/2 * * * * *")//for every 2 seconds  
public void jobForScheduler() {
        //Custom Code to execute every 2 seconds
  


Conclusion


To enable scheduling in Spring we need to annotate class with @EnableScheduling and @configuration. The method to be considered for job to be annotated with @Scheduled

No comments:

Post a Comment

Some Algorithms

Algorithm: Tower of Hanoi

Tower of Hanoi consists of three towers called as pegs with n number of rings. Rings are of different size.  Conditions to be fulfill...

Popular Posts