Subversion Repositories SmartDukaan

Rev

Rev 4729 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/**
 * 
 */
package in.shop2020.warehouse.service;

import in.shop2020.thrift.clients.config.ConfigClient;
import in.shop2020.utils.ConfigClientKeys;
import in.shop2020.warehouse.WarehouseService.Iface;
import in.shop2020.warehouse.WarehouseService.Processor;
import in.shop2020.warehouse.service.handler.WarehouseServiceHandler;

import org.apache.commons.daemon.Daemon;
import org.apache.commons.daemon.DaemonContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author mandeep
 *
 */
public class WarehouseServer implements Daemon{
    private static Log log = LogFactory.getLog(WarehouseServer.class);

    private static WarehouseServiceHandler handler;

    private static Processor<Iface> processor;
    private 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.warehouse_service_db_hostname.toString()) + "/warehouse";
            String portNo = ConfigClient.getClient().get(ConfigClientKeys.warehouse_service_server_port.toString());
            int port = Integer.parseInt(portNo);
        
            log.info("Creating context");
            ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
            Iface handler = (Iface)context.getBean("warehouseHandler");
            processor = new Processor<Iface>(handler);
            
                log.info("DB Connection String is: " + dbHost);
                //log.info("URL read by data source before setting is: " + handler.getDataSourceUrl());
                //handler.setDataSourceUrl(dbHost);
                //log.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);
            
            log.info("Warehouse service started on port " + port);
            server.serve();
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
    
    public void destroy() {
        System.out.println("Warehouse Server stopped.");
    }

    public void init(DaemonContext dc) throws Exception {
        System.out.println("Initializing Warehouse Server...");
    }

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

    public void stop() throws Exception {
        log.info("Stopping Warehouse Server...");
        server.stop();
    }
}