| 68 |
ashish |
1 |
package in.shop2020.thrift.clients;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.config.ConfigException;
|
|
|
4 |
import in.shop2020.thrift.clients.config.ConfigClient;
|
|
|
5 |
import in.shop2020.utils.Logger;
|
|
|
6 |
|
|
|
7 |
import org.apache.thrift.protocol.TCompactProtocol;
|
|
|
8 |
import org.apache.thrift.protocol.TProtocol;
|
|
|
9 |
import org.apache.thrift.transport.TFramedTransport;
|
|
|
10 |
import org.apache.thrift.transport.TSocket;
|
|
|
11 |
import org.apache.thrift.transport.TTransport;
|
|
|
12 |
|
|
|
13 |
public abstract class GenericServiceClient {
|
|
|
14 |
|
|
|
15 |
protected String hostname = "localhost";
|
|
|
16 |
protected int port = 8080;
|
|
|
17 |
|
|
|
18 |
protected TSocket socket = null;
|
|
|
19 |
protected TTransport transport = null;
|
|
|
20 |
protected TProtocol protocol = null;
|
|
|
21 |
|
|
|
22 |
private final int SOCKET_TIMEOUT = 1000 * 60 * 5;
|
|
|
23 |
|
|
|
24 |
public GenericServiceClient(String clientIdentifier, String hostConfigKey, String portConfigKey) throws Exception{
|
|
|
25 |
if(clientIdentifier == null||clientIdentifier.equals("")){
|
|
|
26 |
Logger.log("ClientIdentifier is null or empty, cannot initiate client", null);
|
|
|
27 |
}
|
|
|
28 |
this.hostname = getHost(hostConfigKey);
|
|
|
29 |
this.port = getPort(portConfigKey);
|
|
|
30 |
if(hostname == null || hostname.equals("")){
|
|
|
31 |
Logger.log("Hostname null for client :"+ clientIdentifier+ "using localhost", null);
|
|
|
32 |
}else{
|
|
|
33 |
Logger.log("Initializing client for: "+ clientIdentifier+" with host as "+ hostname, null);
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
Logger.log("Initializing socket infra ", this);
|
|
|
37 |
|
|
|
38 |
socket = new TSocket(hostname, port);
|
|
|
39 |
socket.setTimeout(SOCKET_TIMEOUT);
|
|
|
40 |
transport = new TFramedTransport(socket);
|
|
|
41 |
protocol = new TCompactProtocol(transport);
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
private String getHost(String key){
|
|
|
45 |
try {
|
|
|
46 |
String host = ConfigClient.getClient().get(key);
|
|
|
47 |
return host;
|
|
|
48 |
} catch (ConfigException e) {
|
|
|
49 |
Logger.log("Error while fetching hostname for key "+ key+" using localhost:"+ e, this);
|
|
|
50 |
return "localhost";
|
|
|
51 |
}
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
private int getPort(String key){
|
|
|
55 |
try {
|
|
|
56 |
String port = ConfigClient.getClient().get(key);
|
|
|
57 |
return Integer.parseInt(port);
|
|
|
58 |
}catch (NumberFormatException ne){
|
|
|
59 |
Logger.log("Could not convert string to int for key "+ key+" using 8080 as port"+ ne, this);
|
|
|
60 |
return 8080;
|
|
|
61 |
}
|
|
|
62 |
catch (ConfigException e) {
|
|
|
63 |
Logger.log("Error while fetching port for key "+ key+" using 8080:"+ e, this);
|
|
|
64 |
return 8080;
|
|
|
65 |
}
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
}
|