Skip to content

Instantly share code, notes, and snippets.

@esarbanis
Created July 28, 2016 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esarbanis/34e060ee945e6ba6ea90959f6d90e560 to your computer and use it in GitHub Desktop.
Save esarbanis/34e060ee945e6ba6ea90959f6d90e560 to your computer and use it in GitHub Desktop.
DeferredResultSubscriber
import org.springframework.web.context.request.async.DeferredResult;
import rx.Observable;
import rx.Observer;
import rx.Subscription;
/**
* A {@link DeferredResult} that subscribes to an observable
*
* @see DeferredResult
*/
public class DeferredResultSubscriber<T> extends DeferredResult<T> implements Observer<T>, Runnable{
private final Subscription subscription;
private boolean completed;
public DeferredResultSubscriber(Observable<T> observable) {
onTimeout(this);
onCompletion(this);
this.subscription = observable.subscribe(this);
}
@Override
public void onNext(T value) {
if (!completed) {
setResult(value);
}
}
@Override
public void onError(Throwable e) {
setErrorResult(e);
}
@Override
public void onCompleted() {
completed = true;
}
@Override
public void run() {
this.subscription.unsubscribe();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment