Subversion Repositories SmartDukaan

Rev

Rev 4637 | Rev 4719 | Go to most recent revision | 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;

/**
 * @author mandeep
 *
 */
public class WarehouseServer {
    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 {
            int port = 9013;
            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());
                port = Integer.parseInt(portNo);
            } catch(Exception e) {
                log.warn("Unable to get port number from the Config server because of:", e);
            }

            // Setting dbHost env property to make this value accessible in context.xml 
            // while spring context creation.
            System.setProperty("dbHost", dbHost);

            handler = new WarehouseServiceHandler();
            processor = new Processor<Iface>(handler);
            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.");
        // TODO Auto-generated method stub
        
    }

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

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

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