Search in sources :

Example 11 with Publisher

use of org.reactivestreams.Publisher in project RxDownload by ssseasonnn.

the class DownloadType method startDownload.

public Observable<DownloadStatus> startDownload() {
    return Flowable.just(1).doOnSubscribe(new Consumer<Subscription>() {

        @Override
        public void accept(Subscription subscription) throws Exception {
            log(startLog());
            record.start();
        }
    }).flatMap(new Function<Integer, Publisher<DownloadStatus>>() {

        @Override
        public Publisher<DownloadStatus> apply(Integer integer) throws Exception {
            return download();
        }
    }).doOnNext(new Consumer<DownloadStatus>() {

        @Override
        public void accept(DownloadStatus status) throws Exception {
            record.update(status);
        }
    }).doOnError(new Consumer<Throwable>() {

        @Override
        public void accept(Throwable throwable) throws Exception {
            log(errorLog());
            record.error();
        }
    }).doOnComplete(new Action() {

        @Override
        public void run() throws Exception {
            log(completeLog());
            record.complete();
        }
    }).doOnCancel(new Action() {

        @Override
        public void run() throws Exception {
            log(cancelLog());
            record.cancel();
        }
    }).doFinally(new Action() {

        @Override
        public void run() throws Exception {
            log(finishLog());
            record.finish();
        }
    }).toObservable();
}
Also used : Action(io.reactivex.functions.Action) Consumer(io.reactivex.functions.Consumer) Publisher(org.reactivestreams.Publisher) Subscription(org.reactivestreams.Subscription) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 12 with Publisher

use of org.reactivestreams.Publisher in project RxJava by ReactiveX.

the class TransformerTest method flowableTransformerThrows.

@Test
public void flowableTransformerThrows() {
    try {
        Flowable.just(1).compose(new FlowableTransformer<Integer, Integer>() {

            @Override
            public Publisher<Integer> apply(Flowable<Integer> v) {
                throw new TestException("Forced failure");
            }
        });
        fail("Should have thrown!");
    } catch (TestException ex) {
        assertEquals("Forced failure", ex.getMessage());
    }
}
Also used : TestException(io.reactivex.exceptions.TestException) Publisher(org.reactivestreams.Publisher) Test(org.junit.Test)

Example 13 with Publisher

use of org.reactivestreams.Publisher in project ratpack by ratpack.

the class ServerSentEvents method render.

/**
   * {@inheritDoc}
   */
@Override
public void render(Context context) throws Exception {
    ByteBufAllocator bufferAllocator = context.get(ByteBufAllocator.class);
    Response response = context.getResponse();
    response.getHeaders().add(HttpHeaderConstants.CONTENT_TYPE, HttpHeaderConstants.TEXT_EVENT_STREAM_CHARSET_UTF_8);
    response.getHeaders().add(HttpHeaderConstants.TRANSFER_ENCODING, HttpHeaderConstants.CHUNKED);
    response.getHeaders().add(HttpHeaderConstants.CACHE_CONTROL, HttpHeaderConstants.NO_CACHE_FULL);
    response.getHeaders().add(HttpHeaderConstants.PRAGMA, HttpHeaderConstants.NO_CACHE);
    response.sendStream(Streams.map(publisher, i -> ServerSentEventEncoder.INSTANCE.encode(i, bufferAllocator)));
}
Also used : Response(ratpack.http.Response) Response(ratpack.http.Response) Context(ratpack.handling.Context) ServerSentEventEncoder(ratpack.sse.internal.ServerSentEventEncoder) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) Action(ratpack.func.Action) Publisher(org.reactivestreams.Publisher) Renderable(ratpack.render.Renderable) Streams(ratpack.stream.Streams) HttpHeaderConstants(ratpack.http.internal.HttpHeaderConstants) DefaultEvent(ratpack.sse.internal.DefaultEvent) ByteBufAllocator(io.netty.buffer.ByteBufAllocator)

Example 14 with Publisher

use of org.reactivestreams.Publisher in project ratpack by ratpack.

the class WebSockets method websocketBroadcast.

/**
   * Sets up a websocket that sends the published Strings to a client.
   * <p>
   * This takes the place of a {@link Streams#bindExec(Publisher)} call.
   *
   * @param context the request handling context
   * @param broadcaster a {@link Publisher} of Strings to send to the websocket client
   */
public static void websocketBroadcast(final Context context, final Publisher<String> broadcaster) {
    ByteBufAllocator bufferAllocator = context.get(ByteBufAllocator.class);
    websocketByteBufBroadcast(context, Streams.map(broadcaster, s -> ByteBufUtil.encodeString(bufferAllocator, CharBuffer.wrap(s), CharsetUtil.UTF_8)));
}
Also used : Function(ratpack.func.Function) Context(ratpack.handling.Context) CharBuffer(java.nio.CharBuffer) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) Publisher(org.reactivestreams.Publisher) DefaultWebSocketConnector(ratpack.websocket.internal.DefaultWebSocketConnector) WebsocketBroadcastSubscriber(ratpack.websocket.internal.WebsocketBroadcastSubscriber) ByteBufUtil(io.netty.buffer.ByteBufUtil) ByteBuf(io.netty.buffer.ByteBuf) ServerConfig(ratpack.server.ServerConfig) WebSocketEngine(ratpack.websocket.internal.WebSocketEngine) CharsetUtil(io.netty.util.CharsetUtil) Streams(ratpack.stream.Streams) ByteBufAllocator(io.netty.buffer.ByteBufAllocator)

Example 15 with Publisher

use of org.reactivestreams.Publisher in project ratpack by ratpack.

the class ConcatPublisher method subscribe.

@Override
public void subscribe(Subscriber<? super T> s) {
    s.onSubscribe(new ManagedSubscription<T>(s, disposer) {

        Iterator<? extends Publisher<? extends T>> iterator = publishers.iterator();

        Subscription current;

        @Override
        protected void onRequest(long n) {
            if (current == null) {
                if (iterator.hasNext()) {
                    Publisher<? extends T> publisher = iterator.next();
                    publisher.subscribe(new Subscriber<T>() {

                        @Override
                        public void onSubscribe(Subscription s) {
                            current = s;
                            s.request(n);
                        }

                        @Override
                        public void onNext(T t) {
                            emitNext(t);
                        }

                        @Override
                        public void onError(Throwable t) {
                            emitError(t);
                        }

                        @Override
                        public void onComplete() {
                            current = null;
                            long demand = getDemand();
                            if (demand > 0) {
                                onRequest(demand);
                            }
                        }
                    });
                } else {
                    emitComplete();
                }
            } else {
                current.request(n);
            }
        }

        @Override
        protected void onCancel() {
            if (current != null) {
                current.cancel();
            }
        }
    });
}
Also used : Subscriber(org.reactivestreams.Subscriber) Publisher(org.reactivestreams.Publisher) TransformablePublisher(ratpack.stream.TransformablePublisher) Subscription(org.reactivestreams.Subscription)

Aggregations

Publisher (org.reactivestreams.Publisher)28 List (java.util.List)11 Mono (reactor.core.publisher.Mono)11 Test (org.junit.Test)9 Collections (java.util.Collections)8 Assert.assertSame (org.junit.Assert.assertSame)7 CharSequenceEncoder (org.springframework.core.codec.CharSequenceEncoder)7 HttpStatus (org.springframework.http.HttpStatus)7 EncoderHttpMessageWriter (org.springframework.http.codec.EncoderHttpMessageWriter)7 MockServerHttpRequest (org.springframework.mock.http.server.reactive.test.MockServerHttpRequest)7 Duration (java.time.Duration)6 CoreMatchers.instanceOf (org.hamcrest.CoreMatchers.instanceOf)6 CoreMatchers.startsWith (org.hamcrest.CoreMatchers.startsWith)6 Matchers.is (org.hamcrest.Matchers.is)6 Assert.assertEquals (org.junit.Assert.assertEquals)6 Assert.assertThat (org.junit.Assert.assertThat)6 Before (org.junit.Before)6 Subscriber (org.reactivestreams.Subscriber)6 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)6 Bean (org.springframework.context.annotation.Bean)6