Search in sources :

Example 61 with EventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup in project jdepth by Crab2died.

the class C2DServer method bind.

private void bind(String host, int port) throws InterruptedException {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap bootstrap = new ServerBootstrap().group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(new C2DHessianMsgDecoder(1024 * 1024, 4, 4, -8, 0)).addLast("MessageEncoder", new C2DHessianMsgEncoder()).addLast("ReadTimeoutHandler", new ReadTimeoutHandler(TIME_OUT)).addLast("LoginAuthResp", new LoginAuthRespHandler()).addLast("Pong", new PongHandler());
            }
        });
        ChannelFuture future = bootstrap.bind(host, port).sync();
        future.channel().closeFuture().sync();
        logger.info("server is started");
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) PongHandler(com.github.jvm.io.protocol.c2d.heart.PongHandler) C2DHessianMsgDecoder(com.github.jvm.io.protocol.c2d.codc.hessian.C2DHessianMsgDecoder) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) C2DHessianMsgEncoder(com.github.jvm.io.protocol.c2d.codc.hessian.C2DHessianMsgEncoder) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) LoginAuthRespHandler(com.github.jvm.io.protocol.c2d.auth.LoginAuthRespHandler) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 62 with EventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup in project x-pipe by ctripcorp.

the class PsyncLatencyTest method startGetLatency.

private void startGetLatency() {
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    Bootstrap b = new Bootstrap();
    b.group(eventLoopGroup).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            p.addLast(new LoggingHandler(LogLevel.DEBUG));
            p.addLast(new NettySimpleMessageHandler());
            p.addLast(new ReceiveMessageHandler(runId, offset));
        }
    });
    b.connect(dest);
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) NettySimpleMessageHandler(com.ctrip.xpipe.netty.NettySimpleMessageHandler) ReceiveMessageHandler(com.ctrip.xpipe.redis.keeper.simple.latency.netty.ReceiveMessageHandler) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 63 with EventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup in project component-runtime by Talend.

the class HandlerImpl method start.

public synchronized HandlerImpl<T> start() {
    if (instance != null) {
        throw new IllegalStateException("Instance already started");
    }
    if (handler.getPort() <= 0) {
        handler.setPort(newRandomPort());
    }
    final CountDownLatch startingPistol = new CountDownLatch(1);
    instance = new Thread(() -> {
        // todo: config
        final EventLoopGroup bossGroup = new NioEventLoopGroup(1, new DefaultThreadFactory("talend-api-boss"));
        final EventLoopGroup workerGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors(), new DefaultThreadFactory("talend-api-worker"));
        try {
            final ServerBootstrap b = new ServerBootstrap();
            b.option(ChannelOption.SO_REUSEADDR, true).group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ProxyInitializer(handler)).bind("localhost", handler.getPort()).sync().addListener((ChannelFutureListener) f -> {
                if (f.isSuccess()) {
                    shutdown = () -> {
                        bossGroup.shutdownGracefully();
                        workerGroup.shutdownGracefully();
                    };
                } else {
                    log.error("Can't start API server");
                }
                startingPistol.countDown();
            }).channel().closeFuture().sync();
        } catch (final InterruptedException e) {
            Thread.interrupted();
            close();
        }
    }) {

        {
            setName("Talend-API-monitor_" + HandlerImpl.this.getClass().getSimpleName() + "_" + HandlerImpl.this.hashCode());
        }
    };
    log.info("Starting Talend API server on port {}", handler.getPort());
    instance.start();
    try {
        if (!startingPistol.await(Integer.getInteger("talend.junit.http.starting.timeout", 60), SECONDS)) {
            log.warn("API server took more than the expected timeout to start, you can tune it " + "setting talend.junit.http.starting.timeout system property");
        }
    } catch (final InterruptedException e) {
        Thread.interrupted();
        log.warn(e.getMessage());
    }
    if (shutdown != null && handler.isGlobalProxyConfiguration()) {
        final String pt = Integer.toString(handler.getPort());
        Stream.of("", "s").forEach(s -> {
            shutdown = decorate(setProperty("http" + s + ".proxyHost", "localhost"), shutdown);
            shutdown = decorate(setProperty("http" + s + ".proxyPort", pt), shutdown);
            shutdown = decorate(setProperty("http" + s + ".nonProxyHosts", "local|*.local"), shutdown);
        });
        if (handler.getSslContext() != null) {
            try {
                final SSLContext defaultSslContext = SSLContext.getDefault();
                final HostnameVerifier defaultHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
                shutdown = decorate(() -> SSLContext.setDefault(defaultSslContext), shutdown);
                shutdown = decorate(() -> {
                    HttpsURLConnection.setDefaultSSLSocketFactory(defaultSslContext.getSocketFactory());
                    HttpsURLConnection.setDefaultHostnameVerifier(defaultHostnameVerifier);
                }, shutdown);
                SSLContext.setDefault(handler.getSslContext());
                HttpsURLConnection.setDefaultSSLSocketFactory(handler.getSslContext().getSocketFactory());
                HttpsURLConnection.setDefaultHostnameVerifier((host, sslSession) -> true);
            } catch (final NoSuchAlgorithmException e) {
                throw new IllegalStateException(e);
            }
        }
        log.info("Configured the JVM to use the {} API proxy localhost:{}", handler.getSslContext() != null ? "SSL" : "plain", handler.getPort());
    }
    return this;
}
Also used : HttpsURLConnection(javax.net.ssl.HttpsURLConnection) SSLContext(javax.net.ssl.SSLContext) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) ChannelOption(io.netty.channel.ChannelOption) Optional.ofNullable(java.util.Optional.ofNullable) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) IOException(java.io.IOException) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) HttpApiHandler(org.talend.sdk.component.junit.http.api.HttpApiHandler) ServerSocket(java.net.ServerSocket) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) HostnameVerifier(javax.net.ssl.HostnameVerifier) AllArgsConstructor(lombok.AllArgsConstructor) SECONDS(java.util.concurrent.TimeUnit.SECONDS) ExecutorService(java.util.concurrent.ExecutorService) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CountDownLatch(java.util.concurrent.CountDownLatch) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) HostnameVerifier(javax.net.ssl.HostnameVerifier) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 64 with EventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup in project cassandra by apache.

the class ProxyHandlerTest method test.

public void test(DoTest test) throws Throwable {
    EventLoopGroup serverGroup = new NioEventLoopGroup(1);
    EventLoopGroup clientGroup = new NioEventLoopGroup(1);
    InboundProxyHandler.Controller controller = new InboundProxyHandler.Controller();
    InboundProxyHandler proxyHandler = new InboundProxyHandler(controller);
    TestHandler testHandler = new TestHandler();
    ServerBootstrap sb = new ServerBootstrap();
    sb.group(serverGroup).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<LocalChannel>() {

        @Override
        public void initChannel(LocalChannel ch) {
            ch.pipeline().addLast(proxyHandler).addLast(testHandler);
        }
    }).childOption(ChannelOption.AUTO_READ, false);
    Bootstrap cb = new Bootstrap();
    cb.group(clientGroup).channel(LocalChannel.class).handler(new ChannelInitializer<LocalChannel>() {

        @Override
        public void initChannel(LocalChannel ch) throws Exception {
            ch.pipeline().addLast(new LoggingHandler(LogLevel.TRACE));
        }
    });
    final LocalAddress addr = new LocalAddress("test");
    Channel serverChannel = sb.bind(addr).sync().channel();
    Channel clientChannel = cb.connect(addr).sync().channel();
    test.doTest(controller, testHandler, clientChannel);
    clientChannel.close();
    serverChannel.close();
    serverGroup.shutdownGracefully();
    clientGroup.shutdownGracefully();
}
Also used : LoggingHandler(io.netty.handler.logging.LoggingHandler) LocalAddress(io.netty.channel.local.LocalAddress) LocalChannel(io.netty.channel.local.LocalChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Channel(io.netty.channel.Channel) LocalChannel(io.netty.channel.local.LocalChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 65 with EventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup in project flink by apache.

the class AbstractServerBase method shutdownServer.

/**
 * Shuts down the server and all related thread pools.
 *
 * @return A {@link CompletableFuture} that will be completed upon termination of the shutdown
 *     process.
 */
public CompletableFuture<Void> shutdownServer() {
    CompletableFuture<Void> shutdownFuture = new CompletableFuture<>();
    if (serverShutdownFuture.compareAndSet(null, shutdownFuture)) {
        log.info("Shutting down {} @ {}", serverName, serverAddress);
        final CompletableFuture<Void> groupShutdownFuture = new CompletableFuture<>();
        if (bootstrap != null) {
            EventLoopGroup group = bootstrap.group();
            if (group != null && !group.isShutdown()) {
                group.shutdownGracefully(0L, 0L, TimeUnit.MILLISECONDS).addListener(finished -> {
                    if (finished.isSuccess()) {
                        groupShutdownFuture.complete(null);
                    } else {
                        groupShutdownFuture.completeExceptionally(finished.cause());
                    }
                });
            } else {
                groupShutdownFuture.complete(null);
            }
        } else {
            groupShutdownFuture.complete(null);
        }
        final CompletableFuture<Void> handlerShutdownFuture = new CompletableFuture<>();
        if (handler == null) {
            handlerShutdownFuture.complete(null);
        } else {
            handler.shutdown().whenComplete((result, throwable) -> {
                if (throwable != null) {
                    handlerShutdownFuture.completeExceptionally(throwable);
                } else {
                    handlerShutdownFuture.complete(null);
                }
            });
        }
        final CompletableFuture<Void> queryExecShutdownFuture = CompletableFuture.runAsync(() -> {
            if (queryExecutor != null) {
                ExecutorUtils.gracefulShutdown(10L, TimeUnit.MINUTES, queryExecutor);
            }
        });
        CompletableFuture.allOf(queryExecShutdownFuture, groupShutdownFuture, handlerShutdownFuture).whenComplete((result, throwable) -> {
            if (throwable != null) {
                shutdownFuture.completeExceptionally(throwable);
            } else {
                shutdownFuture.complete(null);
            }
        });
    }
    return serverShutdownFuture.get();
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) EventLoopGroup(org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup) NioEventLoopGroup(org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup)

Aggregations

EventLoopGroup (io.netty.channel.EventLoopGroup)367 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)271 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)162 Bootstrap (io.netty.bootstrap.Bootstrap)137 Channel (io.netty.channel.Channel)131 ChannelFuture (io.netty.channel.ChannelFuture)129 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)109 SocketChannel (io.netty.channel.socket.SocketChannel)94 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)92 InetSocketAddress (java.net.InetSocketAddress)69 Test (org.junit.jupiter.api.Test)67 DefaultEventLoopGroup (io.netty.channel.DefaultEventLoopGroup)60 LoggingHandler (io.netty.handler.logging.LoggingHandler)55 ChannelPipeline (io.netty.channel.ChannelPipeline)51 SslContext (io.netty.handler.ssl.SslContext)51 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)50 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)49 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)46 LocalServerChannel (io.netty.channel.local.LocalServerChannel)42 LocalChannel (io.netty.channel.local.LocalChannel)40