| 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 |
|
| 193 |
ashish |
7 |
import org.apache.thrift.protocol.TBinaryProtocol;
|
| 68 |
ashish |
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);
|
| 193 |
ashish |
41 |
//protocol = new TCompactProtocol(transport);
|
|
|
42 |
protocol = new TBinaryProtocol(transport);
|
| 68 |
ashish |
43 |
}
|
|
|
44 |
|
|
|
45 |
private String getHost(String key){
|
|
|
46 |
try {
|
|
|
47 |
String host = ConfigClient.getClient().get(key);
|
|
|
48 |
return host;
|
|
|
49 |
} catch (ConfigException e) {
|
|
|
50 |
Logger.log("Error while fetching hostname for key "+ key+" using localhost:"+ e, this);
|
|
|
51 |
return "localhost";
|
|
|
52 |
}
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
private int getPort(String key){
|
|
|
56 |
try {
|
|
|
57 |
String port = ConfigClient.getClient().get(key);
|
|
|
58 |
return Integer.parseInt(port);
|
|
|
59 |
}catch (NumberFormatException ne){
|
|
|
60 |
Logger.log("Could not convert string to int for key "+ key+" using 8080 as port"+ ne, this);
|
| 767 |
rajveer |
61 |
return 9009;
|
| 68 |
ashish |
62 |
}
|
|
|
63 |
catch (ConfigException e) {
|
|
|
64 |
Logger.log("Error while fetching port for key "+ key+" using 8080:"+ e, this);
|
| 767 |
rajveer |
65 |
return 9009;
|
| 68 |
ashish |
66 |
}
|
|
|
67 |
}
|
| 767 |
rajveer |
68 |
|
|
|
69 |
public abstract void closeSession();
|
|
|
70 |
|
|
|
71 |
public void closeConnection(){
|
|
|
72 |
if(transport != null && transport.isOpen()){
|
| 1563 |
rajveer |
73 |
//Removed to prevent socket connection closed exception
|
| 1568 |
rajveer |
74 |
///this.closeSession();
|
| 767 |
rajveer |
75 |
Logger.log("Closing transport :", this);
|
|
|
76 |
transport.close();
|
|
|
77 |
}
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
protected void finalize() throws Throwable{
|
|
|
81 |
super.finalize();
|
|
|
82 |
this.closeConnection();
|
|
|
83 |
}
|
|
|
84 |
|
| 68 |
ashish |
85 |
}
|