Search in sources :

Example 1 with NioSocketChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioSocketChannel in project dubbo by alibaba.

the class NettyServer method doOpen.

@Override
protected void doOpen() throws Throwable {
    NettyHelper.setNettyLoggerFactory();
    bootstrap = new ServerBootstrap();
    bossGroup = new NioEventLoopGroup(1, new DefaultThreadFactory("NettyServerBoss", true));
    workerGroup = new NioEventLoopGroup(getUrl().getPositiveParameter(Constants.IO_THREADS_KEY, Constants.DEFAULT_IO_THREADS), new DefaultThreadFactory("NettyServerWorker", true));
    final NettyServerHandler nettyServerHandler = new NettyServerHandler(getUrl(), this);
    channels = nettyServerHandler.getChannels();
    bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childOption(ChannelOption.TCP_NODELAY, Boolean.TRUE).childOption(ChannelOption.SO_REUSEADDR, Boolean.TRUE).childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT).childHandler(new ChannelInitializer<NioSocketChannel>() {

        @Override
        protected void initChannel(NioSocketChannel ch) throws Exception {
            NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyServer.this);
            // .addLast("logging",new LoggingHandler(LogLevel.INFO))//for debug
            ch.pipeline().addLast("decoder", adapter.getDecoder()).addLast("encoder", adapter.getEncoder()).addLast("handler", nettyServerHandler);
        }
    });
    // bind
    ChannelFuture channelFuture = bootstrap.bind(getBindAddress());
    channelFuture.syncUninterruptibly();
    channel = channelFuture.channel();
}
Also used : DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) ChannelFuture(io.netty.channel.ChannelFuture) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) RemotingException(com.alibaba.dubbo.remoting.RemotingException)

Example 2 with NioSocketChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioSocketChannel in project protools by SeanDragon.

the class HttpClientChannelPoolHandler method channelCreated.

@Override
public void channelCreated(Channel channel) {
    NioSocketChannel nioSocketChannel = (NioSocketChannel) channel;
    nioSocketChannel.config().setTcpNoDelay(true).setKeepAlive(true);
    final ChannelPipeline p = nioSocketChannel.pipeline();
    // HTTPS
    if (sslCtx != null) {
        p.addLast(sslCtx.newHandler(channel.alloc()));
    }
    p.addLast(new HttpClientCodec(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE));
    p.addLast(new HttpObjectAggregator(Integer.MAX_VALUE));
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 3 with NioSocketChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioSocketChannel in project apiRecord by tobecoder2015.

the class HttpProxyServerHandle method handleProxyData.

private void handleProxyData(final ChannelHandlerContext ctx, final Object msg) throws InterruptedException {
    if (cf == null) {
        Bootstrap bootstrap = new Bootstrap();
        // 注册线程池
        bootstrap.group(NettyHttpProxyServer.proxyGroup).channel(// 使用NioSocketChannel来作为连接用的channel类
        NioSocketChannel.class).handler(new HttpProxyInitializer(ctx.channel(), isSSL));
        cf = bootstrap.connect(host, port).sync();
    /*cf.addListener(new ChannelFutureListener() {
                public void operationComplete(ChannelFuture future) throws Exception {
                    System.out.println("11111"+msg);
                    if (future.isSuccess()) {
                        future.channel().writeAndFlush(msg);
                    } else {
                        ctx.channel().close();
                    }
                }
            });*/
    }
    cf.channel().writeAndFlush(msg);
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Bootstrap(io.netty.bootstrap.Bootstrap)

Example 4 with NioSocketChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioSocketChannel in project reactor-netty by reactor.

the class DefaultPoolResourcesTest method fixedPoolTwoAcquire.

@Test
public void fixedPoolTwoAcquire() throws ExecutionException, InterruptedException, IOException {
    final ScheduledExecutorService service = Executors.newScheduledThreadPool(2);
    int echoServerPort = SocketUtils.findAvailableTcpPort();
    TcpClientTests.EchoServer echoServer = new TcpClientTests.EchoServer(echoServerPort);
    List<Channel> createdChannels = new ArrayList<>();
    try {
        final InetSocketAddress address = InetSocketAddress.createUnresolved("localhost", echoServerPort);
        ChannelPool pool = PoolResources.fixed("fixedPoolTwoAcquire", 2).selectOrCreate(address, () -> new Bootstrap().remoteAddress(address).channelFactory(NioSocketChannel::new).group(new NioEventLoopGroup(2)), createdChannels::add, new NioEventLoopGroup(2));
        // fail a couple
        assertThatExceptionOfType(Throwable.class).isThrownBy(pool.acquire()::get).withMessageContaining("Connection refused");
        assertThatExceptionOfType(Throwable.class).isThrownBy(pool.acquire()::get).withMessageContaining("Connection refused");
        // start the echo server
        service.submit(echoServer);
        Thread.sleep(100);
        // acquire 2
        final Channel channel1 = pool.acquire().get();
        final Channel channel2 = pool.acquire().get();
        // make room for 1 more
        channel2.close().get();
        final Channel channel3 = pool.acquire().get();
        // next one will block until a previous one is released
        long start = System.currentTimeMillis();
        service.schedule(() -> pool.release(channel1), 500, TimeUnit.MILLISECONDS);
        final Channel channel4 = pool.acquire().get();
        long end = System.currentTimeMillis();
        assertThat(end - start).as("channel4 acquire blocked until channel1 released").isGreaterThanOrEqualTo(500);
        pool.release(channel3).get();
        pool.release(channel4).get();
        assertThat(pool).isInstanceOf(DefaultPoolResources.Pool.class);
        DefaultPoolResources.Pool defaultPool = (DefaultPoolResources.Pool) pool;
        assertThat(defaultPool.activeConnections.get()).as("activeConnections fully released").isZero();
    } finally {
        echoServer.close();
    }
}
Also used : ChannelPool(io.netty.channel.pool.ChannelPool) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TcpClientTests(reactor.ipc.netty.tcp.TcpClientTests) InetSocketAddress(java.net.InetSocketAddress) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) ArrayList(java.util.ArrayList) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelPool(io.netty.channel.pool.ChannelPool) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Example 5 with NioSocketChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioSocketChannel in project grpc-java by grpc.

the class UtilsTest method channelOptionsTest_nio.

@Test
public void channelOptionsTest_nio() {
    Channel channel = new NioSocketChannel();
    SocketOptions socketOptions = setAndValidateGeneric(channel);
    assertNull(socketOptions.soTimeoutMillis);
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ServerChannel(io.netty.channel.ServerChannel) Channel(io.netty.channel.Channel) SocketOptions(io.grpc.InternalChannelz.SocketOptions) Test(org.junit.Test)

Aggregations

NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)17 Test (org.junit.Test)8 Bootstrap (io.netty.bootstrap.Bootstrap)5 Channel (io.netty.channel.Channel)5 ChannelFuture (io.netty.channel.ChannelFuture)5 ChannelInitializer (io.netty.channel.ChannelInitializer)4 ChannelPipeline (io.netty.channel.ChannelPipeline)4 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)4 ChannelPool (io.netty.channel.pool.ChannelPool)4 InetSocketAddress (java.net.InetSocketAddress)4 MetricRegistry (com.codahale.metrics.MetricRegistry)3 MultiplexedChannelRecordTest (com.github.ambry.network.http2.MultiplexedChannelRecordTest)3 SocketChannel (io.netty.channel.socket.SocketChannel)3 SocketChannelConfig (io.netty.channel.socket.SocketChannelConfig)3 DefaultPromise (io.netty.util.concurrent.DefaultPromise)3 RskSystemProperties (co.rsk.config.RskSystemProperties)2 PeerScoringManager (co.rsk.scoring.PeerScoringManager)2 ChannelFutureListener (io.netty.channel.ChannelFutureListener)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)2