com.dingwang.netty.server.PersonServer.java Source code

Java tutorial

Introduction

Here is the source code for com.dingwang.netty.server.PersonServer.java

Source

/*
 * Copyright 2016 Zhongan.com All right reserved. This software is the
 * confidential and proprietary information of Zhongan.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Zhongan.com.
 */
package com.dingwang.netty.server;

import com.dingwang.netty.decoder.PersonDecoder;
import com.dingwang.netty.encoder.PersonEncoder;
import com.dingwang.netty.handler.PersonOutHandler;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * DiscardServer.java??TODO ??<br>
 * <a href="http://ifeve.com/netty5-user-guide/">netty</a>
 * 
 * @author wangding_91@163.com 2016218 ?3:08:27
 */
public class PersonServer {

    //??
    private int port;

    public PersonServer(int port) {
        this.port = port;
    }

    public void run() {
        //NioEventLoopGroup ??I/O?Netty????EventLoopGroup
        //????????2NioEventLoopGroup
        //???boss?????worker???
        //boss?worker???
        //Channels??EventLoopGroup???
        EventLoopGroup bossGroup = new NioEventLoopGroup(5);
        EventLoopGroup workerGroup = new NioEventLoopGroup(20);

        //ServerBootstrap ?NIO????Channel??
        //????
        ServerBootstrap b = new ServerBootstrap();

        //NioServerSocketChannel?Channel?
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                //?????ChannelChannelInitializer?
                //?Channel?DiscardServerHandle??
                //ChannelChannelPipeline???????
                //?pipline??????
                .childHandler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new PersonDecoder(), new PersonEncoder(), new PersonOutHandler());

                    }
                })
                //????TCP/IP??socket?
                //tcpNoDelaykeepAlive?ChannelOptionChannelConfig??
                //ChannelOptions
                .option(ChannelOption.SO_BACKLOG, 128)
                //option()childOption()?option()??NioServerSocketChannel??
                //childOption()???ServerChannel?NioServerSocketChannel
                .childOption(ChannelOption.SO_KEEPALIVE, true);

        try {
            //?????8080?
            //?bind()(???)
            ChannelFuture f = b.bind(port).sync();
            f.channel().closeFuture().sync();

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }

    }
}