Search in sources :

Example 1 with ConnectableFlowable

use of io.reactivex.flowables.ConnectableFlowable in project RxJava by ReactiveX.

the class RxJavaPluginsTest method overrideConnectableFlowable.

@SuppressWarnings("rawtypes")
@Test
public void overrideConnectableFlowable() {
    try {
        RxJavaPlugins.setOnConnectableFlowableAssembly(new Function<ConnectableFlowable, ConnectableFlowable>() {

            @Override
            public ConnectableFlowable apply(ConnectableFlowable co) throws Exception {
                return new ConnectableFlowable() {

                    @Override
                    public void connect(Consumer connection) {
                    }

                    @SuppressWarnings("unchecked")
                    @Override
                    protected void subscribeActual(Subscriber subscriber) {
                        subscriber.onSubscribe(new ScalarSubscription(subscriber, 10));
                    }
                };
            }
        });
        Flowable.just(1).publish().autoConnect().test().assertResult(10);
    } finally {
        RxJavaPlugins.reset();
    }
    Flowable.just(1).publish().autoConnect().test().assertResult(1);
}
Also used : ConnectableFlowable(io.reactivex.flowables.ConnectableFlowable) ScalarSubscription(io.reactivex.internal.subscriptions.ScalarSubscription)

Example 2 with ConnectableFlowable

use of io.reactivex.flowables.ConnectableFlowable in project RxJava by ReactiveX.

the class RxJavaPluginsTest method clearIsPassthrough.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void clearIsPassthrough() {
    try {
        RxJavaPlugins.reset();
        assertNull(RxJavaPlugins.onAssembly((Observable) null));
        assertNull(RxJavaPlugins.onAssembly((ConnectableObservable) null));
        assertNull(RxJavaPlugins.onAssembly((Flowable) null));
        assertNull(RxJavaPlugins.onAssembly((ConnectableFlowable) null));
        Observable oos = new Observable() {

            @Override
            public void subscribeActual(Observer t) {
            }
        };
        Flowable fos = new Flowable() {

            @Override
            public void subscribeActual(Subscriber t) {
            }
        };
        assertSame(oos, RxJavaPlugins.onAssembly(oos));
        assertSame(fos, RxJavaPlugins.onAssembly(fos));
        assertNull(RxJavaPlugins.onAssembly((Single) null));
        Single sos = new Single() {

            @Override
            public void subscribeActual(SingleObserver t) {
            }
        };
        assertSame(sos, RxJavaPlugins.onAssembly(sos));
        assertNull(RxJavaPlugins.onAssembly((Completable) null));
        Completable cos = new Completable() {

            @Override
            public void subscribeActual(CompletableObserver t) {
            }
        };
        assertSame(cos, RxJavaPlugins.onAssembly(cos));
        assertNull(RxJavaPlugins.onAssembly((Maybe) null));
        assertNull(RxJavaPlugins.onSchedule(null));
        Maybe myb = new Maybe() {

            @Override
            public void subscribeActual(MaybeObserver t) {
            }
        };
        assertSame(myb, RxJavaPlugins.onAssembly(myb));
        assertNull(RxJavaPlugins.onSchedule(null));
        Runnable action = Functions.EMPTY_RUNNABLE;
        assertSame(action, RxJavaPlugins.onSchedule(action));
        class AllSubscriber implements Subscriber, Observer, SingleObserver, CompletableObserver, MaybeObserver {

            @Override
            public void onSuccess(Object value) {
            }

            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onSubscribe(Subscription s) {
            }

            @Override
            public void onNext(Object t) {
            }

            @Override
            public void onError(Throwable t) {
            }

            @Override
            public void onComplete() {
            }
        }
        AllSubscriber all = new AllSubscriber();
        assertNull(RxJavaPlugins.onSubscribe(Observable.never(), null));
        assertSame(all, RxJavaPlugins.onSubscribe(Observable.never(), all));
        assertNull(RxJavaPlugins.onSubscribe(Flowable.never(), null));
        assertSame(all, RxJavaPlugins.onSubscribe(Flowable.never(), all));
        assertNull(RxJavaPlugins.onSubscribe(Single.just(1), null));
        assertSame(all, RxJavaPlugins.onSubscribe(Single.just(1), all));
        assertNull(RxJavaPlugins.onSubscribe(Completable.never(), null));
        assertSame(all, RxJavaPlugins.onSubscribe(Completable.never(), all));
        assertNull(RxJavaPlugins.onSubscribe(Maybe.never(), null));
        assertSame(all, RxJavaPlugins.onSubscribe(Maybe.never(), all));
        // These hooks don't exist in 2.0
        //            Subscription subscription = Subscriptions.empty();
        //
        //            assertNull(RxJavaPlugins.onObservableReturn(null));
        //
        //            assertSame(subscription, RxJavaPlugins.onObservableReturn(subscription));
        //
        //            assertNull(RxJavaPlugins.onSingleReturn(null));
        //
        //            assertSame(subscription, RxJavaPlugins.onSingleReturn(subscription));
        //
        //            TestException ex = new TestException();
        //
        //            assertNull(RxJavaPlugins.onObservableError(null));
        //
        //            assertSame(ex, RxJavaPlugins.onObservableError(ex));
        //
        //            assertNull(RxJavaPlugins.onSingleError(null));
        //
        //            assertSame(ex, RxJavaPlugins.onSingleError(ex));
        //
        //            assertNull(RxJavaPlugins.onCompletableError(null));
        //
        //            assertSame(ex, RxJavaPlugins.onCompletableError(ex));
        //
        //            Observable.Operator oop = new Observable.Operator() {
        //                @Override
        //                public Object call(Object t) {
        //                    return t;
        //                }
        //            };
        //
        //            assertNull(RxJavaPlugins.onObservableLift(null));
        //
        //            assertSame(oop, RxJavaPlugins.onObservableLift(oop));
        //
        //            assertNull(RxJavaPlugins.onSingleLift(null));
        //
        //            assertSame(oop, RxJavaPlugins.onSingleLift(oop));
        //
        //            Completable.CompletableOperator cop = new Completable.CompletableOperator() {
        //                @Override
        //                public CompletableSubscriber call(CompletableSubscriber t) {
        //                    return t;
        //                }
        //            };
        //
        //            assertNull(RxJavaPlugins.onCompletableLift(null));
        //
        //            assertSame(cop, RxJavaPlugins.onCompletableLift(cop));
        final Scheduler s = ImmediateThinScheduler.INSTANCE;
        Callable<Scheduler> c = new Callable<Scheduler>() {

            @Override
            public Scheduler call() throws Exception {
                return s;
            }
        };
        assertSame(s, RxJavaPlugins.onComputationScheduler(s));
        assertSame(s, RxJavaPlugins.onIoScheduler(s));
        assertSame(s, RxJavaPlugins.onNewThreadScheduler(s));
        assertSame(s, RxJavaPlugins.onSingleScheduler(s));
        assertSame(s, RxJavaPlugins.initComputationScheduler(c));
        assertSame(s, RxJavaPlugins.initIoScheduler(c));
        assertSame(s, RxJavaPlugins.initNewThreadScheduler(c));
        assertSame(s, RxJavaPlugins.initSingleScheduler(c));
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : ImmediateThinScheduler(io.reactivex.internal.schedulers.ImmediateThinScheduler) ConnectableFlowable(io.reactivex.flowables.ConnectableFlowable) Observable(io.reactivex.Observable) ConnectableObservable(io.reactivex.observables.ConnectableObservable) Observer(io.reactivex.Observer) ConnectableObservable(io.reactivex.observables.ConnectableObservable) ScalarSubscription(io.reactivex.internal.subscriptions.ScalarSubscription) ConnectableFlowable(io.reactivex.flowables.ConnectableFlowable) ParallelFlowable(io.reactivex.parallel.ParallelFlowable)

Example 3 with ConnectableFlowable

use of io.reactivex.flowables.ConnectableFlowable in project RxJava-Android-Samples by kaushikgopal.

the class RxBusDemo_Bottom3Fragment method onStart.

@Override
public void onStart() {
    super.onStart();
    _disposables = new CompositeDisposable();
    ConnectableFlowable<Object> tapEventEmitter = _rxBus.asFlowable().publish();
    // 
    _disposables.add(tapEventEmitter.subscribe(event -> {
        if (event instanceof RxBusDemoFragment.TapEvent) {
            _showTapText();
        }
    }));
    _disposables.add(tapEventEmitter.publish(stream -> stream.buffer(stream.debounce(1, TimeUnit.SECONDS))).observeOn(AndroidSchedulers.mainThread()).subscribe(taps -> {
        _showTapCount(taps.size());
    }));
    _disposables.add(tapEventEmitter.connect());
}
Also used : Bundle(android.os.Bundle) ButterKnife(butterknife.ButterKnife) LayoutInflater(android.view.LayoutInflater) ConnectableFlowable(io.reactivex.flowables.ConnectableFlowable) MainActivity(com.morihacky.android.rxjava.MainActivity) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) ViewGroup(android.view.ViewGroup) BindView(butterknife.BindView) R(com.morihacky.android.rxjava.R) TimeUnit(java.util.concurrent.TimeUnit) BaseFragment(com.morihacky.android.rxjava.fragments.BaseFragment) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) TextView(android.widget.TextView) View(android.view.View) ViewCompat(android.support.v4.view.ViewCompat) Nullable(android.support.annotation.Nullable) CompositeDisposable(io.reactivex.disposables.CompositeDisposable)

Example 4 with ConnectableFlowable

use of io.reactivex.flowables.ConnectableFlowable in project brave by openzipkin.

the class CurrentTraceContextAssemblyTrackingMatrixTest method connectableObservable_assembleInScope_subscribeNoScope.

// NOTE: we aren't doing separate tests for conditional ConnectableFlowable as there are no
// operations on the type that are conditional (ex filter)
@Test
public void connectableObservable_assembleInScope_subscribeNoScope() {
    ConnectableObservable<Integer> source, errorSource;
    try (Scope scope = currentTraceContext.newScope(assemblyContext)) {
        source = Observable.range(1, 3).doOnNext(e -> assertInAssemblyContext()).doOnComplete(this::assertInAssemblyContext).publish();
        errorSource = Observable.<Integer>error(new IllegalStateException()).doOnError(t -> assertInAssemblyContext()).doOnComplete(this::assertInAssemblyContext).publish();
    }
    subscribeInNoContext(source.autoConnect(), errorSource.autoConnect()).assertResult(1, 2, 3);
}
Also used : MaybeObserver(io.reactivex.MaybeObserver) ScalarCallable(io.reactivex.internal.fuseable.ScalarCallable) ObservableRange(io.reactivex.internal.operators.observable.ObservableRange) ParallelFilter(io.reactivex.internal.operators.parallel.ParallelFilter) FlowableFromCallable(io.reactivex.internal.operators.flowable.FlowableFromCallable) Completable(io.reactivex.Completable) FlowableFilter(io.reactivex.internal.operators.flowable.FlowableFilter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Maybe(io.reactivex.Maybe) ConnectableFlowable(io.reactivex.flowables.ConnectableFlowable) StrictCurrentTraceContext(brave.propagation.StrictCurrentTraceContext) Callable(java.util.concurrent.Callable) CurrentTraceContext(brave.propagation.CurrentTraceContext) SingleFromCallable(io.reactivex.internal.operators.single.SingleFromCallable) Single(io.reactivex.Single) SingleJust(io.reactivex.internal.operators.single.SingleJust) MaybeFromCallable(io.reactivex.internal.operators.maybe.MaybeFromCallable) ParallelFlowable(io.reactivex.parallel.ParallelFlowable) Flowable(io.reactivex.Flowable) CompletableFromCallable(io.reactivex.internal.operators.completable.CompletableFromCallable) After(org.junit.After) Observable(io.reactivex.Observable) Subscriber(org.reactivestreams.Subscriber) MaybeFilterSingle(io.reactivex.internal.operators.maybe.MaybeFilterSingle) Before(org.junit.Before) ObservablePublish(io.reactivex.internal.operators.observable.ObservablePublish) ConnectableObservable(io.reactivex.observables.ConnectableObservable) CompletableObserver(io.reactivex.CompletableObserver) ParallelFromPublisher(io.reactivex.internal.operators.parallel.ParallelFromPublisher) CompletableEmpty(io.reactivex.internal.operators.completable.CompletableEmpty) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test) RxJavaPlugins(io.reactivex.plugins.RxJavaPlugins) Predicate(io.reactivex.functions.Predicate) TraceContext(brave.propagation.TraceContext) MaybeFilter(io.reactivex.internal.operators.maybe.MaybeFilter) FlowablePublish(io.reactivex.internal.operators.flowable.FlowablePublish) ObservableFromCallable(io.reactivex.internal.operators.observable.ObservableFromCallable) SingleObserver(io.reactivex.SingleObserver) FlowableRange(io.reactivex.internal.operators.flowable.FlowableRange) MaybeJust(io.reactivex.internal.operators.maybe.MaybeJust) Observer(io.reactivex.Observer) Scope(brave.propagation.CurrentTraceContext.Scope) ObservableFilter(io.reactivex.internal.operators.observable.ObservableFilter) Scope(brave.propagation.CurrentTraceContext.Scope) Test(org.junit.Test)

Example 5 with ConnectableFlowable

use of io.reactivex.flowables.ConnectableFlowable in project incubator-gobblin by apache.

the class StreamModelTaskRunner method run.

protected void run() throws Exception {
    // Get the fork operator. By default IdentityForkOperator is used with a single branch.
    ForkOperator forkOperator = closer.register(this.taskContext.getForkOperator());
    RecordStreamWithMetadata<?, ?> stream = this.extractor.recordStream(this.shutdownRequested);
    ConnectableFlowable connectableStream = stream.getRecordStream().publish();
    stream = stream.withRecordStream(connectableStream);
    stream = stream.mapRecords(r -> {
        this.task.onRecordExtract();
        return r;
    });
    if (this.task.isStreamingTask()) {
        // Start watermark manager and tracker
        if (this.watermarkTracker.isPresent()) {
            this.watermarkTracker.get().start();
        }
        this.watermarkManager.get().start();
        ((StreamingExtractor) this.taskContext.getRawSourceExtractor()).start(this.watermarkStorage.get());
        stream = stream.mapRecords(r -> {
            AcknowledgableWatermark ackableWatermark = new AcknowledgableWatermark(r.getWatermark());
            if (watermarkTracker.isPresent()) {
                watermarkTracker.get().track(ackableWatermark);
            }
            r.addCallBack(ackableWatermark);
            return r;
        });
    }
    // Use the recordStreamProcessor list if it is configured. This list can contain both all RecordStreamProcessor types
    if (!this.recordStreamProcessors.isEmpty()) {
        for (RecordStreamProcessor streamProcessor : this.recordStreamProcessors) {
            stream = streamProcessor.processStream(stream, this.taskState);
        }
    } else {
        if (this.converter instanceof MultiConverter) {
            // if multiconverter, unpack it
            for (Converter cverter : ((MultiConverter) this.converter).getConverters()) {
                stream = cverter.processStream(stream, this.taskState);
            }
        } else {
            stream = this.converter.processStream(stream, this.taskState);
        }
    }
    stream = this.rowChecker.processStream(stream, this.taskState);
    Forker.ForkedStream<?, ?> forkedStreams = new Forker().forkStream(stream, forkOperator, this.taskState);
    boolean isForkAsync = !this.task.areSingleBranchTasksSynchronous(this.taskContext) || forkedStreams.getForkedStreams().size() > 1;
    int bufferSize = this.taskState.getPropAsInt(ConfigurationKeys.FORK_RECORD_QUEUE_CAPACITY_KEY, ConfigurationKeys.DEFAULT_FORK_RECORD_QUEUE_CAPACITY);
    for (int fidx = 0; fidx < forkedStreams.getForkedStreams().size(); fidx++) {
        RecordStreamWithMetadata<?, ?> forkedStream = forkedStreams.getForkedStreams().get(fidx);
        if (forkedStream != null) {
            if (isForkAsync) {
                forkedStream = forkedStream.mapStream(f -> f.observeOn(Schedulers.from(this.taskExecutor.getForkExecutor()), false, bufferSize));
            }
            Fork fork = new Fork(this.taskContext, forkedStream.getGlobalMetadata().getSchema(), forkedStreams.getForkedStreams().size(), fidx, this.taskMode);
            fork.consumeRecordStream(forkedStream);
            this.forks.put(Optional.of(fork), Optional.of(Futures.immediateFuture(null)));
            this.task.configureStreamingFork(fork, this.watermarkingStrategy);
        }
    }
    connectableStream.connect();
    if (!ExponentialBackoff.awaitCondition().callable(() -> this.forks.keySet().stream().map(Optional::get).allMatch(Fork::isDone)).initialDelay(1000L).maxDelay(1000L).maxWait(TimeUnit.MINUTES.toMillis(60)).await()) {
        throw new TimeoutException("Forks did not finish withing specified timeout.");
    }
}
Also used : StreamingExtractor(org.apache.gobblin.source.extractor.StreamingExtractor) WatermarkManager(org.apache.gobblin.writer.WatermarkManager) ForkOperator(org.apache.gobblin.fork.ForkOperator) ConnectableFlowable(io.reactivex.flowables.ConnectableFlowable) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AcknowledgableWatermark(org.apache.gobblin.writer.AcknowledgableWatermark) Fork(org.apache.gobblin.runtime.fork.Fork) Future(java.util.concurrent.Future) Closer(com.google.common.io.Closer) Optional(com.google.common.base.Optional) Map(java.util.Map) Schedulers(io.reactivex.schedulers.Schedulers) RecordStreamProcessor(org.apache.gobblin.records.RecordStreamProcessor) Forker(org.apache.gobblin.fork.Forker) WatermarkStorage(org.apache.gobblin.writer.WatermarkStorage) ExponentialBackoff(org.apache.gobblin.util.ExponentialBackoff) Converter(org.apache.gobblin.converter.Converter) ConfigurationKeys(org.apache.gobblin.configuration.ConfigurationKeys) TimeUnit(java.util.concurrent.TimeUnit) Extractor(org.apache.gobblin.source.extractor.Extractor) List(java.util.List) Futures(com.google.common.util.concurrent.Futures) FineGrainedWatermarkTracker(org.apache.gobblin.writer.FineGrainedWatermarkTracker) RowLevelPolicyChecker(org.apache.gobblin.qualitychecker.row.RowLevelPolicyChecker) RecordStreamWithMetadata(org.apache.gobblin.records.RecordStreamWithMetadata) AllArgsConstructor(lombok.AllArgsConstructor) Fork(org.apache.gobblin.runtime.fork.Fork) StreamingExtractor(org.apache.gobblin.source.extractor.StreamingExtractor) ConnectableFlowable(io.reactivex.flowables.ConnectableFlowable) Forker(org.apache.gobblin.fork.Forker) RecordStreamProcessor(org.apache.gobblin.records.RecordStreamProcessor) AcknowledgableWatermark(org.apache.gobblin.writer.AcknowledgableWatermark) ForkOperator(org.apache.gobblin.fork.ForkOperator) Converter(org.apache.gobblin.converter.Converter) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

ConnectableFlowable (io.reactivex.flowables.ConnectableFlowable)7 Observable (io.reactivex.Observable)4 Observer (io.reactivex.Observer)4 ConnectableObservable (io.reactivex.observables.ConnectableObservable)4 ParallelFlowable (io.reactivex.parallel.ParallelFlowable)4 CurrentTraceContext (brave.propagation.CurrentTraceContext)3 Scope (brave.propagation.CurrentTraceContext.Scope)3 StrictCurrentTraceContext (brave.propagation.StrictCurrentTraceContext)3 TraceContext (brave.propagation.TraceContext)3 Completable (io.reactivex.Completable)3 CompletableObserver (io.reactivex.CompletableObserver)3 Flowable (io.reactivex.Flowable)3 Maybe (io.reactivex.Maybe)3 MaybeObserver (io.reactivex.MaybeObserver)3 Single (io.reactivex.Single)3 SingleObserver (io.reactivex.SingleObserver)3 Predicate (io.reactivex.functions.Predicate)3 ScalarCallable (io.reactivex.internal.fuseable.ScalarCallable)3 CompletableEmpty (io.reactivex.internal.operators.completable.CompletableEmpty)3 CompletableFromCallable (io.reactivex.internal.operators.completable.CompletableFromCallable)3