Search in sources :

Example 61 with ChannelInitializer

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelInitializer in project chuidiang-ejemplos by chuidiang.

the class Client method run.

public void run() throws Exception {
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        // (2)
        Bootstrap b = new Bootstrap();
        b.group(workerGroup).channel(// (3)
        NioSocketChannel.class).handler(new // (4)
        ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(clientHandler);
            }
        }).option(ChannelOption.SO_KEEPALIVE, // (6)
        true);
        // Bind and start to accept incoming connections.
        // (7)
        ChannelFuture f = b.connect("localhost", port).sync();
        // Wait until the server socket is closed.
        // In this example, this does not happen, but you can do that to gracefully
        // shut down your server.
        f.channel().closeFuture().sync();
    } finally {
        workerGroup.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 62 with ChannelInitializer

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelInitializer in project chuidiang-ejemplos by chuidiang.

the class Client method run.

public void run() throws Exception {
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        // (2)
        Bootstrap b = new Bootstrap();
        b.group(workerGroup).channel(// (3)
        NioSocketChannel.class).handler(new // (4)
        ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(new StringDecoder());
                ch.pipeline().addLast(new StringEncoder());
                ch.pipeline().addLast(clientHandler);
            }
        }).option(ChannelOption.SO_KEEPALIVE, // (6)
        true);
        // Bind and start to accept incoming connections.
        // (7)
        ChannelFuture f = b.connect("localhost", port).sync();
        // Wait until the server socket is closed.
        // In this example, this does not happen, but you can do that to gracefully
        // shut down your server.
        f.channel().closeFuture().sync();
    } finally {
        workerGroup.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 63 with ChannelInitializer

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelInitializer in project ambry by linkedin.

the class MultiplexedChannelRecordTest method setup.

@Before
public void setup() throws Exception {
    loopGroup = new NioEventLoopGroup(4);
    channel = new MockChannel();
    idleTimeoutMillis = 500L;
    streamChannelInitializer = new ChannelInitializer() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
        // do nothing
        }
    };
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Before(org.junit.Before)

Example 64 with ChannelInitializer

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelInitializer in project ambry by linkedin.

the class FrontendNettyFactoryTest method doGetNettyServerTest.

/**
 * Test that a {@link NettyServer} can be constructed by the factory.
 * @param properties the {@link Properties} to use.
 * @param defaultSslFactory the default {@link SSLFactory} to pass into the constructor.
 */
private void doGetNettyServerTest(Properties properties, SSLFactory defaultSslFactory) throws Exception {
    VerifiableProperties verifiableProperties = new VerifiableProperties(properties);
    NettyConfig nettyConfig = new NettyConfig(verifiableProperties);
    FrontendNettyFactory nettyServerFactory = new FrontendNettyFactory(verifiableProperties, new MetricRegistry(), REST_REQUEST_HANDLER, PUBLIC_ACCESS_LOGGER, REST_SERVER_STATE, defaultSslFactory, null);
    NioServer nioServer = nettyServerFactory.getNioServer();
    assertNotNull("No NioServer returned", nioServer);
    assertEquals("Did not receive a NettyServer instance", NettyServer.class.getCanonicalName(), nioServer.getClass().getCanonicalName());
    Map<Integer, ChannelInitializer<SocketChannel>> channelInitializers = nettyServerFactory.channelInitializers;
    if (nettyConfig.nettyServerEnableSSL && defaultSslFactory != null) {
        assertEquals("Expected two ChannelInitializers when SSLFactory is not null", 2, channelInitializers.size());
        assertNotNull("No ChannelInitializer for SSL port", channelInitializers.get(nettyConfig.nettyServerSSLPort));
    } else {
        assertEquals("Expected one ChannelInitializer when SSLFactory is null", 1, channelInitializers.size());
    }
    assertNotNull("No ChannelInitializer for plaintext port", channelInitializers.get(nettyConfig.nettyServerPort));
}
Also used : VerifiableProperties(com.github.ambry.config.VerifiableProperties) MetricRegistry(com.codahale.metrics.MetricRegistry) ChannelInitializer(io.netty.channel.ChannelInitializer) NettyConfig(com.github.ambry.config.NettyConfig)

Example 65 with ChannelInitializer

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelInitializer in project ambry by linkedin.

the class NettyServer method start.

@Override
public void start() throws InstantiationException {
    long startupBeginTime = System.currentTimeMillis();
    try {
        logger.trace("Starting NettyServer deployment");
        if (Epoll.isAvailable()) {
            logger.trace("Using EpollEventLoopGroup in NettyServer.");
            bossGroup = new EpollEventLoopGroup(nettyConfig.nettyServerBossThreadCount);
            workerGroup = new EpollEventLoopGroup(nettyConfig.nettyServerWorkerThreadCount);
        } else {
            bossGroup = new NioEventLoopGroup(nettyConfig.nettyServerBossThreadCount);
            workerGroup = new NioEventLoopGroup(nettyConfig.nettyServerWorkerThreadCount);
        }
        for (Map.Entry<Integer, ChannelInitializer<SocketChannel>> entry : channelInitializers.entrySet()) {
            bindServer(entry.getKey(), entry.getValue(), bossGroup, workerGroup);
        }
        nettyMetrics.registerNettyPendingTasksGauge(bossGroup, "Boss");
        nettyMetrics.registerNettyPendingTasksGauge(workerGroup, "Worker");
    } catch (InterruptedException e) {
        logger.error("NettyServer start await was interrupted", e);
        nettyMetrics.nettyServerStartError.inc();
        throw new InstantiationException("Netty server bind to port [" + nettyConfig.nettyServerPort + "] was interrupted");
    } finally {
        long startupTime = System.currentTimeMillis() - startupBeginTime;
        logger.info("NettyServer start took {} ms", startupTime);
        nettyMetrics.nettyServerStartTimeInMs.update(startupTime);
    }
}
Also used : EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) ChannelInitializer(io.netty.channel.ChannelInitializer) Map(java.util.Map) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

ChannelInitializer (io.netty.channel.ChannelInitializer)83 Channel (io.netty.channel.Channel)58 Bootstrap (io.netty.bootstrap.Bootstrap)42 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)39 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)36 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)32 InetSocketAddress (java.net.InetSocketAddress)32 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)31 ChannelFuture (io.netty.channel.ChannelFuture)29 EventLoopGroup (io.netty.channel.EventLoopGroup)26 ChannelPipeline (io.netty.channel.ChannelPipeline)25 LocalServerChannel (io.netty.channel.local.LocalServerChannel)22 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)21 LocalChannel (io.netty.channel.local.LocalChannel)20 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)17 SslHandler (io.netty.handler.ssl.SslHandler)17 SocketChannel (io.netty.channel.socket.SocketChannel)16 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)14 DefaultEventLoopGroup (io.netty.channel.DefaultEventLoopGroup)12 CountDownLatch (java.util.concurrent.CountDownLatch)12