作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我向第三方 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/
我是一名优秀的程序员,十分优秀!