All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gateway.connector.tcp.server.TServer Maven / Gradle / Ivy

 
package com.gateway.connector.tcp.server;

import com.app.common.config.PropertiesHelper;
import com.gateway.connector.tcp.config.ServerTransportConfig;
import com.gateway.connector.utils.LicenseUtil;
import com.gateway.utils.ThreadNameFactory;
import com.gateway.exception.InitErrorException;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

 
public class TServer {

    private final static Logger logger = LoggerFactory.getLogger(TServer.class);

    private ServerTransportConfig serverConfig;

    public ServerTransportConfig getServerConfig() {
		return serverConfig;
	}

	private int port;

    public int getPort() {
		return port;
	}

	private static final int BIZ_GROUP_SIZE = Runtime.getRuntime().availableProcessors() * 2;
    private static final int BIZ_THREAD_SIZE = Runtime.getRuntime().availableProcessors() *4;

    private final EventLoopGroup bossGroup = new NioEventLoopGroup(BIZ_GROUP_SIZE,new ThreadNameFactory(this.getClass().getSimpleName()+" Boss"));
    private final EventLoopGroup workerGroup = new NioEventLoopGroup(BIZ_THREAD_SIZE,new ThreadNameFactory(this.getClass().getSimpleName()+" Worker"));

    public void init() throws Exception {
        logger.info("start tcp server ...");
		LicenseUtil.license_path=PropertiesHelper.getFullPath(PropertiesHelper.getATS_ROOT(),  "/control/dc.dat");
		LicenseUtil.install();
        Class clazz = NioServerSocketChannel.class;
        // Server 
        ServerBootstrap bootstrap = new ServerBootstrap();

        bootstrap.group(bossGroup, workerGroup);
        bootstrap.channel(clazz);
        bootstrap.childHandler(new SChannelInitializer(serverConfig));
        // 
        bootstrap  .option(ChannelOption.SO_REUSEADDR, Boolean.TRUE)
     
          
        .childOption(ChannelOption.TCP_NODELAY, true)
        .childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(false));  // heap buf 's better
        // ,
        logger.info("start tcp server at port[" + port + "].");
        ChannelFuture future = bootstrap.bind(port).sync();
        ChannelFuture channelFuture = future.addListener(new ChannelFutureListener() {
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()) {
                    logger.info("tcp server have success bind to " + port);
                } else {
                    logger.error("tcp server fail bind to " + port);
                    throw new InitErrorException("tcp server start fail !", future.cause());
                }
            }
        });
    }

    public void shutdown() {
        logger.info("shutdown tcp server ...");
        // 
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
        logger.info("shutdown tcp server end.");
    }

    //------------------ set && get --------------------


    public void setServerConfig(ServerTransportConfig serverConfig) {
        this.serverConfig = serverConfig;
    }

    public void setPort(int port) {
        this.port = port;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy