Mithrand1r
Mithrand1r

Reputation: 2343

Spring boot Scheduler is being executed randomly not according to fixedDelay

I have recently switched from @Scheduled(cron="") to @Scheduled(fixedDelay=) because after few hours of application working scheduled tasks just stoped to being executed until application restart. Now I have something like this:

@SpringBootApplication(scanBasePackages = {"pl.corpnet.bst"})
@ComponentScan
@EnableScheduling
@EnableJpaAuditing
public class AiostApplication {

    public static void main(String[] args) throws JSONException {

        SpringApplication.run(AiostApplication.class, args);

    }


}

and scheduled method which I want to execute every 5min (logic just send GET request to OpenStack API and load results to DB)

  @Scheduled(fixedDelay = 300000)
  public void reportCurrentTime() throws JSONException, ParseException, UnknownHostException {
    Set<Thread> ts = Thread.getAllStackTraces().keySet();
    log.info("Running threads {}",ts.size());
    List<IaasApi> apis = iaasApiRepository.findAll();
        ... logic here
    }

My guess it was somehow made by Thread lock and by investigating more and putting

        Set<Thread> ts = Thread.getAllStackTraces().keySet();
        for (Thread t : ts) {
            log.info(t.getName()+ " "+ t.getState());
        } 

I can see multiple (overall 27 threads just after starting the app) https-jsse-nio-8443-exec-8 in WAITING state

Can anyone give me a hint if it is a proper way of working or I misconfigured something?

Upvotes: 1

Views: 611

Answers (1)

gbandres
gbandres

Reputation: 911

With fixedDelay, Sprint starts counting the delay after the method stops executing. If you want it to run exactly every 5 minutes you should use fixedRate, which starts counting right after the method starts running so it will always happen exactly every 5 minutes. If the last execution hasn't finished yet, Spring will create another Thread to run the task again.

Upvotes: 2

Related Questions