uidserver.UIDServer.java Source code

Java tutorial

Introduction

Here is the source code for uidserver.UIDServer.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package uidserver;

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;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.SelfSignedCertificate;

/**
 *
 * @author ghfan
 */
public class UIDServer {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) throws Exception {
        if (args.length == 1) {
            if (args[0].toLowerCase().contains("help")) {
                System.out.println("This script can only run in Java 1.8");
                System.out.println("java -jar ./UIDServer.jar " + "./config.xml");
                System.exit(0);
            } else {
                new Config(args[0]);
            }
        } else {
            new Config("D:/projects/UIDTest/server/config.xml");
        }
        new UIDGenerator();
        final int PORT = Integer.parseInt(System.getProperty("port", Config.port));
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        SslContext sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();

        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                    .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new UIDServerInitializer(sslCtx));

            b.bind(PORT).sync().channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}