我有以下代码,我想跟踪HttpRequest花费了多少时间并记录下来。
在顺序代码中,我们通常标记开始,执行和标记结束,最后标记 (endTime-startTime) 。
它应该能够打印成功或失败所花费的时间。
io.reactivex.Flowable
.fromIterable(records.records())
.parallel(5)
.flatMap(record -> {
Single<HttpResponse<String>> request = client
.get(xxxxxx, "xxx.xxx.xxx.xxx" , "/xxxxxxx")
.as(BodyCodec.string())
.rxSend();
return request.toFlowable();
}
)
.sequential()
.subscribe(record -> {
System.out.println(record.body());
}, ex -> {
ex.printStackTrace();
});
我设法做到了。但这并没有涵盖“例外”,这是正确的方向吗?
io.reactivex.Flowable
.fromIterable(records.records())
.parallel(5)
.flatMap(record -> {
Single<HttpResponse<String>> request = client
.get(xxxxxx, "xxx.xxx.xxx.xxx" , "xxxxxx")
.as(BodyCodec.string())
.rxSend();
MyWrapper wrapper = new MyWrapper(System.currentTimeMillis(), request);
return Flowable.just(wrapper);
}
)
.sequential()
.subscribe(record -> {
long startTime = record.getStartTime();
record.getHttpResponse().toFlowable().subscribe(t -> {
long endTime = System.currentTimeMillis();
System.out.println("Took: " + (endTime - startTime) + " - " + t.body());
});
}, ex -> {
ex.printStackTrace();
});
谢谢
答案 0 :(得分:0)
这很好地完成了窍门...
.flatMap(record -> {
Single<HttpResponse<String>> request = client
.get(xxxxxx, "xxxxxx", "xxxxxx")
.as(BodyCodec.string())
.rxSend();
final long startTime = System.currentTimeMillis();
return request.toFlowable()
.doOnError(ex -> {
long endTime = System.currentTimeMillis();
System.out.println("Took: " + (endTime - startTime));
ex.printStackTrace();
})
.doOnComplete(() -> {
long endTime = System.currentTimeMillis();
System.out.println("Took: " + (endTime - startTime));
});
}
)