io.reactivex.rxjava3.core.BackpressureStrategy Java Examples

The following examples show how to use io.reactivex.rxjava3.core.BackpressureStrategy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CacheFirstTimeoutStrategy.java    From RxCache with Apache License 2.0 6 votes vote down vote up
@Override
public <T> Publisher<Record<T>> execute(RxCache rxCache, String key, Flowable<T> source, Type type, BackpressureStrategy backpressureStrategy) {

    Flowable<Record<T>> cache = rxCache.<T>load2Flowable(key, type, backpressureStrategy)
            .filter(new Predicate<Record<T>>() {
                @Override
                public boolean test(Record<T> record) throws Exception {
                    return System.currentTimeMillis() - record.getCreateTime() <= timestamp;
                }
            });

    Flowable<Record<T>> remote = source
            .map(new Function<T, Record<T>>() {
                @Override
                public Record<T> apply(@NonNull T t) throws Exception {

                    rxCache.save(key, t);

                    return new Record<>(Source.CLOUD, key, t);
                }
            });

    return cache.switchIfEmpty(remote);
}
 
Example #2
Source File: CacheAndRemoteStrategy.java    From RxCache with Apache License 2.0 6 votes vote down vote up
@Override
public <T> Publisher<Record<T>> execute(RxCache rxCache, String key, Flowable<T> source, Type type, BackpressureStrategy backpressureStrategy) {

    Flowable<Record<T>> cache = rxCache.<T>load2Flowable(key, type, backpressureStrategy);

    Flowable<Record<T>> remote = source
            .map(new Function<T, Record<T>>() {
                @Override
                public Record<T> apply(@NonNull T t) throws Exception {

                    rxCache.save(key, t);

                    return new Record<>(Source.CLOUD, key, t);
                }
            });

    return Flowable.concatDelayError(Arrays.asList(cache, remote))
            .filter(new Predicate<Record<T>>() {
                @Override
                public boolean test(@NonNull Record<T> record) throws Exception {
                    return record.getData() != null;
                }
            });
}
 
Example #3
Source File: RemoteFirstStrategy.java    From RxCache with Apache License 2.0 6 votes vote down vote up
@Override
public <T> Publisher<Record<T>> execute(RxCache rxCache, String key, Flowable<T> source, Type type, BackpressureStrategy backpressureStrategy) {

    Flowable<Record<T>> cache = rxCache.<T>load2Flowable(key, type, backpressureStrategy);

    Flowable<Record<T>> remote = source
            .map(new Function<T, Record<T>>() {
                @Override
                public Record<T> apply(@NonNull T t) throws Exception {

                    rxCache.save(key, t);

                    return new Record<>(Source.CLOUD, key, t);
                }
            });

    return remote.switchIfEmpty(cache);
}
 
Example #4
Source File: RemoteOnlyStrategy.java    From RxCache with Apache License 2.0 6 votes vote down vote up
@Override
public <T> Publisher<Record<T>> execute(RxCache rxCache, String key, Flowable<T> source, Type type, BackpressureStrategy backpressureStrategy) {

    Flowable<Record<T>> remote = source
            .map(new Function<T, Record<T>>() {
                @Override
                public Record<T> apply(@NonNull T t) throws Exception {

                    rxCache.save(key, t);

                    return new Record<>(Source.CLOUD, key, t);
                }
            });

    return remote;
}
 
Example #5
Source File: CacheFirstStrategy.java    From RxCache with Apache License 2.0 6 votes vote down vote up
@Override
public <T> Publisher<Record<T>> execute(RxCache rxCache, String key, Flowable<T> source, Type type, BackpressureStrategy backpressureStrategy) {

    Flowable<Record<T>> cache = rxCache.<T>load2Flowable(key, type, backpressureStrategy);

    Flowable<Record<T>> remote = source
            .map(new Function<T, Record<T>>() {
                @Override
                public Record<T> apply(@NonNull T t) throws Exception {

                    rxCache.save(key, t);

                    return new Record<>(Source.CLOUD, key, t);
                }
            });

    return cache.switchIfEmpty(remote);
}
 
Example #6
Source File: FlowableRxInvokerImpl.java    From cxf with Apache License 2.0 5 votes vote down vote up
private <T> Flowable<T> create(Supplier<T> supplier) {
    Flowable<T> flowable = Flowable.create(new FlowableOnSubscribe<T>() {
        @Override
        public void subscribe(FlowableEmitter<T> emitter) throws Exception {
            try {
                T response = supplier.get();
                if (!emitter.isCancelled()) {
                    emitter.onNext(response);
                }
                
                if (!emitter.isCancelled()) {
                    emitter.onComplete();
                }
            } catch (Throwable e) {
                if (!emitter.isCancelled()) {
                    emitter.onError(e);
                }
            }
        }
    }, BackpressureStrategy.DROP);
    
    if (sc == null) {
        return flowable.subscribeOn(Schedulers.io());
    }
    
    return flowable.subscribeOn(sc).observeOn(sc);
}
 
Example #7
Source File: DefaultDispatchManager.java    From catnip with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Flowable<T> asFlowable(final BackpressureStrategy backpressureStrategy) {
    return Flowable.create(emitter -> {
        internalHandler = emitter::onNext;
        emitter.setCancellable(this::close);
    }, backpressureStrategy);
}
 
Example #8
Source File: RxJava3FlowableService.java    From cxf with Apache License 2.0 5 votes vote down vote up
@GET
@Produces("application/json")
@Path("textJsonImplicitList")
public Flowable<HelloWorldBean> getJsonImplicitList() {
    return Flowable.create(subscriber -> {
        Thread t = new Thread(() -> {
            subscriber.onNext(new HelloWorldBean("Hello"));
            sleep();
            subscriber.onNext(new HelloWorldBean("Ciao"));
            sleep();
            subscriber.onComplete();
        });
        t.start();
    }, BackpressureStrategy.MISSING);
}
 
Example #9
Source File: RequestContextAssemblyTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
static Flowable<String> flowable(int count) {
    RequestContext.current();
    return Flowable.create(emitter -> {
        pool.submit(() -> {
            for (int i = 0; i < count; i++) {
                emitter.onNext(String.valueOf(count));
            }
            emitter.onComplete();
        });
    }, BackpressureStrategy.BUFFER);
}
 
Example #10
Source File: NoCacheStrategy.java    From RxCache with Apache License 2.0 5 votes vote down vote up
@Override
public <T> Publisher<Record<T>> execute(RxCache rxCache, String key, Flowable<T> source, Type type, BackpressureStrategy backpressureStrategy) {

    return source.map(new Function<T, Record<T>>() {
        @Override
        public Record<T> apply(@NonNull T t) throws Exception {
            return new Record<>(Source.CLOUD, key, t);
        }
    });
}
 
Example #11
Source File: CacheOnlyStrategy.java    From RxCache with Apache License 2.0 4 votes vote down vote up
@Override
public <T> Publisher<Record<T>> execute(RxCache rxCache, String key, Flowable<T> source, Type type, BackpressureStrategy backpressureStrategy) {

    return rxCache.<T>load2Flowable(key, type, backpressureStrategy);
}
 
Example #12
Source File: Rx3Apollo.java    From apollo-android with MIT License 4 votes vote down vote up
@NotNull
@CheckReturnValue
public static <T> Flowable<Response<T>> from(@NotNull ApolloSubscriptionCall<T> call) {
  return from(call, BackpressureStrategy.LATEST);
}
 
Example #13
Source File: Rx3Apollo.java    From apollo-android with MIT License 4 votes vote down vote up
@NotNull
@CheckReturnValue
public static <T> Flowable<Response<T>> from(@NotNull final ApolloSubscriptionCall<T> call,
    @NotNull BackpressureStrategy backpressureStrategy) {
  checkNotNull(call, "originalCall == null");
  checkNotNull(backpressureStrategy, "backpressureStrategy == null");
  return Flowable.create(new FlowableOnSubscribe<Response<T>>() {
    @Override public void subscribe(final FlowableEmitter<Response<T>> emitter) throws Exception {
      cancelOnFlowableDisposed(emitter, call);
      call.execute(
          new ApolloSubscriptionCall.Callback<T>() {
            @Override public void onResponse(@NotNull Response<T> response) {
              if (!emitter.isCancelled()) {
                emitter.onNext(response);
              }
            }

            @Override public void onFailure(@NotNull ApolloException e) {
              Exceptions.throwIfFatal(e);
              if (!emitter.isCancelled()) {
                emitter.onError(e);
              }
            }

            @Override public void onCompleted() {
              if (!emitter.isCancelled()) {
                emitter.onComplete();
              }
            }

            @Override public void onTerminated() {
              onFailure(new ApolloSubscriptionTerminatedException("Subscription server unexpectedly terminated "
                  + "connection"));
            }

            @Override public void onConnected() {
            }
          }
      );
    }
  }, backpressureStrategy);
}
 
Example #14
Source File: DataServiceImpl.java    From java-11-examples with Apache License 2.0 4 votes vote down vote up
@Override
public Flowable<DataItem> getDataFlowWithBackPressure(DataQuery dataQuery) {
    return Flowable.create(new FlowableDataProducer(executor, dataQuery), BackpressureStrategy.BUFFER);
}
 
Example #15
Source File: LifecycleTransformer.java    From RxLifecycle with Apache License 2.0 4 votes vote down vote up
@Override
public Publisher<T> apply(Flowable<T> upstream) {
    return upstream.takeUntil(observable.toFlowable(BackpressureStrategy.LATEST));
}
 
Example #16
Source File: MessageConsumer.java    From catnip with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
default Flowable<T> asFlowable() {
    return asFlowable(BackpressureStrategy.BUFFER);
}
 
Example #17
Source File: RxQuery.java    From objectbox-java with Apache License 2.0 2 votes vote down vote up
/**
 * The returned Flowable emits Query results one by one. Once all results have been processed, onComplete is called.
 * Uses BackpressureStrategy.BUFFER.
 */
public static <T> Flowable<T> flowableOneByOne(final Query<T> query) {
    return flowableOneByOne(query, BackpressureStrategy.BUFFER);
}
 
Example #18
Source File: RxQuery.java    From objectbox-java with Apache License 2.0 2 votes vote down vote up
/**
 * The returned Flowable emits Query results one by one. Once all results have been processed, onComplete is called.
 * Uses given BackpressureStrategy.
 */
public static <T> Flowable<T> flowableOneByOne(final Query<T> query, BackpressureStrategy strategy) {
    return Flowable.create(emitter -> createListItemEmitter(query, emitter), strategy);
}
 
Example #19
Source File: FlowableStrategy.java    From RxCache with Apache License 2.0 votes vote down vote up
<T> Publisher<Record<T>> execute(RxCache rxCache, String key, Flowable<T> source, Type type, BackpressureStrategy backpressureStrategy); 
Example #20
Source File: MessageConsumer.java    From catnip with BSD 3-Clause "New" or "Revised" License votes vote down vote up
Flowable<T> asFlowable(BackpressureStrategy backpressureStrategy);