Search in sources :

Example 1 with WorkQueueProcessor

use of reactor.core.publisher.WorkQueueProcessor 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)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ByteBuf (io.netty.buffer.ByteBuf)1 Unpooled (io.netty.buffer.Unpooled)1 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)1 JsonObjectDecoder (io.netty.handler.codec.json.JsonObjectDecoder)1 SslContext (io.netty.handler.ssl.SslContext)1 SslContextBuilder (io.netty.handler.ssl.SslContextBuilder)1 InsecureTrustManagerFactory (io.netty.handler.ssl.util.InsecureTrustManagerFactory)1 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)1 NetUtil (io.netty.util.NetUtil)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 URISyntaxException (java.net.URISyntaxException)1 ByteBuffer (java.nio.ByteBuffer)1 SocketChannel (java.nio.channels.SocketChannel)1 Charset (java.nio.charset.Charset)1 StandardCharsets (java.nio.charset.StandardCharsets)1 FileSystem (java.nio.file.FileSystem)1 FileSystems (java.nio.file.FileSystems)1