问题
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 when found launch e-mail sending procedure for each message).
I want to use Spring Boot as a base (especially because of Actuator to allow expose health-checks), but for now I used Spring Boot for building REST web-services.
Is there any best practices or patterns to follow when building infinite loop applications ? Does anyone tried to build it based on Spring Boot and can share with me his architecture for this case ?
Best regards.
回答1:
What I'm using is a message broker and a consumer put at the spring boot application to do the job.
回答2:
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
来源:https://stackoverflow.com/questions/36541857/spring-boot-infinite-loop-service