com.mc.netty.server.NettyServer.java Source code

Java tutorial

Introduction

Here is the source code for com.mc.netty.server.NettyServer.java

Source

/*
 * Copyright 2015-2020 wuage.com All right reserved. This software is the confidential and proprietary information of
 * Alibaba.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 Wuage.com.
 */
package com.mc.netty.server;

import com.mc.netty.handler.TcpChannelInitializer;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.AdaptiveRecvByteBufAllocator;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * NettyServer.java??TODO ??
 * 
 * @author macun 201735 ?8:26:57
 */
public class NettyServer {

    private final NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
    private final NioEventLoopGroup workerGroup = new NioEventLoopGroup();
    private int port = 8081;
    private String localHost = "127.0.0.1";
    private String serverUrl; // host:port
    private Channel channle;

    private ServerBootstrap getDefaultServerBootstrap() {
        ServerBootstrap bootStrap = new ServerBootstrap();
        bootStrap.group(bossGroup, workerGroup).option(ChannelOption.SO_BACKLOG, 1000)
                // ???
                .option(ChannelOption.SO_SNDBUF, 32 * 1024).option(ChannelOption.SO_RCVBUF, 32 * 1024)
                .option(ChannelOption.TCP_NODELAY, true)
                // ???
                .option(ChannelOption.RCVBUF_ALLOCATOR, AdaptiveRecvByteBufAllocator.DEFAULT)
                .channel(NioServerSocketChannel.class).childOption(ChannelOption.SO_KEEPALIVE, true);

        return bootStrap;
    }

    public ChannelFuture start() throws InterruptedException {
        // ??ip
        ChannelFuture future = getDefaultServerBootstrap().childHandler(new TcpChannelInitializer()).bind(port)
                .sync();
        // ServerManager.pubServer(serverUrl);
        System.out.println("steelIm???,??" + port + '.');
        channle = future.channel();
        return future;
    }

    public void destroy() {
        System.out.println("steelIm?");
        if (channle != null) {
            channle.close();
        }
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
        System.out.println("steelIm??");
    }

}