com.lxd.server.AdamServer.java Source code

Java tutorial

Introduction

Here is the source code for com.lxd.server.AdamServer.java

Source

/*
 * Copyright (C) 2014 a5834099147(lxd) <a5834099147@126.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.lxd.server;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.lxd.secondary.MessageSend;
import com.lxd.server.link.ServerInitalizer;
import com.lxd.server.secondary.MsgInPre;
import com.lxd.server.secondary.UploadFile;
import com.lxd.threadpool.impl.TaskThreadPool;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

/**
 * ??
 * @author: a5834099147
 * @mailto: a5834099147@126.com
 * @date: 20141216
 * @blog : http://a5834099147.github.io/
 * @review 
 */
public class AdamServer {
    private static final int PORT = 8463;

    public static void main(String[] args) {
        new AdamServer().initServer();
    }

    private static final Logger log = LogManager.getLogger(AdamServer.class);

    public void initServer() {

        ///<        
        new TaskThreadPool().start();
        log.info("??");
        ///< ??      
        new MsgInPre().start();
        log.info("????");
        ///< ???        
        new MessageSend().start();
        log.info("?????");
        ///< ??
        new UploadFile().start();
        ///< 
        if (!netStart(PORT)) {
            return;
        }
    }

    private boolean netStart(int prot) {
        ///< ?
        EventLoopGroup listenGroup = new NioEventLoopGroup(1);
        ///< 
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap bootstrap = new ServerBootstrap();
            ///< ??
            bootstrap.group(listenGroup, workerGroup).channel(NioServerSocketChannel.class)
                    ///< , ?? INFO
                    .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ServerInitalizer());

            ///< ?
            try {
                bootstrap.bind(prot).sync().channel().closeFuture().sync();
            } catch (InterruptedException e) {
                e.printStackTrace();
                return false;
            }
        } finally {
            ///< 
            try {
                listenGroup.shutdownGracefully().sync();
                workerGroup.shutdownGracefully().sync();
            } catch (InterruptedException e) {
                e.printStackTrace();
                return false;
            }
        }

        return true;
    }
}