Search in sources :

Example 46 with ServerBootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap in project java-in-action by xinghalo.

the class NettyOioServer method serve.

public void serve(int port) throws InterruptedException {
    final ByteBuf buf = Unpooled.unreleasableBuffer(Unpooled.copiedBuffer("Hi\r\b", Charset.forName("UTF-8")));
    // 创建Boss线程组
    EventLoopGroup bossGroup = new OioEventLoopGroup(1);
    // 创建Worker线程组,默认线程数量与CPU的核的数量有关系
    EventLoopGroup workerGroup = new OioEventLoopGroup();
    try {
        // 创建BootstrapServer
        ServerBootstrap b = new ServerBootstrap();
        // 配置boss和worker
        b.group(bossGroup, workerGroup).channel(// 使用阻塞的SocketChannel
        OioServerSocketChannel.class).localAddress(// 绑定端口号
        new InetSocketAddress(port)).handler(// 定义服务器启动的时候经过的状态
        new SimpleServerHandler()).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel socketChannel) throws Exception {
                // 定义worker接收到数据的时候都干嘛
                socketChannel.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    @Override
                    public void channelActive(ChannelHandlerContext ctx) throws Exception {
                        ctx.writeAndFlush(buf.duplicate()).addListener(ChannelFutureListener.CLOSE);
                    }
                });
            }
        });
        // 绑定服务器并接收连接
        ChannelFuture f = b.bind().sync();
        // 等待服务器关闭socket
        f.channel().closeFuture().sync();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        bossGroup.shutdownGracefully().sync();
        workerGroup.shutdownGracefully().sync();
    }
}
Also used : OioServerSocketChannel(io.netty.channel.socket.oio.OioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) OioEventLoopGroup(io.netty.channel.oio.OioEventLoopGroup) InetSocketAddress(java.net.InetSocketAddress) ByteBuf(io.netty.buffer.ByteBuf) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) OioEventLoopGroup(io.netty.channel.oio.OioEventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) OioServerSocketChannel(io.netty.channel.socket.oio.OioServerSocketChannel)

Example 47 with ServerBootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap in project java-in-action by xinghalo.

the class HelloServer method main.

public static void main(String[] args) throws InterruptedException {
    final ByteBuf buffer = Unpooled.unreleasableBuffer(Unpooled.copiedBuffer("I'm ok!\r\n", Charset.forName("UTF-8")));
    NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
    NioEventLoopGroup workerGroup = new NioEventLoopGroup();
    ServerBootstrap bootstrap = new ServerBootstrap();
    try {
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast(new ChannelInboundHandlerAdapter() {

                    @Override
                    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                        System.out.println("Server 接收信息");
                        ByteBuf result = (ByteBuf) msg;
                        System.out.println(ctx.channel().remoteAddress() + " said:" + result.toString(Charset.forName("UTF-8")));
                        result.release();
                        ctx.writeAndFlush(buffer);
                    }
                });
            }
        });
        ChannelFuture f = bootstrap.bind(5555).sync();
        f.channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully().sync();
        workerGroup.shutdownGracefully().sync();
    }
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ByteBuf(io.netty.buffer.ByteBuf) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 48 with ServerBootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap in project java-in-action by xinghalo.

the class NettyNioServerHandlerTest method serve.

public void serve(int port) throws InterruptedException {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(new InetSocketAddress(port)).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel socketChannel) throws Exception {
                ChannelPipeline pipeline = socketChannel.pipeline();
                pipeline.addLast("1", new InboundA());
                pipeline.addLast("2", new OutboundA());
                pipeline.addLast("3", new InboundB());
                pipeline.addLast("4", new OutboundB());
                pipeline.addLast("5", new OutboundC());
                pipeline.addLast("6", new InboundC());
            }
        });
        ChannelFuture f = b.bind().sync();
        f.channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully().sync();
        workerGroup.shutdownGracefully().sync();
    }
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 49 with ServerBootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap in project java-in-action by xinghalo.

the class FixedEchoServer method bind.

private void bind() throws InterruptedException {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    ServerBootstrap b = new ServerBootstrap();
    try {
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(new FixedLengthFrameDecoder(20));
                ch.pipeline().addLast(new StringDecoder());
                ch.pipeline().addLast(new EchoServerHandler());
            }
        });
        ChannelFuture f = b.bind(5555).sync();
        f.channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully().sync();
        workerGroup.shutdownGracefully().sync();
    }
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) FixedLengthFrameDecoder(io.netty.handler.codec.FixedLengthFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Example 50 with ServerBootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap in project BRFS by zhangnianli.

the class NettyHttpServer method start.

@Override
public void start() throws InterruptedException {
    ServerBootstrap serverStart = new ServerBootstrap();
    serverStart.group(bossGroup, workerGroup);
    serverStart.channel(NioServerSocketChannel.class);
    serverStart.childHandler(handlerInitializer);
    // 积压数量
    serverStart.option(ChannelOption.SO_BACKLOG, baklog);
    // 连接超时时间(毫秒)
    serverStart.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, conTimeout);
    // 保持连接
    serverStart.childOption(ChannelOption.SO_KEEPALIVE, true);
    InetSocketAddress address = (ip == null ? new InetSocketAddress(port) : new InetSocketAddress(ip, port));
    channelFuture = serverStart.bind(address).sync();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Aggregations

ServerBootstrap (io.netty.bootstrap.ServerBootstrap)448 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)246 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)239 Channel (io.netty.channel.Channel)187 ChannelFuture (io.netty.channel.ChannelFuture)183 EventLoopGroup (io.netty.channel.EventLoopGroup)168 Bootstrap (io.netty.bootstrap.Bootstrap)134 SocketChannel (io.netty.channel.socket.SocketChannel)126 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)115 InetSocketAddress (java.net.InetSocketAddress)113 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)98 ChannelPipeline (io.netty.channel.ChannelPipeline)88 LoggingHandler (io.netty.handler.logging.LoggingHandler)82 LocalServerChannel (io.netty.channel.local.LocalServerChannel)76 LocalChannel (io.netty.channel.local.LocalChannel)73 CountDownLatch (java.util.concurrent.CountDownLatch)66 Test (org.junit.jupiter.api.Test)66 LocalAddress (io.netty.channel.local.LocalAddress)61 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)60 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)57