简体   繁体   English

Spring Boot进度报告

[英]Spring Boot Progress report

I would like to be able to report a certain method's progress in Spring Boot. 我希望能够在Spring Boot中报告某个方法的进度。 I used a seperate class which I store the current status in and return as the current view: It looks like this: 我使用了一个单独的类,我将当前状态存储在当前视图中并返回:它看起来像这样:

public class SearchTableReloadState {

    //STATIC STORAGE

    public static long TABLE_ROW_COUNT = 0;
    public static long CURRENT_OFFSET = 0;
    public static long CURRENT_LIMIT = 0;
    public static long DEFAULT_LIMIT = 20000;

    public static void reset() {
        TABLE_ROW_COUNT = 0;
        CURRENT_OFFSET = 0;
        CURRENT_LIMIT = DEFAULT_LIMIT;
    }

    public static void setDefaultLimit(long defaultLimit) {
        DEFAULT_LIMIT = defaultLimit;
    }

    // VIEWMODEL
    public long tableRowCount = 0;
    public long currentOffset = 0;
    public long currentLimit = 0;

    public static SearchTableReloadState getState() {
        SearchTableReloadState reloadState = new SearchTableReloadState();
        reloadState.tableRowCount = TABLE_ROW_COUNT;
        reloadState.currentOffset = CURRENT_OFFSET;
        reloadState.currentLimit = CURRENT_LIMIT;
        return reloadState;
    }
}

And the methods: 方法:

@RequestMapping(value = {"/manage/searchtable/reload/state"}, method = RequestMethod.GET)
public @ResponseBody SearchTableReloadState searchTableReloadState() {
    return SearchTableReloadState.getState();
} 

@ResponseStatus(HttpStatus.OK)
    @RequestMapping(value = {"/manage/searchtable/reload"}, method = RequestMethod.GET)
    public void searchTableReload() throws ResourceAlreadyExistsException, ParameterMissingIdException {
     SearchTableReloadState.reset();
     SearchTableReloadState.TABLE_ROW_COUNT = productDataReferenceDao.countJobReferences();
     productDataReferenceDao.truncateSearchTable();
     while (SearchTableReloadState.CURRENT_OFFSET < SearchTableReloadState.TABLE_ROW_COUNT) {
          ... long running task
          ....
          SearchTableReloadState.CURRENT_OFFSET += SearchTableReloadState.CURRENT_LIMIT;
        }
}

The method with the /state would report the current state, so I could call these with Ajax on a site. 使用/ state的方法将报告当前状态,因此我可以在站点上使用Ajax调用它们。 Problem is, If I start the long running one, the state report request won't complete until the long running did not complete. 问题是,如果我开始长时间运行,状态报告请求将无法完成,直到长时间运行未完成。 I thought Spring uses separate threads for each request. 我认为Spring为每个请求使用单独的线程。 Do I need to implement threading in Spring? 我需要在Spring中实现线程吗?

If I use the @Async annotation for the long running process, it works like I expected, but I still don't understand, why could two separate HTTP requests for a different method block each other! 如果我对长时间运行的进程使用@Async注释,它就像我预期的那样工作,但我仍然不明白,为什么两个单独的HTTP请求可以相互阻塞另一个方法!

If I use the @Async annotation on the method that is supposed to take a long time, the HTTP Request calling it will get a response immediately and it will run in the background and I can call the state method as I expected. 如果我在应该花费很长时间的方法上使用@Async注释,那么调用它的HTTP请求将立即获得响应并且它将在后台运行,我可以按照我的预期调用state方法。 Even though it is working, I still don't know why it won't work without the asynchronous execution. 即使它正在工作,我仍然不知道为什么没有异步执行它将无法工作。

If you want to use the @Async annotation, you have to put the @EnableAsync annotation on the class you used the @SpringBootApplication and/or @EnableAutoConfiguration . 如果要使用@Async批注,则必须将@EnableAsync批注放在使用@SpringBootApplication和/或@EnableAutoConfiguration

I hope someone can provide a better answer later. 我希望有人可以在以后提供更好的答案。

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

相关问题 Spring Boot休眠没有事务进行中 - Spring boot hibernate no transaction is in progress Spring 使用 2 个数据源启动“没有正在进行的事务” - Spring boot "no transaction is in progress" with 2 datasources 在Spring Boot应用程序中集成BIRT报告 - Integrating BIRT report in Spring Boot application 具有调试模式但禁用自动配置报告的 Spring Boot - Spring boot with debug mode but disabled autoconfig report Spring Boot Jasper报告空白PDF - Spring Boot Jasper Report Blank PDF 如何在 spring 启动中的 header 中设置文件大小以显示下载进度? - How to set file size in the header in spring boot to display the download progress? 在 Spring 启动应用程序中获取“javax.persistence.TransactionRequiredException:没有正在进行的事务” - Getting “javax.persistence.TransactionRequiredException: no transaction is in progress” in Spring Boot Application Spring Boot - 在另一个 API 正在进行时停止传入请求 - Spring Boot - Stopping the Incoming request when Another API is in progress Spring boot Birt Report with Angular 2 client 生成损坏的 PDF 文件 - Spring boot Birt Report with Angular 2 client Generates Corrupt PDF File Spring boot 是否提供任何用于生成覆盖率报告的内部库 - Does Spring boot provide any internal library for generating coverage report
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM