Subversion Repositories SmartDukaan

Rev

Rev 2298 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2298 Rev 3123
Line 1... Line 1...
1
package in.shop2020.thrift.clients;
1
package in.shop2020.thrift.clients;
2
 
2
 
-
 
3
import javax.annotation.PostConstruct;
-
 
4
import javax.annotation.PreDestroy;
-
 
5
 
3
import in.shop2020.config.ConfigException;
6
import in.shop2020.config.ConfigException;
4
import in.shop2020.thrift.clients.config.ConfigClient;
7
import in.shop2020.thrift.clients.config.ConfigClient;
5
import in.shop2020.utils.Logger;
8
import in.shop2020.utils.Logger;
6
 
9
 
7
import org.apache.thrift.protocol.TBinaryProtocol;
10
import org.apache.thrift.protocol.TBinaryProtocol;
8
import org.apache.thrift.protocol.TProtocol;
11
import org.apache.thrift.protocol.TProtocol;
9
import org.apache.thrift.transport.TFramedTransport;
12
import org.apache.thrift.transport.TFramedTransport;
10
import org.apache.thrift.transport.TSocket;
13
import org.apache.thrift.transport.TSocket;
11
import org.apache.thrift.transport.TTransport;
14
import org.apache.thrift.transport.TTransport;
-
 
15
import org.apache.thrift.transport.TTransportException;
12
 
16
 
13
public abstract class GenericServiceClient {
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;
14
	
25
	
-
 
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;
15
	protected String hostname = "localhost";
39
	protected String hostname = "localhost";
16
	protected int port = 8080;
40
	protected int port = 8080;
17
	
41
	
18
	protected TSocket socket = null;
42
	protected String hostConfigKey;
19
	protected TTransport transport = null;
-
 
20
	protected TProtocol protocol = null;
43
	protected String portConfigKey;
-
 
44
 
21
		
45
		
22
	private final int SOCKET_TIMEOUT = 1000 * 60 * 5;
-
 
23
	
46
	
-
 
47
	/**
-
 
48
	 * Constructor
-
 
49
	 * @param hostConfigKey
-
 
50
	 * @param portConfigKey
-
 
51
	 * @throws TTransportException
-
 
52
	 */
24
	public GenericServiceClient(String clientIdentifier, String hostConfigKey, String portConfigKey) throws Exception{
53
	public GenericClient(String hostConfigKey, String portConfigKey) throws TTransportException{
25
		if(clientIdentifier == null||clientIdentifier.equals("")){
54
		this.hostConfigKey = hostConfigKey; 
-
 
55
		this.portConfigKey = portConfigKey;
26
			Logger.log("ClientIdentifier is null or empty, cannot initiate client", null);
56
		loadConfigParameters(hostConfigKey, portConfigKey);
-
 
57
		connectToService(FIRST_ATTEMPT);
27
		}
58
	}
-
 
59
	
-
 
60
	/**
-
 
61
	 * Load config parameters for the given key set
-
 
62
	 * @param hostConfigKey
-
 
63
	 * @param portConfigKey
-
 
64
	 */
-
 
65
	private void loadConfigParameters(String hostConfigKey, String  portConfigKey){
28
		this.hostname = getHost(hostConfigKey);
66
		this.hostname = getHost(hostConfigKey);
29
		this.port = getPort(portConfigKey);
67
		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
		protocol = new TBinaryProtocol(transport);
-
 
43
	}
68
	}
44
	
69
	
-
 
70
 
-
 
71
	/**
-
 
72
	 * get the Host name from given key
-
 
73
	 * @param key
-
 
74
	 * @return
-
 
75
	 */
45
	private String getHost(String key){
76
	private String getHost(String key){
46
		try {
77
		try {
47
			String host = ConfigClient.getClient().get(key);
78
			String host = ConfigClient.getClient().get(key);
48
			return host;
79
			return host;
49
		} catch (ConfigException e) {
80
		} catch (ConfigException e) {
50
			Logger.log("Error while fetching hostname for key "+ key+" using localhost:"+ e, this);
81
			Logger.log("Error while fetching hostname for key "+ key+" using localhost:"+ e, this);
51
			return "localhost";
82
			return "localhost";
52
		}
83
		}
53
	}
84
	}
54
	
85
	
-
 
86
	/**
-
 
87
	 * get the Port from given key
-
 
88
	 * @param key
-
 
89
	 * @return
-
 
90
	 */
55
	private int getPort(String key){
91
	private int getPort(String key){
56
		try {
92
		try {
57
			String port = ConfigClient.getClient().get(key);
93
			String port = ConfigClient.getClient().get(key);
58
			return Integer.parseInt(port);
94
			return Integer.parseInt(port);
59
		}catch (NumberFormatException ne){
95
		}catch (NumberFormatException ne){
60
			Logger.log("Could not convert string to int for key "+ key+" using 8080 as port"+ ne, this);
96
			Logger.log("Could not convert string to int for key "+ key+" using 8080 as port"+ ne, this);
61
			return 9009;
97
			return 8080;
62
		}
98
		}
63
		catch (ConfigException e) {
99
		catch (ConfigException e) {
64
			Logger.log("Error while fetching port for key "+ key+" using 8080:"+ e, this);
100
			Logger.log("Error while fetching port for key "+ key+" using 8080:"+ e, this);
65
			return 9009;
101
			return 8080;
-
 
102
		}
-
 
103
	}
-
 
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
-
 
112
	 */
-
 
113
	private void connectToService(int attemptNumber) throws TTransportException{
-
 
114
		try{
-
 
115
			openTransport();
-
 
116
		}catch (TTransportException e) {
-
 
117
			if(attemptNumber < MAX_ATTEMPTS){
-
 
118
				connectToService(attemptNumber+1);
-
 
119
			}else{
-
 
120
				if(serviceNumber < TOTAL_SERVICES){
-
 
121
					serviceNumber += 1;
-
 
122
					Logger.log("Maximum attempts have reached. Failing over to another service", null);
-
 
123
					loadConfigParameters(this.hostConfigKey + serviceNumber, this.portConfigKey + serviceNumber);
-
 
124
					connectToService(FIRST_ATTEMPT);
-
 
125
					updateConfigParameters(this.hostConfigKey, this.portConfigKey);
-
 
126
				}else{
-
 
127
					Logger.log("Have tried enough services. Giving up.....", null);
-
 
128
					throw e;
-
 
129
				}
-
 
130
			}
66
		}
131
		}
67
	}
132
	}
68
	
133
	
-
 
134
	/**
-
 
135
	 * Update the config parameters for the current service being used, so that next time these parameters can be used.
-
 
136
	 * @param hostConfigKey
-
 
137
	 * @param portConfigKey
-
 
138
	 */
-
 
139
	private void updateConfigParameters(String hostConfigKey, String portConfigKey){
-
 
140
		try {
-
 
141
			ConfigClient.getClient().set(hostConfigKey, this.hostname);
-
 
142
			ConfigClient.getClient().set(portConfigKey, this.port+"");
-
 
143
		} catch (ConfigException e) {
-
 
144
			Logger.log("Unable to update the keys in the section", null);
-
 
145
		}	
-
 
146
	}
-
 
147
 
-
 
148
	/**
-
 
149
	 * Open the transport to connect to service.
-
 
150
	 * @throws TTransportException
-
 
151
	 */
-
 
152
	@PostConstruct
-
 
153
	protected void openTransport() throws TTransportException {
-
 
154
		Logger.log("Initializing socket infra ", this);
-
 
155
		socket = new TSocket(hostname, port);
-
 
156
		socket.setTimeout(SOCKET_TIMEOUT);
-
 
157
		transport = new TFramedTransport(socket);
-
 
158
		protocol = new TBinaryProtocol(transport);
-
 
159
 
-
 
160
		if(!transport.isOpen()){
-
 
161
			transport.open();
-
 
162
		}else{
-
 
163
			Logger.log("Transport was already open", this);
-
 
164
		}
-
 
165
	}
-
 
166
 
-
 
167
 
-
 
168
	/**
-
 
169
	 * Close the transport
-
 
170
	 */
-
 
171
	@PreDestroy
69
	public abstract void closeSession();
172
	protected void closeTransport(){
-
 
173
		if(transport != null && transport.isOpen()){
-
 
174
			Logger.log("Closing transport :", this);
-
 
175
			transport.close();
-
 
176
		}
70
	
177
	}
-
 
178
 
-
 
179
	/**
-
 
180
	 * Close the connection.
-
 
181
	 */
71
	public void closeConnection(){
182
	public void closeConnection(){
72
		if(transport != null && transport.isOpen()){
183
		if(transport != null && transport.isOpen()){
73
			//Removed to prevent socket connection closed exception
-
 
74
			///this.closeSession();
-
 
75
			Logger.log("Closing transport :", this);
184
			Logger.log("Closing transport :", this);
76
			transport.close();
185
			transport.close();
77
		}
186
		}
78
	}
187
	}
79
	
188
	
-
 
189
	
-
 
190
	/**
-
 
191
	 * Closing connection in finalize.
-
 
192
	 */
80
	protected void finalize() throws Throwable{
193
	protected void finalize() throws Throwable{
81
		super.finalize();
194
		super.finalize();
82
		this.closeConnection();
195
		this.closeConnection();
83
	}
196
	}
84
	
197
	
-
 
198
	public abstract void closeSession();
-
 
199
	
85
}
200
}