Search in sources :

Example 21 with Flowable

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

the class Scheduled method start.

@Override
public void start() throws Exception {
    Flowable<String> o = Flowable.just("someID1", "someID2", "someID3", "someID4");
    // This scheduler can execute blocking actions
    Scheduler scheduler = io.vertx.reactivex.core.RxHelper.blockingScheduler(vertx);
    // All operations done on the observer now can be blocking
    o = o.observeOn(scheduler);
    // Load from a blocking api
    o = o.map(id -> blockingLoad(id));
    o.subscribe(item -> {
        System.out.println("Got item " + item);
    }, err -> {
        err.printStackTrace();
    }, () -> {
        System.out.println("Done");
    });
}
Also used : Scheduler(io.reactivex.Scheduler) Flowable(io.reactivex.Flowable) AbstractVerticle(io.vertx.reactivex.core.AbstractVerticle) Runner(io.vertx.example.util.Runner) Scheduler(io.reactivex.Scheduler)

Example 22 with Flowable

use of io.reactivex.Flowable in project cyclops by aol.

the class RxTest method asyncFlowableList2.

@Test
public void asyncFlowableList2() {
    AtomicBoolean complete = new AtomicBoolean(false);
    Flowable<Integer> async = Flowable.fromPublisher(Spouts.reactive(Stream.of(100, 200, 300), Executors.newFixedThreadPool(1))).map(i -> {
        Thread.sleep(100);
        return i;
    }).doOnComplete(() -> complete.set(true));
    System.out.println("Initializing!");
    ListX<Integer> asyncList = FlowableCollections.listX(async).map(i -> i + 1);
    System.out.println("Blocked? " + complete.get());
    System.out.println("First value is " + asyncList.get(0));
    System.out.println("Blocked? " + complete.get());
}
Also used : ListX(cyclops.reactive.collections.mutable.ListX) Observables(cyclops.companion.rx2.Observables) ObservableAnyM(cyclops.monads.ObservableAnyM) ReactiveConvertableSequence(com.oath.cyclops.ReactiveConvertableSequence) FlowableReactiveSeq(cyclops.reactive.FlowableReactiveSeq) Spouts(cyclops.reactive.Spouts) AnyMSeq(com.oath.cyclops.anym.AnyMSeq) Flowables(cyclops.companion.rx2.Flowables) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) FlowableCollections(cyclops.reactive.FlowableCollections) Executors(java.util.concurrent.Executors) Assert.assertThat(org.junit.Assert.assertThat) ReactiveSeq(cyclops.reactive.ReactiveSeq) Stream(java.util.stream.Stream) ObservableReactiveSeq.reactiveSeq(cyclops.reactive.ObservableReactiveSeq.reactiveSeq) Rx2Witness.observable(cyclops.monads.Rx2Witness.observable) Flowable(io.reactivex.Flowable) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Observable(io.reactivex.Observable) ObservableReactiveSeq(cyclops.reactive.ObservableReactiveSeq) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 23 with Flowable

use of io.reactivex.Flowable in project cyclops by aol.

the class RxTest method asyncFlowableList.

@Test
public void asyncFlowableList() {
    AtomicBoolean complete = new AtomicBoolean(false);
    Flowable<Integer> async = Flowable.fromPublisher(Spouts.reactive(Stream.of(100, 200, 300), Executors.newFixedThreadPool(1))).map(i -> {
        Thread.sleep(5000);
        return i;
    }).doOnComplete(() -> complete.set(true));
    ListX<Integer> asyncList = ListX.listX(FlowableReactiveSeq.reactiveSeq(async)).map(i -> i + 1);
    System.out.println("Calling materialize!");
    asyncList.materialize();
    System.out.println("Blocked? " + complete.get());
    System.out.println("First value is " + asyncList.get(0));
    System.out.println("Blocked? " + complete.get());
}
Also used : ListX(cyclops.reactive.collections.mutable.ListX) Observables(cyclops.companion.rx2.Observables) ObservableAnyM(cyclops.monads.ObservableAnyM) ReactiveConvertableSequence(com.oath.cyclops.ReactiveConvertableSequence) FlowableReactiveSeq(cyclops.reactive.FlowableReactiveSeq) Spouts(cyclops.reactive.Spouts) AnyMSeq(com.oath.cyclops.anym.AnyMSeq) Flowables(cyclops.companion.rx2.Flowables) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) FlowableCollections(cyclops.reactive.FlowableCollections) Executors(java.util.concurrent.Executors) Assert.assertThat(org.junit.Assert.assertThat) ReactiveSeq(cyclops.reactive.ReactiveSeq) Stream(java.util.stream.Stream) ObservableReactiveSeq.reactiveSeq(cyclops.reactive.ObservableReactiveSeq.reactiveSeq) Rx2Witness.observable(cyclops.monads.Rx2Witness.observable) Flowable(io.reactivex.Flowable) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Observable(io.reactivex.Observable) ObservableReactiveSeq(cyclops.reactive.ObservableReactiveSeq) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 24 with Flowable

use of io.reactivex.Flowable in project jersey by jersey.

the class FlowableAgentResource method recommended.

private Flowable<List<Recommendation>> recommended(final Queue<String> errors) {
    destination.register(RxFlowableInvokerProvider.class);
    // Recommended places.
    final Flowable<Destination> recommended = destination.path("recommended").request().header("Rx-User", "RxJava2").rx(RxFlowableInvoker.class).get(new GenericType<List<Destination>>() {
    }).onErrorReturn(throwable -> {
        errors.offer("Recommended: " + throwable.getMessage());
        return Collections.emptyList();
    }).flatMap(Flowable::fromIterable).cache();
    forecast.register(RxFlowableInvokerProvider.class);
    // Forecasts. (depend on recommended destinations)
    final Flowable<Forecast> forecasts = recommended.flatMap(destination -> forecast.resolveTemplate("destination", destination.getDestination()).request().rx(RxFlowableInvoker.class).get(Forecast.class).onErrorReturn(throwable -> {
        errors.offer("Forecast: " + throwable.getMessage());
        return new Forecast(destination.getDestination(), "N/A");
    }));
    calculation.register(RxFlowableInvokerProvider.class);
    // Calculations. (depend on recommended destinations)
    final Flowable<Calculation> calculations = recommended.flatMap(destination -> {
        return calculation.resolveTemplate("from", "Moon").resolveTemplate("to", destination.getDestination()).request().rx(RxFlowableInvoker.class).get(Calculation.class).onErrorReturn(throwable -> {
            errors.offer("Calculation: " + throwable.getMessage());
            return new Calculation("Moon", destination.getDestination(), -1);
        });
    });
    return Flowable.zip(recommended, forecasts, calculations, Recommendation::new).buffer(Integer.MAX_VALUE);
}
Also used : Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) AsyncResponse(javax.ws.rs.container.AsyncResponse) Path(javax.ws.rs.Path) Singleton(javax.inject.Singleton) Suspended(javax.ws.rs.container.Suspended) Calculation(org.glassfish.jersey.examples.rx.domain.Calculation) Uri(org.glassfish.jersey.server.Uri) Destination(org.glassfish.jersey.examples.rx.domain.Destination) GenericType(javax.ws.rs.core.GenericType) Forecast(org.glassfish.jersey.examples.rx.domain.Forecast) List(java.util.List) Flowable(io.reactivex.Flowable) Recommendation(org.glassfish.jersey.examples.rx.domain.Recommendation) Schedulers(io.reactivex.schedulers.Schedulers) Queue(java.util.Queue) WebTarget(javax.ws.rs.client.WebTarget) RxFlowableInvoker(org.glassfish.jersey.client.rx.rxjava2.RxFlowableInvoker) RxFlowableInvokerProvider(org.glassfish.jersey.client.rx.rxjava2.RxFlowableInvokerProvider) Collections(java.util.Collections) AgentResponse(org.glassfish.jersey.examples.rx.domain.AgentResponse) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Destination(org.glassfish.jersey.examples.rx.domain.Destination) GenericType(javax.ws.rs.core.GenericType) Forecast(org.glassfish.jersey.examples.rx.domain.Forecast) RxFlowableInvoker(org.glassfish.jersey.client.rx.rxjava2.RxFlowableInvoker) Calculation(org.glassfish.jersey.examples.rx.domain.Calculation)

Example 25 with Flowable

use of io.reactivex.Flowable in project jersey by jersey.

the class FlowableAgentResource method flowable.

@GET
public void flowable(@Suspended final AsyncResponse async) {
    final long time = System.nanoTime();
    final Queue<String> errors = new ConcurrentLinkedQueue<>();
    Flowable.just(new AgentResponse()).zipWith(visited(errors), (agentResponse, visited) -> {
        agentResponse.setVisited(visited);
        return agentResponse;
    }).zipWith(recommended(errors), (agentResponse, recommendations) -> {
        agentResponse.setRecommended(recommendations);
        return agentResponse;
    }).observeOn(Schedulers.io()).subscribe(agentResponse -> {
        agentResponse.setProcessingTime((System.nanoTime() - time) / 1000000);
        async.resume(agentResponse);
    }, async::resume);
}
Also used : Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) AsyncResponse(javax.ws.rs.container.AsyncResponse) Path(javax.ws.rs.Path) Singleton(javax.inject.Singleton) Suspended(javax.ws.rs.container.Suspended) Calculation(org.glassfish.jersey.examples.rx.domain.Calculation) Uri(org.glassfish.jersey.server.Uri) Destination(org.glassfish.jersey.examples.rx.domain.Destination) GenericType(javax.ws.rs.core.GenericType) Forecast(org.glassfish.jersey.examples.rx.domain.Forecast) List(java.util.List) Flowable(io.reactivex.Flowable) Recommendation(org.glassfish.jersey.examples.rx.domain.Recommendation) Schedulers(io.reactivex.schedulers.Schedulers) Queue(java.util.Queue) WebTarget(javax.ws.rs.client.WebTarget) RxFlowableInvoker(org.glassfish.jersey.client.rx.rxjava2.RxFlowableInvoker) RxFlowableInvokerProvider(org.glassfish.jersey.client.rx.rxjava2.RxFlowableInvokerProvider) Collections(java.util.Collections) AgentResponse(org.glassfish.jersey.examples.rx.domain.AgentResponse) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) AgentResponse(org.glassfish.jersey.examples.rx.domain.AgentResponse) GET(javax.ws.rs.GET)

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