Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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