简体   繁体   English

在Observable中发出超时项

[英]Emit item on timeout in Observable

I have two PublishSubject s that model a queue where I receive and push jobs to. 我有两个PublishSubject ,它们为接收和推送作业的队列建模。

I want to be able to react to jobs that have been consumed from the first PublishSubject , but not put in the second PublishSubject in a given time window (eg 10s): 我希望能够对从第一个PublishSubject消耗的工作做出反应,但不能在给定的时间范围内(例如10s)放入第二个PublishSubject

final Subject<Job> queue = PublishSubject.<Job>create().toSerialized();
final Subject<Job> done = PublishSubject.<Job>create().toSerialized();
// this is probably wrong already since I am consuming items from queue
queue.subscribe(done::onNext);

final Observable<Job> timeOut = queue.timeout(10, SECONDS, Observable.empty()); // ??

You haven't described what you actually want to do after 10 seconds pass. 经过10秒后,您还没有描述您实际想要做什么。 Do you want to re-add that job to queue again? 您是否要重新添加该作业以再次排队? Do you want to just get information if processing takes more than 10 secs, or do you want to skip that job if it takes too long? 如果处理时间超过10秒,您是否只想获取信息,或者如果处理时间太长,是否要跳过该工作?

If you want to skip processing that job and resume with some new observable you can flatMap each element of queue and timeout them separately. 如果要跳过处理该作业并使用一些新的可观察到的内容继续,则可以对每个队列元素进行flatMap并分别使它们timeout

Observable<Job> observableThatWillTriggerOnTimeout = ...
queue.flatMap(job -> dispatchJob(job)
                    .timeout(10, TimeUnit.SECONDS, observableThatWillTriggerOnTimeout)
             )

Tell more about your scenario, I will update my answer if necessary 详细介绍您的情况,如有必要,我将更新我的答案

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

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