Search in sources :

Example 16 with NioSocketChannel

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

the class HttpProxyServerHandle method transData.

private void transData(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 ChannelInitializer() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    @Override
                    public void channelRead(ChannelHandlerContext ctx0, Object msg0) throws Exception {
                        ctx.channel().writeAndFlush(msg0);
                    }
                });
            }
        });
        cf = bootstrap.connect(host, port).sync();
    }
    cf.channel().writeAndFlush(msg);
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Bootstrap(io.netty.bootstrap.Bootstrap)

Example 17 with NioSocketChannel

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

the class ServerTest method getListeningSockets.

@Test
public void getListeningSockets() throws Exception {
    ServerStatusManager ssm = mock(ServerStatusManager.class);
    Map<NamedSocketAddress, ChannelInitializer<?>> initializers = new HashMap<>();
    final List<NioSocketChannel> nioChannels = Collections.synchronizedList(new ArrayList<NioSocketChannel>());
    ChannelInitializer<Channel> init = new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(final Channel ch) {
            LOGGER.info("Channel: " + ch.getClass().getName() + ", isActive=" + ch.isActive() + ", isOpen=" + ch.isOpen());
            if (ch instanceof NioSocketChannel) {
                nioChannels.add((NioSocketChannel) ch);
            }
        }
    };
    initializers.put(new NamedSocketAddress("test", new InetSocketAddress(0)), init);
    // The port to channel map keys on the port, post bind. This should be unique even if InetAddress is same
    initializers.put(new NamedSocketAddress("test2", new InetSocketAddress(0)), init);
    ClientConnectionsShutdown ccs = new ClientConnectionsShutdown(new DefaultChannelGroup(GlobalEventExecutor.INSTANCE), GlobalEventExecutor.INSTANCE, /* discoveryClient= */
    null);
    EventLoopGroupMetrics elgm = new EventLoopGroupMetrics(Spectator.globalRegistry());
    EventLoopConfig elc = new EventLoopConfig() {

        @Override
        public int eventLoopCount() {
            return 1;
        }

        @Override
        public int acceptorCount() {
            return 1;
        }
    };
    Server s = new Server(new NoopRegistry(), ssm, initializers, ccs, elgm, elc);
    s.start();
    List<NamedSocketAddress> addrs = s.getListeningAddresses();
    assertEquals(2, addrs.size());
    for (NamedSocketAddress address : addrs) {
        assertTrue(address.unwrap() instanceof InetSocketAddress);
        final int port = ((InetSocketAddress) address.unwrap()).getPort();
        assertNotEquals(port, 0);
        checkConnection(port);
    }
    await().atMost(1, SECONDS).until(() -> nioChannels.size() == 2);
    s.stop();
    assertEquals(2, nioChannels.size());
    for (NioSocketChannel ch : nioChannels) {
        assertTrue("isShutdown", ch.isShutdown());
    }
}
Also used : DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) ServerStatusManager(com.netflix.netty.common.status.ServerStatusManager) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EventLoopGroupMetrics(com.netflix.netty.common.metrics.EventLoopGroupMetrics) NoopRegistry(com.netflix.spectator.api.NoopRegistry) ChannelInitializer(io.netty.channel.ChannelInitializer) 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