com.firefly.reactive.adapter.common.ReactiveUtils Maven / Gradle / Ivy
package com.firefly.reactive.adapter.common;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import java.util.concurrent.CompletableFuture;
/**
* @author Pengtao Qiu
*/
abstract public class ReactiveUtils {
public static CompletableFuture toFuture(Publisher publisher) {
CompletableFuture future = new CompletableFuture<>();
publisher.subscribe(new Subscriber() {
@Override
public void onSubscribe(Subscription s) {
s.request(1);
}
@Override
public void onNext(T t) {
future.complete(t);
}
@Override
public void onError(Throwable t) {
future.completeExceptionally(t);
}
@Override
public void onComplete() {
if (!future.isDone()) {
future.complete(null);
}
}
});
return future;
}
}