Search in sources :

Example 66 with NioEventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project pancm_project by xuwujing.

the class NettyServerDemo4 method start.

/**
 * Start.
 */
public void start() {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap sbs = new ServerBootstrap().group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(new InetSocketAddress(port)).childHandler(new ChannelInitializer<SocketChannel>() {

            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                p.addLast(new StringDecoder());
                // 绑定自定义业务逻辑
                p.addLast(new NettyServerHandlerDemo4());
            }
        }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);
        // 绑定端口,开始接收进来的连接
        ChannelFuture future = sbs.bind(port).sync();
        System.out.println("Netty服务端启动成功,端口为: " + port);
        // 释放监听
        future.channel().closeFuture().sync();
    } catch (Exception e) {
        // 释放资源
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) InetSocketAddress(java.net.InetSocketAddress) StringDecoder(io.netty.handler.codec.string.StringDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 67 with NioEventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project uavstack by uavorg.

the class AbstractHttpServiceComponent2 method start.

@Override
public void start(int port, int backlog, int listenThreadCount, int handleThreadCount, boolean forceExit) {
    EventLoopGroup bossGroup = new NioEventLoopGroup(listenThreadCount);
    EventLoopGroup workerGroup = new NioEventLoopGroup(handleThreadCount);
    server = new ServerBootstrap();
    server.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(port).option(ChannelOption.SO_BACKLOG, backlog).childHandler(new HttpServerInitializer(this));
    try {
        this.host = NetworkHelper.getLocalIP();
        this.port = port;
        // Start the server.
        server.bind().sync();
        if (log.isTraceEnable()) {
            log.info(this, "HttpServiceComponent[" + this.cName + "] for feature[" + this.feature + "] started SUCCESS: port=" + this.port);
        }
    } catch (Exception e) {
        log.err(this, "HttpServiceComponent[" + this.cName + "] for feature[" + this.feature + "] starts FAIL.", e);
        if (forceExit == true) {
            System.exit(-1);
        }
    }
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 68 with NioEventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project herddb by diennea.

the class ChannelBenchTest method test.

@Test
public void test() throws Exception {
    try (NettyChannelAcceptor acceptor = new NettyChannelAcceptor("localhost", NetworkUtils.assignFirstFreePort(), false)) {
        acceptor.setAcceptor((Channel channel) -> {
            channel.setMessagesReceiver(new ChannelEventListener() {

                @Override
                public void requestReceived(Pdu message, Channel channel) {
                    ByteBuf msg = buildAckResponse(message);
                    channel.sendReplyMessage(message.messageId, msg);
                    message.close();
                }

                @Override
                public void channelClosed(Channel channel) {
                }
            });
            return (ServerSideConnection) () -> new Random().nextLong();
        });
        acceptor.start();
        ExecutorService executor = Executors.newCachedThreadPool();
        try (Channel client = NettyConnector.connect(acceptor.getHost(), acceptor.getPort(), acceptor.isSsl(), 0, 0, new ChannelEventListener() {

            @Override
            public void channelClosed(Channel channel) {
                System.out.println("client channelClosed");
            }
        }, executor, new NioEventLoopGroup(10, executor))) {
            for (int i = 0; i < 100; i++) {
                ByteBuf buffer = buildAckRequest(i);
                try (Pdu result = client.sendMessageWithPduReply(i, buffer, 10000)) {
                    assertEquals(Pdu.TYPE_ACK, result.type);
                }
            }
        } finally {
            executor.shutdown();
        }
    }
}
Also used : ChannelEventListener(herddb.network.ChannelEventListener) Pdu(herddb.proto.Pdu) Random(java.util.Random) Channel(herddb.network.Channel) ExecutorService(java.util.concurrent.ExecutorService) ServerSideConnection(herddb.network.ServerSideConnection) ByteBuf(io.netty.buffer.ByteBuf) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Example 69 with NioEventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project herddb by diennea.

the class NetworkChannelTest method test.

@Test
public void test() throws Exception {
    try (NettyChannelAcceptor acceptor = new NettyChannelAcceptor("localhost", NetworkUtils.assignFirstFreePort(), true)) {
        acceptor.setEnableJVMNetwork(false);
        acceptor.setAcceptor((Channel channel) -> {
            channel.setMessagesReceiver(new ChannelEventListener() {

                @Override
                public void requestReceived(Pdu message, Channel channel) {
                    ByteBuf msg = buildAckResponse(message);
                    channel.sendReplyMessage(message.messageId, msg);
                    message.close();
                }

                @Override
                public void channelClosed(Channel channel) {
                }
            });
            return (ServerSideConnection) () -> new Random().nextLong();
        });
        acceptor.start();
        ExecutorService executor = Executors.newCachedThreadPool();
        try (Channel client = NettyConnector.connect(acceptor.getHost(), acceptor.getPort(), true, 0, 0, new ChannelEventListener() {

            @Override
            public void channelClosed(Channel channel) {
                System.out.println("client channelClosed");
            }
        }, executor, new NioEventLoopGroup(10, executor))) {
            for (int i = 0; i < 100; i++) {
                ByteBuf buffer = buildAckRequest(i);
                try (Pdu result = client.sendMessageWithPduReply(i, Unpooled.wrappedBuffer(buffer), 10000)) {
                    assertEquals(Pdu.TYPE_ACK, result.type);
                }
            }
        } finally {
            executor.shutdown();
        }
    }
    if (NetworkUtils.isEnableEpoolNative()) {
        try (NettyChannelAcceptor acceptor = new NettyChannelAcceptor("localhost", NetworkUtils.assignFirstFreePort(), true)) {
            acceptor.setEnableJVMNetwork(false);
            acceptor.setAcceptor((Channel channel) -> {
                channel.setMessagesReceiver(new ChannelEventListener() {

                    @Override
                    public void requestReceived(Pdu message, Channel channel) {
                        ByteBuf msg = buildAckResponse(message);
                        channel.sendReplyMessage(message.messageId, msg);
                        message.close();
                    }

                    @Override
                    public void channelClosed(Channel channel) {
                    }
                });
                return (ServerSideConnection) () -> new Random().nextLong();
            });
            acceptor.start();
            ExecutorService executor = Executors.newCachedThreadPool();
            try (Channel client = NettyConnector.connect(acceptor.getHost(), acceptor.getPort(), true, 0, 0, new ChannelEventListener() {

                @Override
                public void channelClosed(Channel channel) {
                    System.out.println("client channelClosed");
                }
            }, executor, new EpollEventLoopGroup(10, executor))) {
                for (int i = 0; i < 100; i++) {
                    ByteBuf buffer = buildAckRequest(i);
                    try (Pdu result = client.sendMessageWithPduReply(i, Unpooled.wrappedBuffer(buffer), 10000)) {
                        assertEquals(Pdu.TYPE_ACK, result.type);
                    }
                }
            } finally {
                executor.shutdown();
            }
        }
    }
}
Also used : ChannelEventListener(herddb.network.ChannelEventListener) Pdu(herddb.proto.Pdu) Random(java.util.Random) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) Channel(herddb.network.Channel) ExecutorService(java.util.concurrent.ExecutorService) ServerSideConnection(herddb.network.ServerSideConnection) ByteBuf(io.netty.buffer.ByteBuf) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Example 70 with NioEventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project herddb by diennea.

the class NetworkChannelTest method testServerPushesData.

@Test
public void testServerPushesData() throws Exception {
    try (NettyChannelAcceptor acceptor = new NettyChannelAcceptor("localhost", NetworkUtils.assignFirstFreePort(), true)) {
        acceptor.setEnableJVMNetwork(false);
        acceptor.setEnableRealNetwork(true);
        acceptor.setAcceptor((Channel channel) -> {
            channel.setMessagesReceiver(new ChannelEventListener() {

                @Override
                public void requestReceived(Pdu message, Channel channel) {
                    try {
                        ByteBuf msg = buildAckResponse(message);
                        // send a message to the client
                        ByteBuf buffer = buildAckRequest(900);
                        Pdu response = channel.sendMessageWithPduReply(900, buffer, 10000);
                        assertEquals(Pdu.TYPE_ACK, response.type);
                        // send the response to the client
                        channel.sendReplyMessage(message.messageId, msg);
                        message.close();
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    } catch (TimeoutException ex) {
                        ex.printStackTrace();
                    }
                }

                @Override
                public void channelClosed(Channel channel) {
                }
            });
            return (ServerSideConnection) () -> new Random().nextLong();
        });
        acceptor.start();
        ExecutorService executor = Executors.newCachedThreadPool();
        CopyOnWriteArrayList<Long> pushedMessagesFromServer = new CopyOnWriteArrayList<>();
        try (Channel client = NettyConnector.connect(acceptor.getHost(), acceptor.getPort(), true, 0, 0, new ChannelEventListener() {

            @Override
            public void requestReceived(Pdu pdu, Channel channel) {
                pushedMessagesFromServer.add(pdu.messageId);
                assertTrue(pdu.isRequest());
                ByteBuf msg = buildAckResponse(pdu);
                // send the response to the server
                channel.sendReplyMessage(pdu.messageId, msg);
                pdu.close();
            }

            @Override
            public void channelClosed(Channel channel) {
                System.out.println("client channelClosed");
            }
        }, executor, new NioEventLoopGroup(10, executor))) {
            ByteBuf buffer = buildAckRequest(134);
            try (Pdu result = client.sendMessageWithPduReply(134, buffer, 10000)) {
                assertEquals(Pdu.TYPE_ACK, result.type);
            }
            assertEquals(1, pushedMessagesFromServer.size());
        } finally {
            executor.shutdown();
        }
    }
}
Also used : Pdu(herddb.proto.Pdu) Channel(herddb.network.Channel) ServerSideConnection(herddb.network.ServerSideConnection) ByteBuf(io.netty.buffer.ByteBuf) ChannelEventListener(herddb.network.ChannelEventListener) Random(java.util.Random) ExecutorService(java.util.concurrent.ExecutorService) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) TimeoutException(java.util.concurrent.TimeoutException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Aggregations

NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)521 EventLoopGroup (io.netty.channel.EventLoopGroup)256 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)226 Bootstrap (io.netty.bootstrap.Bootstrap)184 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)178 SocketChannel (io.netty.channel.socket.SocketChannel)169 ChannelFuture (io.netty.channel.ChannelFuture)157 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)157 Channel (io.netty.channel.Channel)138 InetSocketAddress (java.net.InetSocketAddress)118 LoggingHandler (io.netty.handler.logging.LoggingHandler)87 ChannelPipeline (io.netty.channel.ChannelPipeline)84 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)73 SslContext (io.netty.handler.ssl.SslContext)64 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)49 IOException (java.io.IOException)49 EpollEventLoopGroup (io.netty.channel.epoll.EpollEventLoopGroup)47 ByteBuf (io.netty.buffer.ByteBuf)44 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)43 ChannelInitializer (io.netty.channel.ChannelInitializer)41