简体   繁体   English

使用Observable.timeout后超时后会发生什么?

[英]What will happen after timeout when using Observable.timeout?

I have an Observable that go to database and query for some information. 我有一个Observable去数据库并查询一些信息。 I don't want my observable executes longer than 5 seconds, thus I use: 我不希望我的observable执行时间超过5秒,因此我使用:

myObservable.timeout(5,second);

Then I want to handle the error notification also, thus I use: 然后我也想处理错误通知,因此我使用:

myObservable.timeout(5,second).onError(return empty result);

Then I wonder for what will happen to the code in myObservable that is used to do database query. 然后我想知道myObservable中用于进行数据库查询的代码会发生什么。 Will it also be terminated, or it will continue to run ? 它会被终止,还是会继续运行? (which happens to Java native Future.get(timeLimit) ) (恰好是Java native Future.get(timeLimit)

Let's take an example : 我们来举个例子:

Observable.interval(1, TimeUnit.SECONDS)
            .timeout(10, TimeUnit.MICROSECONDS)
            .onErrorReturn(e -> -1L)
            .subscribe(System.out::println,
                       Throwable::printStackTrace,
                       () -> System.err.println("completed"));

the timeout operator will emit an error. timeout运算符将发出错误。 But precedent operators won't be notifier of this error. 但先例运算符不会是此错误的通知者。

The operator onErrorReturn will transform your error to an event and then will complete your stream (and mark it as finished) and then your source observable will be unsubscribe. 操作符onErrorReturn会将您的错误转换为事件,然后将完成您的流(并将其标记为已完成),然后您的源可观察将取消订阅。

This unsubscription part will run some code that, depending of how your source observable is written, that may stop your request, or just do nothing, or free some resources. 这个取消订阅部分将运行一些代码,这些代码可能会停止您的请求,或者什么都不做,或者释放一些资源,这取决于您的源可观察源的编写方式。

In your case, it may call the cancel method on your Future (according to the Subscriptions class) 在你的情况下,它可以调用你的Future上的cancel方法(根据Subscriptions类)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM