Spring Boot - infinite loop service

前端 未结 3 1099
旧时难觅i
旧时难觅i 2021-02-09 14:27

I want to build a headless application which will query the DB in infinite loop and perform some operations in certain conditions (e.g. fetch records with specific values and wh

3条回答
  •  伪装坚强ぢ
    2021-02-09 14:53

    Do not implement an infinite loop yourself. Let the framework handle it using its task execution capabilities:

    @Service
    public class RecordChecker{
    
        //Executes each 500 ms
        @Scheduled(fixedRate=500)
        public void checkRecords() {
            //Check states and send mails
        }
    }
    

    Don't forget to enable scheduling for your application:

    @SpringBootApplication
    @EnableScheduling
    public class Application {
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(Application.class);
        }
    }
    

    See also:

    • Scheduling Tasks

提交回复
热议问题