How can I configure Spring Scheduled Task to run in a time range with specific delay?

后端 未结 3 461
无人共我
无人共我 2021-01-14 18:30

I need set up spring scheduled time to be executed every 15 minutes from 5 p.m till 8 a.m, how to specify such expression? And also I\'d like task be executed on we

3条回答
  •  猫巷女王i
    2021-01-14 18:50

    Maven Dependency

    Part of Spring Context

     
                org.springframework
                spring-context
                ${springframework.version}
      
    

    Configuration to enable Scheduling

    Reference from Documentation

    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.annotation.EnableScheduling;
    
    @Configuration
    @EnableScheduling
    public class MyAppConfig {
    //..
    }
    

    Method to fire on the Cron you specified

    Reference from Documentation

    // 0/15 -> Every 15 minutes on the clock
    // 17-20 -> Between 5pm and 8pm on the JVM timezone
    // if you want timezone specific there is a 'zone' parameter: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/annotation/Scheduled.html#zone--
    @Scheduled(cron="0 0/15 17-20 * * ?")
    public void doSomething() {
        // ..
    }
    

    Documentation

    Spring Documentation on Scheduling

    Spring Boot

    Spring Boot example of setup to run for the above cron

提交回复
热议问题