我正在使用vertx.io Web框架将项目列表发送到下游HTTP服务器。
records.records()
发出4条记录,我专门将Web客户端设置为连接到错误的IP端口。
Processing...
打印4次。
Exception outer!
打印3次。
如果我放回正确的I.P /端口,那么Susbscribe outer!
将打印4次。
io.reactivex.Flowable
.fromIterable(records.records())
.flatMap(inRecord -> {
System.out.println("Processing...");
// Do stuff here....
Observable<Buffer> bodyBuffer = Observable.just(Buffer.buffer(...));
Single<HttpResponse<Buffer>> request = client
.post(..., ..., ...)
.rxSendStream(bodyBuffer);
return request.toFlowable();
})
.subscribe(record -> {
System.out.println("Subscribe outer!");
}, ex -> {
System.out.println("Exception outer! " + ex.getMessage());
});
更新:
我现在了解到,错误RX会停止。有没有一种方法可以继续处理所有记录,而每条记录都会出错?
答案 0 :(得分:1)
给出这篇文章:https://medium.com/@jagsaund/5-not-so-obvious-things-about-rxjava-c388bd19efbc
我已经提出了...除非您发现这有什么问题?
io.reactivex.Flowable
.fromIterable(records.records())
.flatMap
(inRecord -> {
Observable<Buffer> bodyBuffer = Observable.just(Buffer.buffer(inRecord.toString()));
Single<HttpResponse<Buffer>> request = client
.post("xxxxxx", "xxxxxx", "xxxxxx")
.rxSendStream(bodyBuffer);
// So we can capture how long each request took.
final long startTime = System.currentTimeMillis();
return request.toFlowable()
.doOnNext(response -> {
// Capture total time and print it with the logs. Removed below for brevity.
long processTimeMs = System.currentTimeMillis() - startTime;
int status = response.statusCode();
if(status == 200)
logger.info("Success!");
else
logger.error("Failed!");
}).doOnError(ex -> {
long processTimeMs = System.currentTimeMillis() - startTime;
logger.error("Failed! Exception.", ex);
}).doOnTerminate(() -> {
// Do some extra stuff here...
}).onErrorResumeNext(Flowable.empty()); // This will allow us to continue.
}
).subscribe(); // Don't handle here. We subscribe to the inner events.
答案 1 :(得分:0)
是否有一种方法可以继续处理所有记录而无论如何获取 每个错误吗?
根据doc,如果可观察对象遇到错误,则应将其终止。因此,您无法在onError
中得到每个错误。
您可以使用onErrorReturn
或onErrorResumeNext()
告诉上游如果遇到错误(例如发出null或Flowable.empty()
)时该怎么办。