Subversion Repositories SmartDukaan

Rev

Rev 4718 | Rev 4729 | 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 {
4574 mandeep.dh 46
            int port = 9013;
4500 mandeep.dh 47
            try {
4637 mandeep.dh 48
                dbHost = "jdbc:mysql://" + ConfigClient.getClient().get(ConfigClientKeys.warehouse_service_db_hostname.toString()) + "/warehouse";
4500 mandeep.dh 49
                String portNo = ConfigClient.getClient().get(ConfigClientKeys.warehouse_service_server_port.toString());
50
                port = Integer.parseInt(portNo);
51
            } catch(Exception e) {
52
                log.warn("Unable to get port number from the Config server because of:", e);
53
            }
54
 
4637 mandeep.dh 55
            // Setting dbHost env property to make this value accessible in context.xml 
56
            // while spring context creation.
57
            System.setProperty("dbHost", dbHost);
58
 
59
            handler = new WarehouseServiceHandler();
60
            processor = new Processor<Iface>(handler);
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
        // TODO Auto-generated method stub
81
 
82
    }
83
 
84
    public void init(DaemonContext dc) throws Exception {
85
        System.out.println("Initializing Warehouse Server...");
86
        // TODO Auto-generated method stub
87
 
88
    }
89
 
90
    public void start() throws Exception {
91
        System.out.println("Starting Warehouse Server");
92
        initialize();
93
        // TODO Auto-generated method stub
94
 
95
    }
96
 
97
    public void stop() throws Exception {
98
        System.out.println("Stopping Warehouse Server...");
99
        server.stop();
100
        System.exit(0);
101
        // TODO Auto-generated method stub
102
 
103
    }
4500 mandeep.dh 104
}