Search in sources :

Example 6 with EmitterProcessor

use of reactor.core.publisher.EmitterProcessor in project reactor-netty by reactor.

the class HttpTests method streamAndPoolExplicitCompression.

@Test
public void streamAndPoolExplicitCompression() throws Exception {
    EmitterProcessor<String> ep = EmitterProcessor.create();
    NettyContext server = HttpServer.create(opts -> opts.port(0)).newRouter(r -> r.post("/hi", (req, res) -> req.receive().aggregate().asString().log().then(res.sendString(Flux.just("test")).then())).get("/stream", (req, res) -> req.receive().then(res.compression(true).options(op -> op.flushOnEach()).sendString(ep.log()).then()))).block(Duration.ofSeconds(30));
    String content = HttpClient.create(opts -> opts.port(server.address().getPort()).compression(true)).post("/hi", req -> req.sendString(Flux.just("1", "2", "3", "4", "5"))).flatMap(res -> res.receive().aggregate().asString().log()).block();
    Flux<String> f = HttpClient.create(opts -> opts.port(server.address().getPort()).compression(true)).get("/stream").flatMapMany(res -> res.receive().asString());
    System.out.println(content);
    StepVerifier.create(f).then(() -> ep.onNext("test1")).expectNext("test1").thenAwait(Duration.ofMillis(300)).then(() -> ep.onNext("test2")).thenAwait(Duration.ofMillis(300)).expectNext("test2").thenAwait(Duration.ofMillis(300)).then(() -> ep.onComplete()).verifyComplete();
    content = HttpClient.create(opts -> opts.port(server.address().getPort()).compression(true)).post("/hi", req -> req.sendString(Flux.just("1", "2", "3", "4", "5"))).flatMap(res -> res.receive().aggregate().asString().log()).block();
    server.dispose();
}
Also used : StepVerifier(reactor.test.StepVerifier) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Unpooled(io.netty.buffer.Unpooled) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) List(java.util.List) ByteBuf(io.netty.buffer.ByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) EmitterProcessor(reactor.core.publisher.EmitterProcessor) NettyContext(reactor.ipc.netty.NettyContext) HttpClient(reactor.ipc.netty.http.client.HttpClient) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) HttpClientException(reactor.ipc.netty.http.client.HttpClientException) HttpServer(reactor.ipc.netty.http.server.HttpServer) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 7 with EmitterProcessor

use of reactor.core.publisher.EmitterProcessor in project reactor-netty by reactor.

the class TcpServerTests method test5.

@Test
@Ignore
public void test5() throws Exception {
    // Hot stream of data, could be injected from anywhere
    EmitterProcessor<String> broadcaster = EmitterProcessor.create();
    // Get a reference to the tail of the operation pipeline (microbatching + partitioning)
    final Processor<List<String>, List<String>> processor = WorkQueueProcessor.<List<String>>builder().autoCancel(false).build();
    broadcaster.bufferTimeout(10, Duration.ofSeconds(1)).log("broadcaster").subscribe(processor);
    // on a server dispatching data on the default shared dispatcher, and serializing/deserializing as string
    // Listen for anything exactly hitting the root URI and route the incoming connection request to the callback
    NettyContext s = HttpServer.create(0).newRouter(r -> r.get("/", (request, response) -> {
        // prepare a response header to be appended first before any reply
        response.addHeader("X-CUSTOM", "12345");
        // returning a stream of String from each microbatch merged
        return response.sendString(Flux.from(processor).flatMap(Flux::fromIterable).take(Duration.ofSeconds(5)).concatWith(Flux.just("end\n")));
    })).block(Duration.ofSeconds(30));
    for (int i = 0; i < 50; i++) {
        Thread.sleep(500);
        broadcaster.onNext(System.currentTimeMillis() + "\n");
    }
    s.dispose();
}
Also used : URISyntaxException(java.net.URISyntaxException) BiFunction(java.util.function.BiFunction) Processor(org.reactivestreams.Processor) NettyOutbound(reactor.ipc.netty.NettyOutbound) ByteBuffer(java.nio.ByteBuffer) Unpooled(io.netty.buffer.Unpooled) Loggers(reactor.util.Loggers) SocketChannel(java.nio.channels.SocketChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) After(org.junit.After) Logger(reactor.util.Logger) Assertions(org.assertj.core.api.Assertions) SocketUtils(reactor.ipc.netty.SocketUtils) Path(java.nio.file.Path) JsonObjectDecoder(io.netty.handler.codec.json.JsonObjectDecoder) FileSystem(java.nio.file.FileSystem) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) SSLException(javax.net.ssl.SSLException) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) Exceptions(reactor.core.Exceptions) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) StandardCopyOption(java.nio.file.StandardCopyOption) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) WorkQueueProcessor(reactor.core.publisher.WorkQueueProcessor) Charset(java.nio.charset.Charset) ByteBuf(io.netty.buffer.ByteBuf) NettyInbound(reactor.ipc.netty.NettyInbound) NettyPipeline(reactor.ipc.netty.NettyPipeline) EmitterProcessor(reactor.core.publisher.EmitterProcessor) HttpClient(reactor.ipc.netty.http.client.HttpClient) Schedulers(reactor.core.scheduler.Schedulers) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) HttpServer(reactor.ipc.netty.http.server.HttpServer) SslContext(io.netty.handler.ssl.SslContext) Files(java.nio.file.Files) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) Assert.assertNotNull(org.junit.Assert.assertNotNull) Publisher(org.reactivestreams.Publisher) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) MonoProcessor(reactor.core.publisher.MonoProcessor) NetUtil(io.netty.util.NetUtil) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) CertificateException(java.security.cert.CertificateException) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Flux(reactor.core.publisher.Flux) Ignore(org.junit.Ignore) Paths(java.nio.file.Paths) NettyContext(reactor.ipc.netty.NettyContext) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) FileSystems(java.nio.file.FileSystems) List(java.util.List) NettyContext(reactor.ipc.netty.NettyContext) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 8 with EmitterProcessor

use of reactor.core.publisher.EmitterProcessor in project reactor-netty by reactor.

the class HttpTests method streamAndPoolDefaultCompression.

@Test
public void streamAndPoolDefaultCompression() throws Exception {
    EmitterProcessor<String> ep = EmitterProcessor.create();
    NettyContext server = HttpServer.create(opts -> opts.port(0).compression(true)).newRouter(r -> r.post("/hi", (req, res) -> req.receive().aggregate().asString().log().then(res.compression(false).sendString(Flux.just("test")).then())).get("/stream", (req, res) -> req.receive().then(res.options(op -> op.flushOnEach()).sendString(ep.log()).then()))).block(Duration.ofSeconds(30));
    String content = HttpClient.create(opts -> opts.port(server.address().getPort()).compression(true)).post("/hi", req -> req.sendString(Flux.just("1", "2", "3", "4", "5"))).flatMap(res -> res.receive().aggregate().asString().log()).block();
    Flux<String> f = HttpClient.create(opts -> opts.port(server.address().getPort()).compression(true)).get("/stream").flatMapMany(res -> res.receive().asString());
    System.out.println(content);
    StepVerifier.create(f).then(() -> ep.onNext("test1")).expectNext("test1").thenAwait(Duration.ofMillis(300)).then(() -> ep.onNext("test2")).thenAwait(Duration.ofMillis(300)).expectNext("test2").thenAwait(Duration.ofMillis(300)).then(() -> ep.onComplete()).verifyComplete();
    content = HttpClient.create(opts -> opts.port(server.address().getPort()).compression(true)).post("/hi", req -> req.sendString(Flux.just("1", "2", "3", "4", "5"))).flatMap(res -> res.receive().aggregate().asString().log()).block();
    server.dispose();
}
Also used : StepVerifier(reactor.test.StepVerifier) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Unpooled(io.netty.buffer.Unpooled) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) List(java.util.List) ByteBuf(io.netty.buffer.ByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) EmitterProcessor(reactor.core.publisher.EmitterProcessor) NettyContext(reactor.ipc.netty.NettyContext) HttpClient(reactor.ipc.netty.http.client.HttpClient) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) HttpClientException(reactor.ipc.netty.http.client.HttpClientException) HttpServer(reactor.ipc.netty.http.server.HttpServer) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 9 with EmitterProcessor

use of reactor.core.publisher.EmitterProcessor in project reactor-core by reactor.

the class FluxSpecTests method whenFilterFunctionThrowsFilteredComposableAcceptsError.

@Test
public void whenFilterFunctionThrowsFilteredComposableAcceptsError() {
    // "When a filter function throws an exception, the filtered composable accepts the error"
    // given: "a source composable with a filter function that throws an error"
    EmitterProcessor<Integer> source = EmitterProcessor.create();
    Flux<Integer> filtered = source.filter(it -> {
        if (it == 1) {
            throw new RuntimeException();
        } else {
            return true;
        }
    });
    LongAdder errors = new LongAdder();
    filtered.doOnError(e -> errors.increment()).subscribe();
    // when: "the source accepts a value"
    source.onNext(1);
    // then: "the error is passed on"
    assertThat(errors.intValue()).isEqualTo(1);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LongAdder(java.util.concurrent.atomic.LongAdder) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) BiFunction(java.util.function.BiFunction) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FluxProcessor(reactor.core.publisher.FluxProcessor) Scheduler(reactor.core.scheduler.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) EmitterProcessor(reactor.core.publisher.EmitterProcessor) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Schedulers(reactor.core.scheduler.Schedulers) Iterator(java.util.Iterator) MonoProcessor(reactor.core.publisher.MonoProcessor) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) ReplayProcessor(reactor.core.publisher.ReplayProcessor) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) List(java.util.List) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Assert(org.junit.Assert) Collections(java.util.Collections) LongAdder(java.util.concurrent.atomic.LongAdder) Test(org.junit.Test)

Example 10 with EmitterProcessor

use of reactor.core.publisher.EmitterProcessor in project reactor-core by reactor.

the class FluxSpecTests method whenMappingFunctionThrowsMappedComposableAcceptsError.

@Test
public void whenMappingFunctionThrowsMappedComposableAcceptsError() {
    // "When a mapping function throws an exception, the mapped composable accepts the error"
    // given: "a source composable with a mapping function that throws an error"
    EmitterProcessor<Integer> source = EmitterProcessor.create();
    Flux<String> mapped = source.map(it -> {
        if (it == 1) {
            throw new RuntimeException();
        } else {
            return "na";
        }
    });
    LongAdder errors = new LongAdder();
    mapped.doOnError(e -> errors.increment()).subscribe();
    // when: "the source accepts a value"
    source.onNext(1);
    // then: "the error is passed on"
    assertThat(errors.intValue()).isEqualTo(1);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LongAdder(java.util.concurrent.atomic.LongAdder) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) BiFunction(java.util.function.BiFunction) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FluxProcessor(reactor.core.publisher.FluxProcessor) Scheduler(reactor.core.scheduler.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) EmitterProcessor(reactor.core.publisher.EmitterProcessor) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Schedulers(reactor.core.scheduler.Schedulers) Iterator(java.util.Iterator) MonoProcessor(reactor.core.publisher.MonoProcessor) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) ReplayProcessor(reactor.core.publisher.ReplayProcessor) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) List(java.util.List) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Assert(org.junit.Assert) Collections(java.util.Collections) LongAdder(java.util.concurrent.atomic.LongAdder) Test(org.junit.Test)

Aggregations

Duration (java.time.Duration)13 List (java.util.List)13 CountDownLatch (java.util.concurrent.CountDownLatch)13 TimeUnit (java.util.concurrent.TimeUnit)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 Test (org.junit.Test)13 EmitterProcessor (reactor.core.publisher.EmitterProcessor)13 Flux (reactor.core.publisher.Flux)13 Mono (reactor.core.publisher.Mono)13 Schedulers (reactor.core.scheduler.Schedulers)13 StepVerifier (reactor.test.StepVerifier)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)11 MonoProcessor (reactor.core.publisher.MonoProcessor)11 ArrayList (java.util.ArrayList)10 Arrays (java.util.Arrays)10 Collections (java.util.Collections)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 Consumer (java.util.function.Consumer)10 Assert (org.junit.Assert)10 FluxProcessor (reactor.core.publisher.FluxProcessor)10