Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
4500 mandeep.dh 1
/**
2
 * 
3
 */
4
package in.shop2020.warehouse.service;
5
 
6
import in.shop2020.thrift.clients.config.ConfigClient;
7
import in.shop2020.utils.ConfigClientKeys;
8
import in.shop2020.warehouse.WarehouseService.Iface;
9
import in.shop2020.warehouse.WarehouseService.Processor;
10
import in.shop2020.warehouse.service.handler.WarehouseServiceHandler;
11
 
4718 anupam.sin 12
import org.apache.commons.daemon.Daemon;
13
import org.apache.commons.daemon.DaemonContext;
4500 mandeep.dh 14
import org.apache.commons.logging.Log;
15
import org.apache.commons.logging.LogFactory;
16
import org.apache.thrift.protocol.TBinaryProtocol;
17
import org.apache.thrift.protocol.TProtocolFactory;
18
import org.apache.thrift.server.TServer;
19
import org.apache.thrift.server.TThreadPoolServer;
20
import org.apache.thrift.server.TThreadPoolServer.Args;
21
import org.apache.thrift.transport.TFramedTransport;
22
import org.apache.thrift.transport.TServerSocket;
23
import org.apache.thrift.transport.TServerTransport;
24
import org.apache.thrift.transport.TTransportFactory;
21617 amit.gupta 25
import org.springframework.context.ApplicationContext;
26
import org.springframework.context.support.ClassPathXmlApplicationContext;
4500 mandeep.dh 27
 
28
/**
29
 * @author mandeep
30
 *
31
 */
4719 anupam.sin 32
public class WarehouseServer implements Daemon{
4500 mandeep.dh 33
    private static Log log = LogFactory.getLog(WarehouseServer.class);
34
 
35
    private static WarehouseServiceHandler handler;
36
 
37
    private static Processor<Iface> processor;
4637 mandeep.dh 38
    private static String dbHost;
4718 anupam.sin 39
    private static TServer server;
4500 mandeep.dh 40
 
41
    public static void main(String[] args){
4718 anupam.sin 42
        initialize();
43
    }
44
 
45
    private static void initialize() {
4500 mandeep.dh 46
        try {
4729 rajveer 47
 
48
            dbHost = "jdbc:mysql://" + ConfigClient.getClient().get(ConfigClientKeys.warehouse_service_db_hostname.toString()) + "/warehouse";
49
            String portNo = ConfigClient.getClient().get(ConfigClientKeys.warehouse_service_server_port.toString());
50
            int port = Integer.parseInt(portNo);
51
 
21617 amit.gupta 52
            log.info("Creating context");
53
            ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
54
            Iface handler = (Iface)context.getBean("warehouseHandler");
4637 mandeep.dh 55
            processor = new Processor<Iface>(handler);
4729 rajveer 56
 
57
	        log.info("DB Connection String is: " + dbHost);
21617 amit.gupta 58
	        //log.info("URL read by data source before setting is: " + handler.getDataSourceUrl());
59
	        //handler.setDataSourceUrl(dbHost);
60
	        //log.info("URL read by data source after setting is: " + handler.getDataSourceUrl());
4729 rajveer 61
 
4500 mandeep.dh 62
            TServerTransport serverTransport = new TServerSocket(port);
63
            TTransportFactory tFactory = new TFramedTransport.Factory();
64
            TProtocolFactory pFactory = new TBinaryProtocol.Factory();
65
 
66
            Args serverParams = new Args(serverTransport);
67
            serverParams.processor(processor);
68
            serverParams.transportFactory(tFactory);
69
            serverParams.protocolFactory(pFactory);
4718 anupam.sin 70
            server = new TThreadPoolServer(serverParams);
4500 mandeep.dh 71
 
72
            log.info("Warehouse service started on port " + port);
73
            server.serve();
74
        }catch(Exception ex){
75
            ex.printStackTrace();
76
        }
77
    }
4718 anupam.sin 78
 
79
    public void destroy() {
80
        System.out.println("Warehouse Server stopped.");
81
    }
82
 
83
    public void init(DaemonContext dc) throws Exception {
84
        System.out.println("Initializing Warehouse Server...");
85
    }
86
 
87
    public void start() throws Exception {
4729 rajveer 88
    	log.info("Starting Warehouse Server");
4718 anupam.sin 89
        initialize();
90
    }
91
 
92
    public void stop() throws Exception {
4729 rajveer 93
        log.info("Stopping Warehouse Server...");
4718 anupam.sin 94
        server.stop();
95
    }
4500 mandeep.dh 96
}