-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to implement polling using Observables? #448
Comments
I think about this implementation:
But we have recursive subscribe here. Probably this is bad usage of observables. Could you share your opinion? |
What about this?
|
@samuelgruetter interval won't wait until operation will be completed. im also guess that same params will be used for each request in this case. |
Yes.
PS I had to delete one post because I accidentally hit 'comment' too early on my smartphone |
Any conclusion on this? |
@benjchristensen probably yes. If we will use @samuelgruetter implementation with recently implemented TimeInterval operator.
Also in my case i still have hacky implementation since my delay is dynamic and I need Delay operator to be implemented. |
How about something like this? Observable.create({ observer ->
Schedulers.newThread().schedulePeriodically({
observer.onNext("application-state-from-network");
}, 0, 1000, TimeUnit.MILLISECONDS);
}).take(10).subscribe({ v -> println(v) }); |
Here is Java using manual recursion instead of Observable.create(new OnSubscribeFunc<String>() {
@Override
public Subscription onSubscribe(final Observer<? super String> o) {
return Schedulers.newThread().schedule(0L, new Func2<Scheduler, Long, Subscription>() {
@Override
public Subscription call(Scheduler inner, Long t2) {
o.onNext("data-from-polling");
return inner.schedule(t2, this, 1000, TimeUnit.MILLISECONDS);
}
});
}
}).toBlockingObservable().forEach(new Action1<String>() {
@Override
public void call(String v) {
System.out.println("output: " + v);
}
}); |
I think primary problem is that schedulePeriodically do not wait till operation is completed. Java ScheduledExecutorService(http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html) has two different methods:
So I think we need to add scheduleWithFixedDelay analog to Rx scheduler. |
@benjchristensen Haven't noticed you second message. Thanks a lot for this manual recursion implementation! |
How can scheduling periodically but wait for completion be done today using RxJava? Your code snippet has several things that have been deprecated, so I am wondering what an alternative would look like? |
Use Observable.interval(1, TimeUnit.SECONDS, Schedulers.io())
.map(time -> pollSource())
.toBlocking()
.subscribe(System.out::println, Throwable::printStackTrace) |
The problem with that is that the "interval" operator is dumb. It will continue to pump out a pulse every X seconds without any regards what happens down the line. That is not what I want. The "toBlocking" does nothing to fix this. The behavior I want is a new tick from the time the poll finished not every 20 seconds: What your code does: What I want: |
Okay then, how about: Observable.fromCallable(() -> pollValue())
.repeatWhen(o -> o.concatMap(v -> Observable.timer(20, TimeUnit.SECONDS))); |
I did some testing and it looks like it's working as intended. I will have to throw in some conditional check so it doesn't go on forever but it looks like we have a winner! Thank you so much! I really appreciate your help! |
@akarnokd |
@cxzhang2 An unsubscribe should stop that timer. Do you have some code that demonstrates what you experience? |
@akarnokd totally my mistake, great solution thank you! |
I found this post, hope it helps. |
You can use |
I've got parametrized rest call with Observable interface:
I want to repeat it with delay so I have created delayed version of it:
But now I want to have polling observable that will recreate "delayedApiCall" with params and produce continuous results. I also want observable to continue producing results even if error was returned in "delayedApiCall".
The text was updated successfully, but these errors were encountered: