Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5519 amar.kumar 1
package in.shop2020.alert.service;
2
 
3
import org.apache.commons.daemon.Daemon;
4
import org.apache.commons.daemon.DaemonContext;
5
import org.apache.thrift.protocol.TBinaryProtocol;
6
import org.apache.thrift.protocol.TProtocolFactory;
7
import org.apache.thrift.server.TServer;
8
import org.apache.thrift.server.TThreadPoolServer;
9
import org.apache.thrift.server.TThreadPoolServer.Args;
10
import org.apache.thrift.transport.TFramedTransport;
11
import org.apache.thrift.transport.TServerSocket;
12
import org.apache.thrift.transport.TServerTransport;
13
import org.apache.thrift.transport.TTransportFactory;
14
import org.slf4j.Logger;
15
import org.slf4j.LoggerFactory;
16
 
17
import in.shop2020.thrift.clients.config.ConfigClient;
18
import in.shop2020.alert.AlertService.Iface;
19
import in.shop2020.alert.AlertService.Processor;
20
import in.shop2020.alert.handler.AlertServiceHandler;
21
import in.shop2020.utils.ConfigClientKeys;
22
 
23
public class AlertServer implements Daemon{
24
 
25
	private static Logger logger = LoggerFactory.getLogger(AlertServer.class);
26
	private static AlertServiceHandler handler;
27
	private static Processor<Iface> processor;
28
	public static String dbHost;
29
	private static TServer server;
30
 
31
	public static void main(String[] args) {
32
	    initialize();
33
	}
34
 
35
	private static void initialize() {
36
		try{
37
 
38
		    dbHost = "jdbc:mysql://" + ConfigClient.getClient().get(ConfigClientKeys.alert_service_db_hostname.toString()) + "/monitor";
39
		    String portNo = ConfigClient.getClient().get(ConfigClientKeys.alert_service_server_port.toString());
40
		    int port = Integer.parseInt(portNo);
41
 
42
	        handler = new AlertServiceHandler();
43
	        processor = new Processor<Iface>(handler);
44
 
45
	        logger.info("DB Connection String is: " + dbHost);
46
	        logger.info("URL read by data source before setting is: " + handler.getDataSourceUrl());
47
	        handler.setDataSourceUrl(dbHost);
48
	        logger.info("URL read by data source after setting is: " + handler.getDataSourceUrl());
49
 
50
			TServerTransport serverTransport = new TServerSocket(port);
51
			TTransportFactory tFactory = new TFramedTransport.Factory();
52
			TProtocolFactory pFactory = new TBinaryProtocol.Factory();
53
 
54
			Args serverParams = new Args(serverTransport);
55
			serverParams.processor(processor);
56
			serverParams.transportFactory(tFactory);
57
			serverParams.protocolFactory(pFactory);
58
			server = new TThreadPoolServer(serverParams);
59
 
60
			logger.info("Alert service started on port 9018");
61
			server.serve();
62
		}catch(Exception ex){
63
			logger.error("Unable to get port number from the Config server because of:", ex);
64
		}
65
	}
66
 
67
 
68
	public void destroy() {
69
        logger.info("Alert Server stopped.");
70
    }
71
 
72
    public void init(DaemonContext dc) throws Exception {
73
        logger.info("Initializing Alert Server...");
74
    }
75
 
76
    public void start() throws Exception {
77
        logger.info("Starting Alert Server");
78
        initialize();
79
    }
80
 
81
    public void stop() throws Exception {
82
        logger.info("Stopping Alert Server...");
83
        server.stop();
84
        System.exit(0);
85
    }
86
 
87
 
88
 
89
}