简体   繁体   English

Spring Boot - 无限循环服务

[英]Spring Boot - infinite loop service

I want to build a headless application which will query the DB in infinite loop and perform some operations in certain conditions (eg 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.我想使用 Spring Boot 作为基础(特别是因为 Actuator 允许公开健康检查),但现在我使用 Spring Boot 来构建 REST web 服务。

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 ?有没有人尝试过基于 Spring Boot 构建它,并且可以与我分享他在这种情况下的架构?

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:另见:

我正在使用的是消息代理和消费者放在 Spring Boot 应用程序中来完成这项工作。

There are several options.有几种选择。 My approach is to start a loop on an ApplicationReadyEvent, and abstract away the loop logic into an injectable service.我的方法是在 ApplicationReadyEvent 上启动一个循环,并将循环逻辑抽象为一个可注入的服务。 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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM