Search in sources :

Example 1 with PoolClosedException

use of org.davidmoten.rx.jdbc.pool.PoolClosedException 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

Preconditions (com.github.davidmoten.guavamini.Preconditions)1 BackpressureStrategy (io.reactivex.BackpressureStrategy)1 Flowable (io.reactivex.Flowable)1 Scheduler (io.reactivex.Scheduler)1 Consumer (io.reactivex.functions.Consumer)1 Predicate (io.reactivex.functions.Predicate)1 Schedulers (io.reactivex.schedulers.Schedulers)1 PublishSubject (io.reactivex.subjects.PublishSubject)1 Collections (java.util.Collections)1 List (java.util.List)1 Callable (java.util.concurrent.Callable)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 PoolClosedException (org.davidmoten.rx.jdbc.pool.PoolClosedException)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1