Subversion Repositories SmartDukaan

Rev

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

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

import in.shop2020.crm.CRMService.Iface;
import in.shop2020.crm.CRMService.Processor;
import in.shop2020.crm.service.handler.CRMServiceHandler;
import in.shop2020.thrift.clients.config.ConfigClient;
import in.shop2020.utils.ConfigClientKeys;

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;

/**
 * Service for accessing all necessary data relevant for CRM tool.
 * Example tickets, activities, agents etc.
 *
 * @author mandeep
 */
public class CRMServer implements Daemon {
    private static Logger            logger = LoggerFactory.getLogger(CRMServer.class);

    private static CRMServiceHandler handler;

    private static Processor<Iface>       processor;
    
    private static TServer server;
    
    public static void main(String[] args) {
        initialize();
    }

    private static void initialize() {
        try {
            handler = new CRMServiceHandler();
                        processor = new Processor<Iface>(handler);
            int port = 9014;
            try {
                String portNo = ConfigClient.getClient().get(
                        ConfigClientKeys.crm_service_server_port
                                .toString());
                port = Integer.parseInt(portNo);
            } catch (Exception e) {
                logger.warn(
                        "Unable to get port number from the Config server because of:",
                        e);
            }

            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("CRM service started on port 9014");
            server.serve();
        } catch (Exception ex) {
            logger.error("Error starting server", ex);
        }
    }

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

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

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

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