Search in sources :

Example 16 with Flowable

use of io.reactivex.Flowable in project rxjava2-jdbc by davidmoten.

the class NonBlockingPool method createMembers.

private Flowable<Member<T>> createMembers() {
    List<Member<T>> ms = IntStream.range(1, maxSize + 1).mapToObj(//
    n -> memberFactory.create(NonBlockingPool.this)).collect(Collectors.toList());
    Flowable<Member<T>> baseMembers = //
    Flowable.fromIterable(ms).doOnRequest(n -> log.debug("requested={}", n));
    Flowable<Member<T>> returnedMembers = //
    subject.toSerialized().toFlowable(BackpressureStrategy.BUFFER);
    // use CAS loop to handle a race with close
    while (true) {
        List<Member<T>> l = list.get();
        if (l == null) {
            return Flowable.error(new PoolClosedException());
        } else if (list.compareAndSet(Collections.emptyList(), ms)) {
            return //
            returnedMembers.doOnNext(//
            m -> log.debug("returned member reentering")).mergeWith(//
            baseMembers).doOnNext(//
            x -> log.debug("member={}", x)).<Member<T>>flatMap(member -> member.checkout().toFlowable(), false, //
            1).doOnNext(x -> log.debug("checked out member={}", x));
        }
    }
}
Also used : IntStream(java.util.stream.IntStream) BackpressureStrategy(io.reactivex.BackpressureStrategy) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Preconditions(com.github.davidmoten.guavamini.Preconditions) Callable(java.util.concurrent.Callable) Predicate(io.reactivex.functions.Predicate) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors(java.util.stream.Collectors) Consumer(io.reactivex.functions.Consumer) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Scheduler(io.reactivex.Scheduler) Flowable(io.reactivex.Flowable) PublishSubject(io.reactivex.subjects.PublishSubject) PoolClosedException(org.davidmoten.rx.jdbc.pool.PoolClosedException) Schedulers(io.reactivex.schedulers.Schedulers) Collections(java.util.Collections) PoolClosedException(org.davidmoten.rx.jdbc.pool.PoolClosedException)

Aggregations

Flowable (io.reactivex.Flowable)16 Test (org.junit.Test)7 Maybe (io.reactivex.Maybe)5 Single (io.reactivex.Single)5 Observable (io.reactivex.Observable)4 Type (java.lang.reflect.Type)4 List (java.util.List)4 TimeUnit (java.util.concurrent.TimeUnit)4 Schedulers (io.reactivex.schedulers.Schedulers)3 Collections (java.util.Collections)3 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)3 Nullable (android.support.annotation.Nullable)2 Scheduler (io.reactivex.Scheduler)2 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)2 Queue (java.util.Queue)2 Singleton (javax.inject.Singleton)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 WebTarget (javax.ws.rs.client.WebTarget)2