how to create scheduler which triggers a job at every 13 hours in java

how to create scheduler which triggers a job at every 13 hours in java for example if it starts at

  • 12 am then triggers a job at 1 pm
  • 1 pm then triggers a job at 2 am

what about using @Scheduled(cron="0 0 */13 * * * ")

EDIT: you can check here the only difference is that spring has added the first place (first *) for seconds.

EDIT2: you can actually use also fixedRate @Scheduled(fixedRate="13", timeunit=TimeUnit.HOURS)


In Spring Boot, you can just annotate a method with the @Scheduled annotation and the main class with the @EnableScheduling.

Examples:

Main class in Spring application

@EnableScheduling 
@SpringBootApplication
public class ScheduledDemoApplication {

  public static void main(String[] args) {
    SpringApplication.run(ScheduledDemoApplication.class, args);
  }
}

and create a method in a separate class that will be triggered on a schedule:

@Scheduled(some cron exp here)
public void execute() {
  // some logic that will be executed on a schedule
}

This should work fine.

Here is a good article on how to schedule a Spring Boot task.