Blame | Last modification | View Log | RSS feed
package in.shop2020.alert.service;import org.apache.commons.daemon.Daemon;import org.apache.commons.daemon.DaemonContext;import org.apache.thrift.protocol.TBinaryProtocol;import org.apache.thrift.protocol.TProtocolFactory;import org.apache.thrift.server.TServer;import org.apache.thrift.server.TThreadPoolServer;import org.apache.thrift.server.TThreadPoolServer.Args;import org.apache.thrift.transport.TFramedTransport;import org.apache.thrift.transport.TServerSocket;import org.apache.thrift.transport.TServerTransport;import org.apache.thrift.transport.TTransportFactory;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import in.shop2020.thrift.clients.config.ConfigClient;import in.shop2020.alert.AlertService.Iface;import in.shop2020.alert.AlertService.Processor;import in.shop2020.alert.handler.AlertServiceHandler;import in.shop2020.utils.ConfigClientKeys;public class AlertServer implements Daemon{private static Logger logger = LoggerFactory.getLogger(AlertServer.class);private static AlertServiceHandler handler;private static Processor<Iface> processor;public static String dbHost;private static TServer server;public static void main(String[] args) {initialize();}private static void initialize() {try{dbHost = "jdbc:mysql://" + ConfigClient.getClient().get(ConfigClientKeys.alert_service_db_hostname.toString()) + "/monitor";String portNo = ConfigClient.getClient().get(ConfigClientKeys.alert_service_server_port.toString());int port = Integer.parseInt(portNo);handler = new AlertServiceHandler();processor = new Processor<Iface>(handler);logger.info("DB Connection String is: " + dbHost);logger.info("URL read by data source before setting is: " + handler.getDataSourceUrl());handler.setDataSourceUrl(dbHost);logger.info("URL read by data source after setting is: " + handler.getDataSourceUrl());TServerTransport serverTransport = new TServerSocket(port);TTransportFactory tFactory = new TFramedTransport.Factory();TProtocolFactory pFactory = new TBinaryProtocol.Factory();Args serverParams = new Args(serverTransport);serverParams.processor(processor);serverParams.transportFactory(tFactory);serverParams.protocolFactory(pFactory);server = new TThreadPoolServer(serverParams);logger.info("Alert service started on port 9018");server.serve();}catch(Exception ex){logger.error("Unable to get port number from the Config server because of:", ex);}}public void destroy() {logger.info("Alert Server stopped.");}public void init(DaemonContext dc) throws Exception {logger.info("Initializing Alert Server...");}public void start() throws Exception {logger.info("Starting Alert Server");initialize();}public void stop() throws Exception {logger.info("Stopping Alert Server...");server.stop();System.exit(0);}}