Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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.TCompactProtocol;
9
import org.apache.thrift.protocol.TProtocol;
10
import org.apache.thrift.transport.TFramedTransport;
11
import org.apache.thrift.transport.TSocket;
12
import org.apache.thrift.transport.TTransport;
13
 
14
public abstract class GenericServiceClient {
15
 
16
	protected String hostname = "localhost";
17
	protected int port = 8080;
18
 
19
	protected TSocket socket = null;
20
	protected TTransport transport = null;
21
	protected TProtocol protocol = null;
22
 
23
	private final int SOCKET_TIMEOUT = 1000 * 60 * 5;
24
 
25
	public GenericServiceClient(String clientIdentifier, String hostConfigKey, String portConfigKey) throws Exception{
26
		if(clientIdentifier == null||clientIdentifier.equals("")){
27
			Logger.log("ClientIdentifier is null or empty, cannot initiate client", null);
28
		}
29
		this.hostname = getHost(hostConfigKey);
30
		this.port = getPort(portConfigKey);
31
		if(hostname == null || hostname.equals("")){
32
			Logger.log("Hostname null for client :"+ clientIdentifier+ "using localhost", null);
33
		}else{
34
			Logger.log("Initializing client for: "+ clientIdentifier+" with host as "+ hostname, null);
35
		}
36
 
37
		Logger.log("Initializing socket infra ", this);
38
 
39
		socket = new TSocket(hostname, port);
40
		socket.setTimeout(SOCKET_TIMEOUT);
41
		transport = new TFramedTransport(socket);
193 ashish 42
		//protocol = new TCompactProtocol(transport);
43
		protocol = new TBinaryProtocol(transport);
68 ashish 44
	}
45
 
46
	private String getHost(String key){
47
		try {
48
			String host = ConfigClient.getClient().get(key);
49
			return host;
50
		} catch (ConfigException e) {
51
			Logger.log("Error while fetching hostname for key "+ key+" using localhost:"+ e, this);
52
			return "localhost";
53
		}
54
	}
55
 
56
	private int getPort(String key){
57
		try {
58
			String port = ConfigClient.getClient().get(key);
59
			return Integer.parseInt(port);
60
		}catch (NumberFormatException ne){
61
			Logger.log("Could not convert string to int for key "+ key+" using 8080 as port"+ ne, this);
767 rajveer 62
			return 9009;
68 ashish 63
		}
64
		catch (ConfigException e) {
65
			Logger.log("Error while fetching port for key "+ key+" using 8080:"+ e, this);
767 rajveer 66
			return 9009;
68 ashish 67
		}
68
	}
767 rajveer 69
 
70
	public abstract void closeSession();
71
 
72
	public void closeConnection(){
73
		if(transport != null && transport.isOpen()){
1563 rajveer 74
			//Removed to prevent socket connection closed exception
1568 rajveer 75
			///this.closeSession();
767 rajveer 76
			Logger.log("Closing transport :", this);
77
			transport.close();
78
		}
79
	}
80
 
81
	protected void finalize() throws Throwable{
82
		super.finalize();
83
		this.closeConnection();
84
	}
85
 
68 ashish 86
}