Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

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
  • Is it possible to use a similar construct where you can use a variable for (or instead of) 'fixedRate'? For example I want to read the value vor 'fixedRate' from a configuration file. (See stackoverflow.com/questions/69281633/…) – waldrabe Oct 15, 2021 at 10:41 You mean that you use Spring Integration to create channel to poll database and then when something is found your consumer is executing a logic on found rows ? – Pawel Urban Apr 11, 2016 at 7:37 Thanks for your hint. I finally used Spring Integration + Spring Boot. I used JDBC Inbound Channel Adapter to pool database and written my own ServiceActivator to react on found rows. Work like a charm with a little amount of code. – Pawel Urban Apr 12, 2016 at 13:09

    There are several options. My approach is to start a loop on an ApplicationReadyEvent, and abstract away the loop logic into an injectable service. In my case it was a game loop, but this pattern should work for you as well.

    package com.ryanp102694.gameserver;
    import com.ryanp102694.gameserver.service.GameProcessor;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.context.event.ApplicationReadyEvent;
    import org.springframework.context.ApplicationListener;
    import org.springframework.stereotype.Component;
    @Component
    public class GameLauncher implements ApplicationListener<ApplicationReadyEvent> {
        private static Logger logger = LoggerFactory.getLogger(GameLauncher.class);
        private GameProcessor gameProcessor;
        @Autowired
        public GameLauncher(GameProcessor gameProcessor){
            this.gameProcessor = gameProcessor;
        @Override
        public void onApplicationEvent(ApplicationReadyEvent event) {
            logger.info("Starting game process.");
            gameProcessor.start();
            while(gameProcessor.isRunning()){
                logger.debug("Collecting user input.");
                gameProcessor.collectInput();
                logger.debug("Calculating next game state.");
                gameProcessor.nextGameState();
                logger.debug("Updating clients.");
                gameProcessor.updateClients();
            logger.info("Stopping game process.");
            gameProcessor.stop();
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.