| 5432 |
amar.kumar |
1 |
package in.shop2020.user.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.model.v1.user.UserContextService.Iface;
|
|
|
18 |
import in.shop2020.model.v1.user.UserContextService.Processor;
|
|
|
19 |
import in.shop2020.thrift.clients.config.ConfigClient;
|
|
|
20 |
import in.shop2020.user.handler.UserServiceHandler;
|
|
|
21 |
import in.shop2020.utils.ConfigClientKeys;
|
|
|
22 |
|
|
|
23 |
public class UserServer implements Daemon{
|
|
|
24 |
|
|
|
25 |
private static Logger logger = LoggerFactory.getLogger(UserServer.class);
|
|
|
26 |
private static UserServiceHandler 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.payments_service_db_hostname.toString()) + "/user";
|
|
|
39 |
String portNo = ConfigClient.getClient().get(ConfigClientKeys.user_service_server_port.toString());
|
|
|
40 |
int port = Integer.parseInt(portNo);
|
|
|
41 |
|
|
|
42 |
handler = new UserServiceHandler();
|
|
|
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("User service started on port 9000");
|
|
|
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("User Server stopped.");
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
public void init(DaemonContext dc) throws Exception {
|
|
|
73 |
logger.info("Initializing User Server...");
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
public void start() throws Exception {
|
|
|
77 |
logger.info("Starting User Server");
|
|
|
78 |
initialize();
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
public void stop() throws Exception {
|
|
|
82 |
logger.info("Stopping User Server...");
|
|
|
83 |
server.stop();
|
|
|
84 |
System.exit(0);
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
|
|
|
88 |
|
|
|
89 |
}
|