Subversion Repositories SmartDukaan

Rev

Rev 4651 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.payment.service;

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 org.apache.commons.daemon.Daemon;
import org.apache.commons.daemon.DaemonContext;

import in.shop2020.payment.service.handler.PaymentServiceHandler;
import in.shop2020.payments.PaymentService.Iface;
import in.shop2020.payments.PaymentService.Processor;
import in.shop2020.thrift.clients.config.ConfigClient;
import in.shop2020.utils.ConfigClientKeys;

public class PaymentServer implements Daemon {
        private static Logger logger = LoggerFactory.getLogger(PaymentServer.class);
        
        private static PaymentServiceHandler 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()) + "/payment";
                    String portNo = ConfigClient.getClient().get(ConfigClientKeys.payments_service_server_port.toString());
                    int port = Integer.parseInt(portNo);
                    
                handler = new PaymentServiceHandler();
                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("Payment service started on port 9012");
                        server.serve();
                }catch(Exception ex){
                        logger.error("Unable to get port number from the Config server because of:", ex);
                }
        }

    public void destroy() {
        System.out.println("Payment Server stopped.");
        // TODO Auto-generated method stub
        
    }

    public void init(DaemonContext dc) throws Exception {
        System.out.println("Initializing Payment Server...");
        // TODO Auto-generated method stub
        
    }

    public void start() throws Exception {
        System.out.println("Starting Payment Server");
        initialize();
        // TODO Auto-generated method stub
        
    }

    public void stop() throws Exception {
        System.out.println("Stopping Payment Server...");
        server.stop();
        System.exit(0);
        // TODO Auto-generated method stub
        
    }
}