Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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