[我正在尝试为Vertx web-client写一个包装器,以使用来自reactstream的Publisher
从服务器加载响应主体:
import org.reactivestreams.Publisher;
import io.vertx.reactivex.ext.web.client.WebClient;
interface Storage {
Publisher<ByteBuffer> load(String key);
}
class WebStorage implements Storage {
private final WebClient client;
public WebStorage(final WebClient client) {
this.client = client;
}
@Override
public Publisher<ByteBuffer> laod(final String key) {
return client.get(String.format("https://myhost/path?query=%s", key))
.rxSend()
.toFlowable()
.map(resp -> ByteBuffer.wrap(resp.body().getBytes()));
}
}
此解决方案是不正确的,因为它通过getBytes()
调用以阻塞方式读取所有主体字节。
是否可以按块读取Vertx WebClient
的响应并将其转换为Publisher
(或Rx Flowable
)?
Vert.x Web客户端并非旨在流式传输响应主体。它按设计缓冲内容。
如果要流式传输内容,则可以使用更灵活的基础HTTP客户端。