Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

package in.shop2020.user.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.model.v1.user.UserContextService.Iface;
import in.shop2020.model.v1.user.UserContextService.Processor;
import in.shop2020.thrift.clients.config.ConfigClient;
import in.shop2020.user.handler.UserServiceHandler;
import in.shop2020.utils.ConfigClientKeys;

public class UserServer implements Daemon{

        private static Logger logger = LoggerFactory.getLogger(UserServer.class);
        private static UserServiceHandler 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.payments_service_db_hostname.toString()) + "/user";
                    String portNo = ConfigClient.getClient().get(ConfigClientKeys.user_service_server_port.toString());
                    int port = Integer.parseInt(portNo);
                    
                handler = new UserServiceHandler();
                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("User service started on port 9000");
                        server.serve();
                }catch(Exception ex){
                        logger.error("Unable to get port number from the Config server because of:", ex);
                }
        }


        public void destroy() {
        logger.info("User Server stopped.");
    }

    public void init(DaemonContext dc) throws Exception {
        logger.info("Initializing User Server...");
    }

    public void start() throws Exception {
        logger.info("Starting User Server");
        initialize();
    }

    public void stop() throws Exception {
        logger.info("Stopping User Server...");
        server.stop();
        System.exit(0);
    }

        
        
}