Search in sources :

Example 1 with DefaultEventExecutor

use of io.netty.util.concurrent.DefaultEventExecutor in project moleculer-java by moleculer-java.

the class RedisPubSubClient method connect.

// --- CONNECT ---
public final void connect() {
    DefaultClientResources.Builder builder = DefaultClientResources.builder();
    acceptor = Executors.newSingleThreadExecutor();
    group = new NioEventLoopGroup(1, acceptor);
    builder.eventLoopGroupProvider(new EventLoopGroupProvider() {

        @Override
        public final int threadPoolSize() {
            return 1;
        }

        @Override
        public final Future<Boolean> shutdown(long quietPeriod, long timeout, TimeUnit timeUnit) {
            return null;
        }

        @Override
        public final Future<Boolean> release(EventExecutorGroup eventLoopGroup, long quietPeriod, long timeout, TimeUnit unit) {
            return null;
        }

        @SuppressWarnings("unchecked")
        @Override
        public final <T extends EventLoopGroup> T allocate(Class<T> type) {
            return (T) group;
        }
    });
    builder.eventExecutorGroup(new DefaultEventExecutor(executor));
    if (eventBus == null) {
        builder.eventBus(new EventBus() {

            @Override
            public final void publish(Event event) {
            }

            @Override
            public final Observable<Event> get() {
                return null;
            }
        });
    } else {
        builder.eventBus(eventBus);
    }
    resources = builder.build();
    List<RedisURI> redisURIs = parseURLs(urls, password, secure);
    StatefulRedisPubSubConnection<byte[], byte[]> connection;
    ByteArrayCodec codec = new ByteArrayCodec();
    if (urls.length > 1) {
        // Clustered client
        connection = RedisClusterClient.create(resources, redisURIs).connectPubSub(codec);
    } else {
        // Single connection
        connection = RedisClient.create(resources, redisURIs.get(0)).connectPubSub(codec);
    }
    // Add listener
    if (listener != null) {
        connection.addListener(listener);
    }
    commands = connection.async();
}
Also used : EventExecutorGroup(io.netty.util.concurrent.EventExecutorGroup) DefaultEventExecutor(io.netty.util.concurrent.DefaultEventExecutor) RedisURI(com.lambdaworks.redis.RedisURI) EventBus(com.lambdaworks.redis.event.EventBus) DefaultClientResources(com.lambdaworks.redis.resource.DefaultClientResources) Observable(rx.Observable) ByteArrayCodec(com.lambdaworks.redis.codec.ByteArrayCodec) EventLoopGroupProvider(com.lambdaworks.redis.resource.EventLoopGroupProvider) TimeUnit(java.util.concurrent.TimeUnit) Future(io.netty.util.concurrent.Future) Event(com.lambdaworks.redis.event.Event) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 2 with DefaultEventExecutor

use of io.netty.util.concurrent.DefaultEventExecutor in project netty by netty.

the class BurstCostExecutorsBenchmark method setup.

@Setup
public void setup() {
    ExecutorType type = ExecutorType.valueOf(executorType);
    switch(type) {
        case spinning:
            // The case with 3 producers can have a peak of 3*burstLength offers:
            // 4 is to leave some room between the offers and 1024 is to leave some room
            // between producer/consumer when work is > 0 and 1 producer.
            // If work = 0 then the task queue is supposed to be near empty most of the time.
            executor = new SpinExecutorService(Math.min(1024, burstLength * 4));
            executorToShutdown = executor;
            break;
        case defaultEventExecutor:
            executor = new DefaultEventExecutor();
            executorToShutdown = executor;
            break;
        case juc:
            executor = Executors.newSingleThreadScheduledExecutor();
            executorToShutdown = executor;
            break;
        case nioEventLoop:
            NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(1);
            nioEventLoopGroup.setIoRatio(1);
            executor = nioEventLoopGroup.next();
            executorToShutdown = nioEventLoopGroup;
            break;
        case epollEventLoop:
            Epoll.ensureAvailability();
            EpollEventLoopGroup epollEventLoopGroup = new EpollEventLoopGroup(1);
            epollEventLoopGroup.setIoRatio(1);
            executor = epollEventLoopGroup.next();
            executorToShutdown = epollEventLoopGroup;
            break;
        case kqueueEventLoop:
            KQueue.ensureAvailability();
            KQueueEventLoopGroup kQueueEventLoopGroup = new KQueueEventLoopGroup(1);
            kQueueEventLoopGroup.setIoRatio(1);
            executor = kQueueEventLoopGroup.next();
            executorToShutdown = kQueueEventLoopGroup;
            break;
    }
}
Also used : DefaultEventExecutor(io.netty.util.concurrent.DefaultEventExecutor) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) KQueueEventLoopGroup(io.netty.channel.kqueue.KQueueEventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Setup(org.openjdk.jmh.annotations.Setup)

Example 3 with DefaultEventExecutor

use of io.netty.util.concurrent.DefaultEventExecutor in project reactor-netty by reactor.

the class HttpServerTests method testGracefulShutdown.

@Test
void testGracefulShutdown() throws Exception {
    CountDownLatch latch1 = new CountDownLatch(2);
    CountDownLatch latch2 = new CountDownLatch(2);
    CountDownLatch latch3 = new CountDownLatch(1);
    LoopResources loop = LoopResources.create("testGracefulShutdown");
    disposableServer = createServer().runOn(loop).doOnConnection(c -> {
        c.onDispose().subscribe(null, null, latch2::countDown);
        latch1.countDown();
    }).channelGroup(new DefaultChannelGroup(new DefaultEventExecutor())).route(r -> r.get("/delay500", (req, res) -> res.sendString(Mono.just("delay500").delayElement(Duration.ofMillis(500)))).get("/delay1000", (req, res) -> res.sendString(Mono.just("delay1000").delayElement(Duration.ofSeconds(1))))).bindNow(Duration.ofSeconds(30));
    HttpClient client = createClient(disposableServer::address);
    AtomicReference<String> result = new AtomicReference<>();
    Flux.just("/delay500", "/delay1000").flatMap(s -> client.get().uri(s).responseContent().aggregate().asString()).collect(Collectors.joining()).subscribe(s -> {
        result.set(s);
        latch3.countDown();
    });
    assertThat(latch1.await(30, TimeUnit.SECONDS)).isTrue();
    // Stop accepting incoming requests, wait at most 3s for the active requests to finish
    disposableServer.disposeNow();
    assertThat(latch2.await(30, TimeUnit.SECONDS)).isTrue();
    // Dispose the event loop
    loop.disposeLater().block(Duration.ofSeconds(30));
    assertThat(latch3.await(30, TimeUnit.SECONDS)).isTrue();
    assertThat(result.get()).isNotNull().isEqualTo("delay500delay1000");
}
Also used : DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) DefaultEventExecutor(io.netty.util.concurrent.DefaultEventExecutor) AttributeKey(io.netty.util.AttributeKey) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) StepVerifier(reactor.test.StepVerifier) SNIHostName(javax.net.ssl.SNIHostName) AbortedException(reactor.netty.channel.AbortedException) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) HttpMessage(io.netty.handler.codec.http.HttpMessage) ByteBufHolder(io.netty.buffer.ByteBufHolder) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) TcpClient(reactor.netty.tcp.TcpClient) ConnectionObserver(reactor.netty.ConnectionObserver) Future(java.util.concurrent.Future) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) DEFAULT_FORM_DECODER_SPEC(reactor.netty.http.server.HttpServerFormDecoderProvider.DEFAULT_FORM_DECODER_SPEC) Path(java.nio.file.Path) LoopResources(reactor.netty.resources.LoopResources) HttpObjectDecoder(io.netty.handler.codec.http.HttpObjectDecoder) HttpRequest(io.netty.handler.codec.http.HttpRequest) Context(reactor.util.context.Context) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) Assertions.fail(org.assertj.core.api.Assertions.fail) NettyOutbound(reactor.netty.NettyOutbound) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) GZIPOutputStream(java.util.zip.GZIPOutputStream) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) HttpClient(reactor.netty.http.client.HttpClient) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ChannelOption(io.netty.channel.ChannelOption) HttpProtocol(reactor.netty.http.HttpProtocol) Nullable(reactor.util.annotation.Nullable) ArrayList(java.util.ArrayList) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) ValueSource(org.junit.jupiter.params.provider.ValueSource) PrematureCloseException(reactor.netty.http.client.PrematureCloseException) SslContext(io.netty.handler.ssl.SslContext) Publisher(org.reactivestreams.Publisher) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) Field(java.lang.reflect.Field) ReferenceCounted(io.netty.util.ReferenceCounted) Channel(io.netty.channel.Channel) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) DefaultEventExecutor(io.netty.util.concurrent.DefaultEventExecutor) Flux(reactor.core.publisher.Flux) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) FutureMono(reactor.netty.FutureMono) Paths(java.nio.file.Paths) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) ConnectionProvider(reactor.netty.resources.ConnectionProvider) ServerCookieEncoder(io.netty.handler.codec.http.cookie.ServerCookieEncoder) HttpUtil(io.netty.handler.codec.http.HttpUtil) SocketAddress(java.net.SocketAddress) Http11SslContextSpec(reactor.netty.http.Http11SslContextSpec) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) URISyntaxException(java.net.URISyntaxException) BiFunction(java.util.function.BiFunction) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Http2SslContextSpec(reactor.netty.http.Http2SslContextSpec) TimeoutException(java.util.concurrent.TimeoutException) BaseHttpTest(reactor.netty.BaseHttpTest) Random(java.util.Random) Unpooled(io.netty.buffer.Unpooled) GlobalEventExecutor(io.netty.util.concurrent.GlobalEventExecutor) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) TransportConfig(reactor.netty.transport.TransportConfig) ServerCookieDecoder(io.netty.handler.codec.http.cookie.ServerCookieDecoder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteBufFlux(reactor.netty.ByteBufFlux) NettyPipeline(reactor.netty.NettyPipeline) MethodSource(org.junit.jupiter.params.provider.MethodSource) ChannelGroup(io.netty.channel.group.ChannelGroup) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) SignalType(reactor.core.publisher.SignalType) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) ChannelBindException(reactor.netty.ChannelBindException) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) DisposableServer(reactor.netty.DisposableServer) HttpVersion(io.netty.handler.codec.http.HttpVersion) Tuple3(reactor.util.function.Tuple3) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketCloseStatus(io.netty.handler.codec.http.websocketx.WebSocketCloseStatus) Tuple2(reactor.util.function.Tuple2) HttpClientRequest(reactor.netty.http.client.HttpClientRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) SniCompletionEvent(io.netty.handler.ssl.SniCompletionEvent) Charset(java.nio.charset.Charset) ByteBuf(io.netty.buffer.ByteBuf) Connection(reactor.netty.Connection) ExecutorService(java.util.concurrent.ExecutorService) HttpHeaderValues(io.netty.handler.codec.http.HttpHeaderValues) Assumptions.assumeThat(org.assertj.core.api.Assumptions.assumeThat) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpMethod(io.netty.handler.codec.http.HttpMethod) CertificateException(java.security.cert.CertificateException) TimeUnit(java.util.concurrent.TimeUnit) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) TcpServer(reactor.netty.tcp.TcpServer) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor) Comparator(java.util.Comparator) Timeout(org.junit.jupiter.api.Timeout) LoopResources(reactor.netty.resources.LoopResources) HttpClient(reactor.netty.http.client.HttpClient) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) BaseHttpTest(reactor.netty.BaseHttpTest) Test(org.junit.jupiter.api.Test)

Example 4 with DefaultEventExecutor

use of io.netty.util.concurrent.DefaultEventExecutor in project reactor-netty by reactor.

the class TcpServerTests method testGracefulShutdown.

@Test
void testGracefulShutdown() throws Exception {
    CountDownLatch latch1 = new CountDownLatch(2);
    CountDownLatch latch2 = new CountDownLatch(2);
    CountDownLatch latch3 = new CountDownLatch(1);
    LoopResources loop = LoopResources.create("testGracefulShutdown");
    DisposableServer disposableServer = TcpServer.create().port(0).runOn(loop).doOnConnection(c -> {
        c.onDispose().subscribe(null, null, latch2::countDown);
        latch1.countDown();
    }).channelGroup(new DefaultChannelGroup(new DefaultEventExecutor())).handle((in, out) -> out.sendString(Mono.just("delay1000").delayElement(Duration.ofSeconds(1)))).wiretap(true).bindNow(Duration.ofSeconds(30));
    TcpClient client = TcpClient.create().remoteAddress(disposableServer::address).wiretap(true);
    AtomicReference<String> result = new AtomicReference<>();
    Flux.merge(client.connect(), client.connect()).flatMap(conn -> conn.inbound().receive().asString()).collect(Collectors.joining()).subscribe(s -> {
        result.set(s);
        latch3.countDown();
    });
    assertThat(latch1.await(30, TimeUnit.SECONDS)).as("latch await").isTrue();
    // Stop accepting incoming requests, wait at most 3s for the active requests to finish
    disposableServer.disposeNow();
    // Dispose the event loop
    loop.disposeLater().block(Duration.ofSeconds(30));
    assertThat(latch2.await(30, TimeUnit.SECONDS)).as("latch await").isTrue();
    assertThat(latch3.await(30, TimeUnit.SECONDS)).as("latch await").isTrue();
    assertThat(result.get()).isNotNull().isEqualTo("delay1000delay1000");
}
Also used : DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) DefaultEventExecutor(io.netty.util.concurrent.DefaultEventExecutor) DisposableServer(reactor.netty.DisposableServer) LoopResources(reactor.netty.resources.LoopResources) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultEventExecutor

use of io.netty.util.concurrent.DefaultEventExecutor in project moleculer-java by moleculer-java.

the class RedisGetSetClient method connect.

// --- CONNECT ---
public final void connect() {
    DefaultClientResources.Builder builder = DefaultClientResources.builder();
    acceptor = Executors.newSingleThreadExecutor();
    group = new NioEventLoopGroup(1, acceptor);
    builder.eventLoopGroupProvider(new EventLoopGroupProvider() {

        @Override
        public final int threadPoolSize() {
            return 1;
        }

        @Override
        public final Future<Boolean> shutdown(long quietPeriod, long timeout, TimeUnit timeUnit) {
            return null;
        }

        @Override
        public final Future<Boolean> release(EventExecutorGroup eventLoopGroup, long quietPeriod, long timeout, TimeUnit unit) {
            return null;
        }

        @SuppressWarnings("unchecked")
        @Override
        public final <T extends EventLoopGroup> T allocate(Class<T> type) {
            return (T) group;
        }
    });
    builder.eventExecutorGroup(new DefaultEventExecutor(executor));
    if (eventBus == null) {
        builder.eventBus(new EventBus() {

            @Override
            public final void publish(Event event) {
            }

            @Override
            public final Observable<Event> get() {
                return null;
            }
        });
    } else {
        builder.eventBus(eventBus);
    }
    resources = builder.build();
    List<RedisURI> redisURIs = parseURLs(urls, password, secure);
    ByteArrayCodec codec = new ByteArrayCodec();
    if (urls.length > 1) {
        // Clustered client
        clusteredClient = RedisClusterClient.create(resources, redisURIs).connect(codec).async();
    } else {
        // Single server connection
        client = RedisClient.create(resources, redisURIs.get(0)).connect(codec).async();
    }
}
Also used : EventExecutorGroup(io.netty.util.concurrent.EventExecutorGroup) DefaultEventExecutor(io.netty.util.concurrent.DefaultEventExecutor) RedisURI(com.lambdaworks.redis.RedisURI) EventBus(com.lambdaworks.redis.event.EventBus) DefaultClientResources(com.lambdaworks.redis.resource.DefaultClientResources) Observable(rx.Observable) ByteArrayCodec(com.lambdaworks.redis.codec.ByteArrayCodec) EventLoopGroupProvider(com.lambdaworks.redis.resource.EventLoopGroupProvider) TimeUnit(java.util.concurrent.TimeUnit) CompletableFuture(java.util.concurrent.CompletableFuture) RedisFuture(com.lambdaworks.redis.RedisFuture) Future(io.netty.util.concurrent.Future) Event(com.lambdaworks.redis.event.Event) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

DefaultEventExecutor (io.netty.util.concurrent.DefaultEventExecutor)8 DefaultChannelGroup (io.netty.channel.group.DefaultChannelGroup)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Test (org.junit.jupiter.api.Test)5 ChannelGroup (io.netty.channel.group.ChannelGroup)4 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)4 TimeUnit (java.util.concurrent.TimeUnit)4 DisposableServer (reactor.netty.DisposableServer)4 RedisURI (com.lambdaworks.redis.RedisURI)2 ByteArrayCodec (com.lambdaworks.redis.codec.ByteArrayCodec)2 Event (com.lambdaworks.redis.event.Event)2 EventBus (com.lambdaworks.redis.event.EventBus)2 DefaultClientResources (com.lambdaworks.redis.resource.DefaultClientResources)2 EventLoopGroupProvider (com.lambdaworks.redis.resource.EventLoopGroupProvider)2 ByteBuf (io.netty.buffer.ByteBuf)2 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)2 Unpooled (io.netty.buffer.Unpooled)2 Channel (io.netty.channel.Channel)2 DomainSocketAddress (io.netty.channel.unix.DomainSocketAddress)2 HttpClientCodec (io.netty.handler.codec.http.HttpClientCodec)2