gpt4 book ai didi

java - 如何在Reactor中进行分页?

转载 作者:行者123 更新时间:2023-12-03 08:23:35 26 4
gpt4 key购买 nike

我向第三方 Web 服务重复发出分页 WebClient 请求。我现在的实现可以工作,但正在阻塞。

到目前为止我的实现:

var elementsPerPage = 10;
Flux
.generate(
() -> 0,
(pageIndex, emitter) -> {
BlahServiceResponse blahServiceResponse =
webClient
.get()
.uri("/blah?pageIndex={pageIndex}", pageIndex)
.retrieve()
.bodyToMono(BlahServiceResponse.class)
.block(); // Yuck!!!
if (blahServiceResponse.getStudents().size() > 0) {
emitter.next(blahServiceResponse);
} else {
emitter.complete();
}
return pageIndex + elementsPerPage;
}
)
.subscribe(System.out::println); // Replace me with actual logic

出于可以理解的原因,如果将上面的代码更改为以下内容,则会抛出“IllegalStateException:生成器没有调用任何 SynchronousSink 方法”异常:

webClient
.get()
...
.bodyToMono(BlahServiceResponse.class)
.subscribe(emitter::next);

所以我开始寻找异步接收器和 realized it was Flux|MonoSink 。但据我所知,Flux 中没有支持使用 Flux|MonoSink 生成有状态元素的构建器方法。

我错过了什么吗?有没有更优雅的方法?

最佳答案

静态分页

如果您事先知道页面索引并且有生成它的规则。

var elementsPerPage = 10;

Flux.generate(
() -> 0,
(pageIndex, emitter) -> {
if (pageIndex < 30) {
emitter.next(pageIndex);
} else {
emitter.complete();
}
return pageIndex + elementsPerPage;
})
.flatMap(pageIndex -> webClient
.get()
.uri("/blah?pageIndex={pageIndex}", pageIndex)
.retrieve()
.bodyToMono(BlahServiceResponse.class))
.subscribe(System.out::println);

动态分页

下一页索引是否依赖于最后查询的页面。

public static void main(String[] args) {
var elementsPerPage = 10;

callWithPageIndex(0)
.expand(pagedResponse -> {
if (pagedResponse.getResponse().isEmpty()) {
return Mono.empty();
} else {
return callWithPageIndex(pagedResponse.getPageIndex() + elementsPerPage);
}
})
.subscribe(System.out::println);
}

private static Mono<PagedResponse<BlahServiceResponse>> callWithPageIndex(Integer pageIndex) {
return webClient
.get()
.uri("/blah?pageIndex={pageIndex}", pageIndex)
.retrieve()
.bodyToMono(BlahServiceResponse.class)
.map(response -> new PagedResponse<>(pageIndex, response));
}

@lombok.Value
static class PagedResponse<T> {
int pageIndex;
T response;
}

关于java - 如何在Reactor中进行分页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67129283/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com