[英]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.