Search in sources :

Example 1 with Processor

use of org.reactivestreams.Processor in project reactor-core by reactor.

the class EmitterProcessorTest method testThreadAffinity.

@Test
public void testThreadAffinity() throws InterruptedException {
    int count = 10;
    Scheduler[] schedulers = new Scheduler[4];
    CountDownLatch[] latches = new CountDownLatch[schedulers.length];
    for (int i = 0; i < schedulers.length; i++) {
        schedulers[i] = Schedulers.newSingle("scheduler" + i + '-');
        int expectedCount = i == 1 ? count * 2 : count;
        latches[i] = new CountDownLatch(expectedCount);
    }
    EmitterProcessor<Integer> processor = EmitterProcessor.create();
    processor.publishOn(schedulers[0]).share();
    processor.publishOn(schedulers[1]).subscribe(i -> {
        assertThat(Thread.currentThread().getName().contains("scheduler1")).isTrue();
        latches[1].countDown();
    });
    for (int i = 0; i < count; i++) processor.onNext(i);
    processor.publishOn(schedulers[2]).map(i -> {
        assertThat(Thread.currentThread().getName().contains("scheduler2")).isTrue();
        latches[2].countDown();
        return i;
    }).publishOn(schedulers[3]).doOnNext(i -> {
        assertThat(Thread.currentThread().getName().contains("scheduler3")).isTrue();
        latches[3].countDown();
    }).subscribe();
    for (int i = 0; i < count; i++) processor.onNext(i);
    processor.onComplete();
    for (int i = 1; i < latches.length; i++) assertThat(latches[i].await(5, TimeUnit.SECONDS)).isTrue();
    assertThat(latches[0].getCount()).isEqualTo(count);
}
Also used : Disposable(reactor.core.Disposable) StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) Processor(org.reactivestreams.Processor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Scheduler(reactor.core.scheduler.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) Nullable(reactor.util.annotation.Nullable) ArrayList(java.util.ArrayList) Queues(reactor.util.concurrent.Queues) CoreSubscriber(reactor.core.CoreSubscriber) Attr(reactor.core.Scannable.Attr) CANCELLED(reactor.core.Scannable.Attr.CANCELLED) Schedulers(reactor.core.scheduler.Schedulers) Subscriber(org.reactivestreams.Subscriber) CyclicBarrier(java.util.concurrent.CyclicBarrier) Publisher(org.reactivestreams.Publisher) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) LockSupport(java.util.concurrent.locks.LockSupport) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Stream(java.util.stream.Stream) Ignore(org.junit.Ignore) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) TERMINATED(reactor.core.Scannable.Attr.TERMINATED) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) Scheduler(reactor.core.scheduler.Scheduler) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with Processor

use of org.reactivestreams.Processor 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

List (java.util.List)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 TimeUnit (java.util.concurrent.TimeUnit)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 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