Saurabh
Saurabh

Reputation: 967

How to scheduling multiple task through cron expression using Springboot?

I want to schedules multiple task using @schedule annotation using cron expression. I have three job which require to execute at fixed time. For example,Job-1 has been schedule every day at 11:PM, Job-2 has been scheduled every day 7AM-9PM in 1 hour interval and Job-3 has been schedule in every 1 hour. All the 3 schedule tasks are part of the same application.

I have tried the same but all three scheduling is not happening. My application is SpringBoot application.I am not new scheduling.Kindly help me out. Below is he my approach

application.properties

cron.expression.job1=0 0 23 * * ?
cron.expression.job2=0 0 7,9 * * ?
cron.expression.job3=0 0/60 * * ?

Java Code

 @EnableScheduling
 @SpringBootApplication
 public class Scheduler{
     // doCallScheduleJob Code
 }

class ScheduleJob{

  @Scheduled(cron="${cron.expression.job1}")
  public sycName1(){
           ///doSomething()     
  }

  @Scheduled(cron="${cron.expression.job2}")
  public sycName2(){
           ///doSomething()     
  }

  @Scheduled(cron="${cron.expression.job3}")
  public sycName3(){
           ///doSomething()     
  }

Upvotes: 0

Views: 5169

Answers (1)

Aaric Chen
Aaric Chen

Reputation: 1188

You should configure your TaskScheduler thread pool size. if you are not configure, the default size is 1 which is mean spring will execute your task one by one. You can configure your TaskScheduler below.

@Configuration
@EnableAsync
@EnableScheduling
public class SpringBootConfiguration {

    @Bean
    public Executor getTaskExecutor() {
        return Executors.newScheduledThreadPool(10);
    }
}

Upvotes: 3

Related Questions