Subversion Repositories SmartDukaan

Rev

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