Created
July 28, 2016 10:57
-
-
Save esarbanis/34e060ee945e6ba6ea90959f6d90e560 to your computer and use it in GitHub Desktop.
DeferredResultSubscriber
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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