Subversion Repositories SmartDukaan

Rev

Rev 3123 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
68 ashish 1
package in.shop2020.thrift.clients;
2
 
3123 rajveer 3
import javax.annotation.PostConstruct;
4
import javax.annotation.PreDestroy;
5
 
68 ashish 6
import in.shop2020.config.ConfigException;
7
import in.shop2020.thrift.clients.config.ConfigClient;
8
import in.shop2020.utils.Logger;
9
 
193 ashish 10
import org.apache.thrift.protocol.TBinaryProtocol;
68 ashish 11
import org.apache.thrift.protocol.TProtocol;
12
import org.apache.thrift.transport.TFramedTransport;
13
import org.apache.thrift.transport.TSocket;
14
import org.apache.thrift.transport.TTransport;
3123 rajveer 15
import org.apache.thrift.transport.TTransportException;
68 ashish 16
 
3123 rajveer 17
public abstract class GenericClient {
18
	/**
19
	 * Socket timeout to be set for thrift
20
	 */
21
	private final int SOCKET_TIMEOUT = 1000 * 60 * 5;
22
	protected TSocket socket = null;
23
	protected TTransport transport = null;
24
	protected TProtocol protocol = null;
68 ashish 25
 
3123 rajveer 26
	/**
27
	 * Max attempts on one instance of service to be tried before falling on next instance of service
28
	 */
29
	protected static final int MAX_ATTEMPTS = 3;
30
	protected static final int FIRST_ATTEMPT = 1;
31
	/**
32
	 * Max instances of service to be tried before dying
33
	 */
34
	protected static final int TOTAL_SERVICES = 2;
35
	/**
36
	 * Current service instance being used
37
	 */
38
	protected int serviceNumber = 0;
68 ashish 39
	protected String hostname = "localhost";
3183 rajveer 40
	protected int port = 0;
68 ashish 41
 
3123 rajveer 42
	protected String hostConfigKey;
43
	protected String portConfigKey;
44
 
68 ashish 45
 
46
 
3123 rajveer 47
	/**
48
	 * Constructor
49
	 * @param hostConfigKey
50
	 * @param portConfigKey
51
	 * @throws TTransportException
52
	 */
53
	public GenericClient(String hostConfigKey, String portConfigKey) throws TTransportException{
54
		this.hostConfigKey = hostConfigKey; 
55
		this.portConfigKey = portConfigKey;
3183 rajveer 56
		try {
57
			loadConfigParameters(hostConfigKey, portConfigKey);
58
			connectToService(FIRST_ATTEMPT);
59
		} catch (ConfigException e) {
60
			Logger.log("Config exception", this);
61
			throw new TTransportException("Unable to get parameters from config server.");
62
		}
3123 rajveer 63
	}
64
 
65
	/**
66
	 * Load config parameters for the given key set
67
	 * @param hostConfigKey
68
	 * @param portConfigKey
69
	 */
3183 rajveer 70
	private void loadConfigParameters(String hostConfigKey, String  portConfigKey) throws ConfigException{
68 ashish 71
		this.hostname = getHost(hostConfigKey);
72
		this.port = getPort(portConfigKey);
73
	}
74
 
3123 rajveer 75
 
76
	/**
77
	 * get the Host name from given key
78
	 * @param key
79
	 * @return
3183 rajveer 80
	 * @throws ConfigException 
3123 rajveer 81
	 */
3183 rajveer 82
	private String getHost(String key) throws ConfigException{
83
		String host = ConfigClient.getClient().get(key);
84
		return host;
68 ashish 85
	}
86
 
3123 rajveer 87
	/**
88
	 * get the Port from given key
89
	 * @param key
90
	 * @return
3183 rajveer 91
	 * @throws ConfigException 
3123 rajveer 92
	 */
3183 rajveer 93
	private int getPort(String key) throws ConfigException{
68 ashish 94
			String port = ConfigClient.getClient().get(key);
3183 rajveer 95
			try{
96
				int portNumber = Integer.parseInt(port);
97
				return portNumber;
98
			}catch (NumberFormatException e) {
99
				Logger.log("Unable to parse the port number to string", null);
100
				throw new ConfigException(100, "Unable to parse the port number to string");
101
			}
102
 
68 ashish 103
	}
3123 rajveer 104
 
105
	/**
106
	 * Connect to the service. It also have mechanism to handle multiple service instances.
107
	 * Connect to default service:
108
	 *     If fails: Connect to another services instances till have tried all services.
109
	 * Tries the default service at MAX_ATTEMPTS times  
110
	 * @param attemptNumber
111
	 * @throws TTransportException
3183 rajveer 112
	 * @throws ConfigException 
3123 rajveer 113
	 */
3183 rajveer 114
	private void connectToService(int attemptNumber) throws TTransportException, ConfigException{
3123 rajveer 115
		try{
116
			openTransport();
117
		}catch (TTransportException e) {
118
			if(attemptNumber < MAX_ATTEMPTS){
119
				connectToService(attemptNumber+1);
120
			}else{
121
				if(serviceNumber < TOTAL_SERVICES){
122
					serviceNumber += 1;
123
					Logger.log("Maximum attempts have reached. Failing over to another service", null);
124
					loadConfigParameters(this.hostConfigKey + serviceNumber, this.portConfigKey + serviceNumber);
125
					connectToService(FIRST_ATTEMPT);
126
					updateConfigParameters(this.hostConfigKey, this.portConfigKey);
127
				}else{
128
					Logger.log("Have tried enough services. Giving up.....", null);
129
					throw e;
130
				}
131
			}
132
		}
133
	}
767 rajveer 134
 
3123 rajveer 135
	/**
136
	 * Update the config parameters for the current service being used, so that next time these parameters can be used.
137
	 * @param hostConfigKey
138
	 * @param portConfigKey
139
	 */
140
	private void updateConfigParameters(String hostConfigKey, String portConfigKey){
141
		try {
142
			ConfigClient.getClient().set(hostConfigKey, this.hostname);
143
			ConfigClient.getClient().set(portConfigKey, this.port+"");
144
		} catch (ConfigException e) {
145
			Logger.log("Unable to update the keys in the section", null);
146
		}	
147
	}
148
 
149
	/**
150
	 * Open the transport to connect to service.
151
	 * @throws TTransportException
152
	 */
153
	@PostConstruct
154
	protected void openTransport() throws TTransportException {
155
		Logger.log("Initializing socket infra ", this);
156
		socket = new TSocket(hostname, port);
157
		socket.setTimeout(SOCKET_TIMEOUT);
158
		transport = new TFramedTransport(socket);
159
		protocol = new TBinaryProtocol(transport);
160
 
161
		if(!transport.isOpen()){
162
			transport.open();
163
		}else{
164
			Logger.log("Transport was already open", this);
165
		}
166
	}
167
 
168
 
169
	/**
170
	 * Close the transport
171
	 */
172
	@PreDestroy
173
	protected void closeTransport(){
174
		if(transport != null && transport.isOpen()){
175
			Logger.log("Closing transport :", this);
176
			transport.close();
177
		}
178
	}
179
 
180
	/**
181
	 * Close the connection.
182
	 */
767 rajveer 183
	public void closeConnection(){
184
		if(transport != null && transport.isOpen()){
185
			Logger.log("Closing transport :", this);
186
			transport.close();
187
		}
188
	}
189
 
3123 rajveer 190
 
191
	/**
192
	 * Closing connection in finalize.
193
	 */
767 rajveer 194
	protected void finalize() throws Throwable{
195
		super.finalize();
196
		this.closeConnection();
197
	}
198
 
3123 rajveer 199
	public abstract void closeSession();
200
 
68 ashish 201
}