use of io.reactivex.observers.DisposableObserver in project snow-owl by b2ihealthcare.
the class ClassifyOperation method run.
/**
* Allocates a reasoner instance, performs the requested operation, then releases the borrowed instance back to the pool.
* @param monitor an {@link IProgressMonitor} to monitor operation progress
* @return the value returned by {@link #processResults(IProgressMonitor, long)}
* @throws OperationCanceledException
*/
public T run(final IProgressMonitor monitor) throws OperationCanceledException {
monitor.beginTask("Classification in progress...", IProgressMonitor.UNKNOWN);
try {
final String classificationId = UUID.randomUUID().toString();
final String jobId = IDs.sha1(classificationId);
final Notifications notifications = getServiceForClass(Notifications.class);
final BlockingQueue<RemoteJobEntry> jobQueue = Queues.newArrayBlockingQueue(1);
final Observable<RemoteJobEntry> jobObservable = notifications.ofType(RemoteJobNotification.class).filter(RemoteJobNotification::isChanged).filter(notification -> notification.getJobIds().contains(jobId)).concatMap(notification -> JobRequests.prepareSearch().one().filterById(jobId).buildAsync().execute(getEventBus())).map(RemoteJobs::first).map(Optional<RemoteJobEntry>::get).filter(RemoteJobEntry::isDone);
// "One-shot" subscription; it should self-destruct after the first notification
jobObservable.subscribe(new DisposableObserver<RemoteJobEntry>() {
@Override
public void onComplete() {
dispose();
}
@Override
public void onError(final Throwable t) {
dispose();
}
@Override
public void onNext(final RemoteJobEntry job) {
try {
jobQueue.put(job);
} catch (InterruptedException e) {
throw new SnowowlRuntimeException("Interrupted while trying to add a remote job entry to the queue.", e);
} finally {
dispose();
}
}
});
ClassificationRequests.prepareCreateClassification().setClassificationId(classificationId).setReasonerId(reasonerId).setUserId(userId).addAllConcepts(additionalConcepts).setParentLockContext(parentLockContext).build(branch).get(ApplicationContext.getServiceForClass(Environment.class));
while (true) {
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
try {
final RemoteJobEntry jobEntry = jobQueue.poll(CHECK_JOB_INTERVAL_SECONDS, TimeUnit.SECONDS);
if (jobEntry == null) {
continue;
}
switch(jobEntry.getState()) {
// $FALL-THROUGH$
case SCHEDULED:
case RUNNING:
case CANCEL_REQUESTED:
break;
case FINISHED:
try {
return processResults(classificationId);
} finally {
deleteEntry(jobId);
}
case CANCELED:
deleteEntry(jobId);
throw new OperationCanceledException();
case FAILED:
deleteEntry(jobId);
throw new SnowowlRuntimeException("Failed to retrieve the results of the classification.");
default:
throw new IllegalStateException("Unexpected state '" + jobEntry.getState() + "'.");
}
} catch (final InterruptedException e) {
// Nothing to do
}
}
} finally {
monitor.done();
}
}
use of io.reactivex.observers.DisposableObserver in project RxJava-Android-Samples by kaushikgopal.
the class RetrofitFragment method onListContributorsWithFullUserInfoClicked.
@OnClick(R.id.btn_demo_retrofit_contributors_with_user_info)
public void onListContributorsWithFullUserInfoClicked() {
_adapter.clear();
_disposables.add(_githubService.contributors(_username.getText().toString(), _repo.getText().toString()).flatMap(Observable::fromIterable).flatMap(contributor -> {
Observable<User> _userObservable = _githubService.user(contributor.login).filter(user -> !isEmpty(user.name) && !isEmpty(user.email));
return Observable.zip(_userObservable, Observable.just(contributor), Pair::new);
}).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new DisposableObserver<Pair<User, Contributor>>() {
@Override
public void onComplete() {
Timber.d("Retrofit call 2 completed ");
}
@Override
public void onError(Throwable e) {
Timber.e(e, "error while getting the list of contributors along with full " + "names");
}
@Override
public void onNext(Pair<User, Contributor> pair) {
User user = pair.first;
Contributor contributor = pair.second;
_adapter.add(format("%s(%s) has made %d contributions to %s", user.name, user.email, contributor.contributions, _repo.getText().toString()));
_adapter.notifyDataSetChanged();
Timber.d("%s(%s) has made %d contributions to %s", user.name, user.email, contributor.contributions, _repo.getText().toString());
}
}));
}
use of io.reactivex.observers.DisposableObserver in project RandomWebm by alkocher.
the class ToggleVotesUtil method toggleDislike.
public void toggleDislike(String webmId, boolean hasLike, boolean hasDislike) {
ApolloMutationCall<ToggleDislikeMutation.Data> dislikeMutationCall = WebmApolloClient.getWebmApolloClient().mutate(new ToggleDislikeMutation(webmId, hasLike, hasDislike));
mDisposable.add(Rx2Apollo.from(dislikeMutationCall).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribeWith(new DisposableObserver<Response<ToggleDislikeMutation.Data>>() {
@Override
public void onNext(Response<ToggleDislikeMutation.Data> dataResponse) {
}
@Override
public void onError(Throwable e) {
webmData.showErrorSnackbar();
}
@Override
public void onComplete() {
webmData.showSuccessSnackbar();
}
}));
}
Aggregations