001package net.gdface.cli;
002
003import org.apache.commons.cli.CommandLine;
004import org.apache.commons.cli.Option;
005import org.apache.commons.cli.Options;
006import org.apache.commons.cli.ParseException;
007
008/**
009 * 服务基本配置参数
010 * @author guyadong
011 *
012 */
013public abstract class ThriftServiceConfig extends BaseAppConfig implements ThriftServiceConstants {
014
015        /**
016         * 服务端口号
017         */
018        private int servicePort;
019        private int workThreads;
020        private int connectionLimit;
021        private int idleConnectionTimeout;
022        /**
023         *  默认服务端口号
024         * @param defaultPort
025         */
026        public ThriftServiceConfig(int defaultPort) {
027                if(defaultPort<=0){
028                        throw new IllegalArgumentException(String.format("invalid defaultPort %d",defaultPort));
029                }
030                options.addOption(Option.builder().longOpt(SERVICE_PORT_OPTION_LONG)
031                                .desc(SERVICE_PORT_OPTION_DESC + defaultPort).numberOfArgs(1).type(Number.class).build());
032                options.addOption(Option.builder().longOpt(WORK_THREADS_OPTION_LONG)
033                                .desc(WORK_THREADS_OPTION_DESC).numberOfArgs(1).type(Number.class).build());
034                options.addOption(Option.builder().longOpt(CONNECTION_LIMIT_OPTION_LONG)
035                                .desc(CONNECTION_LIMIT_OPTION_DESC).numberOfArgs(1).type(Number.class).build());
036                options.addOption(Option.builder().longOpt(IDLE_CONNECTION_TIMEOUT_OPTION_LONG)
037                                .desc(IDLE_CONNECTION_TIMEOUT_OPTION_DESC).numberOfArgs(1).type(Number.class).build());
038
039                defaultValue.setProperty(SERVICE_PORT_OPTION_LONG, defaultPort);
040                defaultValue.setProperty(WORK_THREADS_OPTION_LONG, Runtime.getRuntime().availableProcessors());
041                defaultValue.setProperty(CONNECTION_LIMIT_OPTION_LONG, DEFAULT_CONNECTION_LIMIT);
042                defaultValue.setProperty(IDLE_CONNECTION_TIMEOUT_OPTION_LONG, DEFAULT_IDLE_TIMEOUT);
043        }
044        @Override
045        public void loadConfig(Options options, CommandLine cmd) throws ParseException {
046                super.loadConfig(options, cmd);
047                this.servicePort = ((Number)getProperty(SERVICE_PORT_OPTION_LONG)).intValue(); 
048                this.workThreads = ((Number)getProperty(WORK_THREADS_OPTION_LONG)).intValue(); 
049                this.connectionLimit = ((Number)getProperty(CONNECTION_LIMIT_OPTION_LONG)).intValue();          
050                this.idleConnectionTimeout = ((Number)getProperty(IDLE_CONNECTION_TIMEOUT_OPTION_LONG)).intValue();
051        }
052        /**
053         * @return 服务端口号
054         */
055        public int getServicePort() {
056                return servicePort;
057        }
058
059        /**
060         * @return 工作线程数量
061         */
062        public int getWorkThreads() {
063                return workThreads;
064        }
065
066        /**
067         * @return 连接上限
068         */
069        public int getConnectionLimit() {
070                return connectionLimit;
071        }
072
073        /**
074         * 
075         * @return 空间连接超时(秒)
076         */
077        public int getIdleConnectionTimeout() {
078                return idleConnectionTimeout;
079        }
080        
081}