【发布时间】:2020-05-28 05:52:59
【问题描述】:
这是我第一次使用 Spring Reactor,我面临以下挑战:
我有一项服务,它允许使用由页码和页面大小指定的许多记录:
Mono<GetContactsForGroupResponse> getContactsForGroup(Integer page, Integer size);
GetContactsForGroupResponse 包含分页元数据的其他字段:
class GetContactsForGroupResponse {
private int totalPages;
private int totalElements;
private int numberOfElements;
private int size;
private int number;
private boolean first;
private boolean last;
//.....
}
现在我需要编写另一个方法来读取来自
的所有页面Mono<GetContactsForGroupResponse> getContactsForGroup(Integer page, Integer size);
并将结果合并到一个集合中:
Mono<Collection<GetContactsForGroupResponse>> getContactsForGroup();
到目前为止,我已经写了:
List<GetContactsForGroupResponse> groupContacts = new ArrayList<>();
AtomicBoolean allPagesConsumed = new AtomicBoolean(false);
int pageNumber = 0;
int pageSize = 10;
while(!allPagesConsumed.get()) {
allPagesConsumed.set(true);
GetContactsForGroupResponse getContactsForGroupResponse =
getContactsForGroup(accountId, g.getId(), 0, pageSize).block();
Optional.ofNullable(getContactsForGroupResponse)
.ifPresent(r -> {
allPagesConsumed.set(r.isLast());
groupContacts.add(r);
});
pageNumber ++;
我逐页阅读结果,直到读到最后一页。 我想知道从 SpringReactor 的角度来看,正确的实现方式是什么
任何建议都将不胜感激,
谢谢
【问题讨论】:
标签: spring spring-webflux spring-reactive spring-reactor spring-mono