Search in sources :

Example 61 with Flowable

use of io.reactivex.Flowable in project RxCache by VictorAlbertos.

the class ProxyTranslator method getLoaderObservable.

private Observable getLoaderObservable(Method method, Object[] objectsMethod) {
    Observable<?> observable = getObjectFromMethodParam(method, Observable.class, objectsMethod);
    if (observable != null)
        return observable;
    Single single = getObjectFromMethodParam(method, Single.class, objectsMethod);
    if (single != null)
        return single.toObservable();
    Maybe maybe = getObjectFromMethodParam(method, Maybe.class, objectsMethod);
    if (maybe != null)
        return maybe.toObservable();
    Flowable flowable = getObjectFromMethodParam(method, Flowable.class, objectsMethod);
    if (flowable != null)
        return flowable.toObservable();
    String errorMessage = method.getName() + io.rx_cache2.internal.Locale.NOT_REACTIVE_TYPE_FOR_LOADER_WAS_FOUND;
    throw new IllegalArgumentException(errorMessage);
}
Also used : Maybe(io.reactivex.Maybe) Single(io.reactivex.Single) Flowable(io.reactivex.Flowable)

Example 62 with Flowable

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

the class RxBusDemo_Bottom2Fragment method onStart.

@Override
public void onStart() {
    super.onStart();
    _disposables = new CompositeDisposable();
    Flowable<Object> tapEventEmitter = _rxBus.asFlowable().share();
    _disposables.add(tapEventEmitter.subscribe(event -> {
        if (event instanceof RxBusDemoFragment.TapEvent) {
            _showTapText();
        }
    }));
    Flowable<Object> debouncedEmitter = tapEventEmitter.debounce(1, TimeUnit.SECONDS);
    Flowable<List<Object>> debouncedBufferEmitter = tapEventEmitter.buffer(debouncedEmitter);
    _disposables.add(debouncedBufferEmitter.observeOn(AndroidSchedulers.mainThread()).subscribe(taps -> {
        _showTapCount(taps.size());
    }));
}
Also used : Bundle(android.os.Bundle) ButterKnife(butterknife.ButterKnife) LayoutInflater(android.view.LayoutInflater) 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) List(java.util.List) TextView(android.widget.TextView) Flowable(io.reactivex.Flowable) View(android.view.View) ViewCompat(android.support.v4.view.ViewCompat) Nullable(android.support.annotation.Nullable) List(java.util.List) CompositeDisposable(io.reactivex.disposables.CompositeDisposable)

Example 63 with Flowable

use of io.reactivex.Flowable in project pentaho-kettle by pentaho.

the class FixedTimeStreamWindowTest method testSharedStreamingBatchPoolExecution.

@Test
public void testSharedStreamingBatchPoolExecution() throws Exception {
    /*
    * Tests that there is only 1 thread running inside the pool at all times.
    * */
    final List<String> errors = new ArrayList<String>();
    // Only 1 thread should be present in the pool at a given time.
    System.setProperty(Const.SHARED_STREAMING_BATCH_POOL_SIZE, "1");
    RowMetaInterface rowMeta = new RowMeta();
    rowMeta.addValueMeta(new ValueMetaString("field"));
    Result mockResult = new Result();
    mockResult.setRows(Arrays.asList(new RowMetaAndData(rowMeta, "queen"), new RowMetaAndData(rowMeta, "king")));
    FixedTimeStreamWindow<List> window1 = new FixedTimeStreamWindow<>(subtransExecutor, rowMeta, 0, 10, 10);
    FixedTimeStreamWindow<List> window2 = new FixedTimeStreamWindow<>(subtransExecutor, rowMeta, 0, 10, 10);
    Flowable flowable = Flowable.fromIterable(singletonList(asList("v1", "v2")));
    Field field = window1.getClass().getDeclaredField("sharedStreamingBatchPool");
    field.setAccessible(true);
    ThreadPoolExecutor sharedStreamingBatchPool = (ThreadPoolExecutor) field.get(window1);
    when(subtransExecutor.getPrefetchCount()).thenReturn(1000);
    when(subtransExecutor.execute(any())).thenAnswer((InvocationOnMock invocation) -> {
        // The active count should always be 1.
        if (sharedStreamingBatchPool.getActiveCount() != 1) {
            errors.add("Error: Active count should have been 1 at all times. Current value: " + sharedStreamingBatchPool.getActiveCount());
        }
        return Optional.of(mockResult);
    });
    Thread bufferThread1 = new Thread(new BufferThread(window1, flowable, mockResult));
    bufferThread1.start();
    Thread bufferThread2 = new Thread(new BufferThread(window2, flowable, mockResult));
    bufferThread2.start();
    Thread.sleep(10000);
    assertEquals(0, errors.size());
}
Also used : ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) RowMeta(org.pentaho.di.core.row.RowMeta) ArrayList(java.util.ArrayList) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) BaseMessages.getString(org.pentaho.di.i18n.BaseMessages.getString) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Result(org.pentaho.di.core.Result) Field(java.lang.reflect.Field) RowMetaAndData(org.pentaho.di.core.RowMetaAndData) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Flowable(io.reactivex.Flowable) Test(org.junit.Test)

Example 64 with Flowable

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

the class StreamModelTaskRunner method run.

protected void run() throws Exception {
    long maxWaitInMinute = taskState.getPropAsLong(ConfigurationKeys.FORK_MAX_WAIT_MININUTES, ConfigurationKeys.DEFAULT_FORK_MAX_WAIT_MININUTES);
    long initialDelay = taskState.getPropAsLong(ConfigurationKeys.FORK_FINISHED_CHECK_INTERVAL, ConfigurationKeys.DEFAULT_FORK_FINISHED_CHECK_INTERVAL);
    // 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);
    // This prevents emitting records until a connect() call is made on the connectable stream
    ConnectableFlowable connectableStream = stream.getRecordStream().publish();
    // The cancel is not propagated to the extractor's record generator when it has been turned into a hot Flowable
    // by publish, and in the case that extractor stuck in reading record when cancel get called,
    // we directly call shutdown to force it instead of setting the shutdownRequested flag on cancel to stop the extractor
    Flowable streamWithShutdownOnCancel = connectableStream.doOnCancel(this.extractor::shutdown);
    stream = stream.withRecordStream(streamWithShutdownOnCancel);
    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);
        }
    }
    Thread thread = new Thread(() -> connectableStream.connect());
    thread.setName(this.getClass().getSimpleName());
    // Log uncaught exceptions (e.g.OOMEs) to prevent threads from dying silently
    thread.setUncaughtExceptionHandler(new LoggingUncaughtExceptionHandler(Optional.absent()));
    thread.start();
    if (!ExponentialBackoff.awaitCondition().callable(() -> this.forks.keySet().stream().map(Optional::get).allMatch(Fork::isDone)).initialDelay(initialDelay).maxDelay(initialDelay).maxWait(TimeUnit.MINUTES.toMillis(maxWaitInMinute)).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) LoggingUncaughtExceptionHandler(org.apache.gobblin.util.LoggingUncaughtExceptionHandler) AcknowledgableWatermark(org.apache.gobblin.writer.AcknowledgableWatermark) Fork(org.apache.gobblin.runtime.fork.Fork) Future(java.util.concurrent.Future) Flowable(io.reactivex.Flowable) 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) LoggingUncaughtExceptionHandler(org.apache.gobblin.util.LoggingUncaughtExceptionHandler) ConnectableFlowable(io.reactivex.flowables.ConnectableFlowable) Flowable(io.reactivex.Flowable) TimeoutException(java.util.concurrent.TimeoutException)

Example 65 with Flowable

use of io.reactivex.Flowable in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    HttpClient client = vertx.createHttpClient();
    // Create two requests
    HttpClientRequest req1 = client.request(HttpMethod.GET, 8080, "localhost", "/");
    HttpClientRequest req2 = client.request(HttpMethod.GET, 8080, "localhost", "/");
    // Turn the requests responses into Flowable<JsonObject>
    Flowable<JsonObject> obs1 = req1.toFlowable().flatMap(HttpClientResponse::toFlowable).map(buf -> new JsonObject(buf.toString("UTF-8")));
    Flowable<JsonObject> obs2 = req2.toFlowable().flatMap(HttpClientResponse::toFlowable).map(buf -> new JsonObject(buf.toString("UTF-8")));
    // Combine the responses with the zip into a single response
    obs1.zipWith(obs2, (b1, b2) -> new JsonObject().put("req1", b1).put("req2", b2)).subscribe(json -> {
        System.out.println("Got combined result " + json);
    }, Throwable::printStackTrace);
    req1.end();
    req2.end();
}
Also used : HttpClientRequest(io.vertx.reactivex.core.http.HttpClientRequest) Flowable(io.reactivex.Flowable) HttpMethod(io.vertx.core.http.HttpMethod) JsonObject(io.vertx.core.json.JsonObject) HttpClient(io.vertx.reactivex.core.http.HttpClient) AbstractVerticle(io.vertx.reactivex.core.AbstractVerticle) Runner(io.vertx.example.util.Runner) HttpClientResponse(io.vertx.reactivex.core.http.HttpClientResponse) HttpClientRequest(io.vertx.reactivex.core.http.HttpClientRequest) HttpClient(io.vertx.reactivex.core.http.HttpClient) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

Flowable (io.reactivex.Flowable)65 Test (org.junit.Test)29 List (java.util.List)21 TimeUnit (java.util.concurrent.TimeUnit)19 Observable (io.reactivex.Observable)14 TestSubscriber (io.reactivex.subscribers.TestSubscriber)14 Assert.assertTrue (org.junit.Assert.assertTrue)14 JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)13 Single (io.reactivex.Single)13 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)13 ClientBuilder (javax.ws.rs.client.ClientBuilder)13 MediaType (javax.ws.rs.core.MediaType)13 AbstractResourceInfo (org.apache.cxf.jaxrs.model.AbstractResourceInfo)13 FlowableRxInvoker (org.apache.cxf.jaxrs.rx2.client.FlowableRxInvoker)13 FlowableRxInvokerProvider (org.apache.cxf.jaxrs.rx2.client.FlowableRxInvokerProvider)13 AbstractBusClientServerTestBase (org.apache.cxf.testutil.common.AbstractBusClientServerTestBase)13 BeforeClass (org.junit.BeforeClass)13 Collections (java.util.Collections)9 Response (javax.ws.rs.core.Response)9 Assert.assertEquals (org.junit.Assert.assertEquals)9