forked from ringcentral/pubnub-jtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestManager.java
More file actions
362 lines (299 loc) · 10.6 KB
/
Copy pathRequestManager.java
File metadata and controls
362 lines (299 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package com.pubnub.api;
import java.util.Hashtable;
import java.util.Vector;
abstract class Worker implements Runnable {
private Vector _requestQueue;
protected volatile boolean _die;
private Thread thread;
protected HttpClient httpclient;
protected static Logger log = new Logger(Worker.class);
public Thread getThread() {
return thread;
}
void setThread(Thread thread) {
this.thread = thread;
}
void startWorker() {
thread.start();
}
void interruptWorker() {
thread.interrupt();
}
void resetConnection() {
httpclient.reset();
}
Worker(Vector _requestQueue, int connectionTimeout, int requestTimeout, Hashtable headers) {
this._requestQueue = _requestQueue;
this.httpclient = HttpClient.getClient(connectionTimeout,
requestTimeout, headers);
}
void setConnectionTimeout(int timeout) {
if (httpclient != null) {
httpclient.setConnectionTimeout(timeout);
}
}
void setRequestTimeout(int timeout) {
if (httpclient != null) {
httpclient.setRequestTimeout(timeout);
}
}
public abstract void shutdown();
void die() {
_die = true;
}
abstract void process(HttpRequest hreq);
public void run() {
do {
HttpRequest hreq = null;
while (!_die) {
synchronized (_requestQueue) {
if (_requestQueue.size() != 0) {
hreq = (HttpRequest) _requestQueue.firstElement();
_requestQueue.removeElementAt(0);
break;
}
try {
_requestQueue.wait(1000);
} catch (InterruptedException e) {
}
}
}
if (hreq != null) {
if (!_die) {
process(hreq);
}
}
} while (!_die);
shutdown();
}
}
class NonSubscribeWorker extends Worker {
NonSubscribeWorker(Vector _requestQueue, int connectionTimeout,
int requestTimeout, Hashtable headers) {
super(_requestQueue, connectionTimeout, requestTimeout, headers);
}
void process(HttpRequest hreq) {
HttpResponse hresp = null;
try {
log.debug(hreq.getUrl());
hresp = httpclient.fetch(hreq.getUrl(), hreq.getHeaders());
} catch (PubnubException pe) {
log.debug("Pubnub Exception in Fetch : " + pe.getPubnubError());
if (!_die)
hreq.getResponseHandler().handleError(hreq, pe.getPubnubError());
return;
} catch (Exception e) {
log.debug("Exception in Fetch : " + e.toString());
if (!_die)
hreq.getResponseHandler().handleError(hreq, PubnubError.getErrorObject(PubnubError.PNERROBJ_HTTP_ERROR, 2, e.toString()));
return;
}
if (!_die) {
if (hresp == null) {
log.debug("Error in fetching url : " + hreq.getUrl());
hreq.getResponseHandler().handleError(hreq, PubnubError.getErrorObject(PubnubError.PNERROBJ_HTTP_ERROR , 3));
return;
}
hreq.getResponseHandler().handleResponse(hreq, hresp.getResponse());
}
}
public void shutdown() {
if (httpclient != null) httpclient.shutdown();
}
}
abstract class RequestManager {
private static int _maxWorkers = 1;
protected Vector _waiting = new Vector();
protected Worker _workers[];
protected String name;
protected volatile int connectionTimeout;
protected volatile int requestTimeout;
protected Hashtable headers;
private static int count = 0;
protected static Logger log = new Logger(RequestManager.class);
public static int getWorkerCount() {
return _maxWorkers;
}
public abstract Worker getWorker();
private void initManager(int maxCalls, String name) {
if (maxCalls < 1) {
maxCalls = 1;
}
this.name = name;
this.headers = new Hashtable();
_workers = new Worker[maxCalls];
synchronized (_workers) {
for (int i = 0; i < maxCalls; ++i) {
Worker w = getWorker();
w.setThread(new Thread(w, name + "-" + ++count));
_workers[i] = w;
log.verbose("Starting new worker " + _workers[i].getThread().getName());
w.startWorker();
}
}
}
public RequestManager(String name, int connectionTimeout, int requestTimeout) {
this.connectionTimeout = connectionTimeout;
this.requestTimeout = requestTimeout;
initManager(_maxWorkers, name);
}
private void interruptWorkers() {
synchronized (_workers) {
for (int i = 0; i < _workers.length; i++) {
_workers[i].interruptWorker();
}
}
}
class ConnectionResetter implements Runnable {
Worker worker;
ConnectionResetter(Worker w) {
this.worker = w;
}
public void run() {
if (this.worker != null) {
worker.resetConnection();
}
}
}
public void resetWorkers() {
synchronized (_workers) {
for (int i = 0; i < _workers.length; i++) {
log.verbose("Sending DIE to " + _workers[i].getThread().getName());
_workers[i].die();
new Thread(new ConnectionResetter(_workers[i])).start();
_workers[i].interruptWorker();
Worker w = getWorker();
w.setThread(new Thread(w, name + "-" + ++count));
_workers[i] = w;
log.verbose("Starting new worker " + _workers[i].getThread().getName());
w.startWorker();
}
}
}
public void setHeader(String key, String value) {
this.headers.put(key, value);
}
public abstract void clearRequestQueue();
public void resetHttpManager() {
clearRequestQueue();
resetWorkers();
}
public void abortClearAndQueue(HttpRequest hreq) {
resetHttpManager();
queue(hreq);
}
public void queue(HttpRequest hreq) {
log.debug("Queued : " + hreq.getUrl());
synchronized (_waiting) {
_waiting.addElement(hreq);
_waiting.notifyAll();
}
}
public static void setWorkerCount(int count) {
_maxWorkers = count;
}
public void stop() {
synchronized (_workers) {
for (int i = 0; i < _maxWorkers; ++i) {
Worker w = _workers[i];
w.die();
}
}
synchronized (_waiting) {
_waiting.notifyAll();
}
}
}
abstract class AbstractSubscribeManager extends RequestManager {
protected volatile int maxRetries = 5;
protected volatile int retryInterval = 5000;
protected volatile int windowInterval = 0;
public AbstractSubscribeManager(String name, int connectionTimeout,
int requestTimeout) {
super(name, connectionTimeout, requestTimeout);
}
public Worker getWorker() {
return new SubscribeWorker(_waiting,
connectionTimeout, requestTimeout,
maxRetries, retryInterval, windowInterval, headers);
}
public void setMaxRetries(int maxRetries) {
this.maxRetries = maxRetries;
for (int i = 0; i < _workers.length; i++) {
((SubscribeWorker) _workers[i]).setMaxRetries(maxRetries);
}
}
public void setRetryInterval(int retryInterval) {
this.retryInterval = retryInterval;
for (int i = 0; i < _workers.length; i++) {
((SubscribeWorker) _workers[i]).setRetryInterval(retryInterval);
}
}
public void setWindowInterval(int windowInterval) {
this.windowInterval = windowInterval;
for (int i = 0; i < _workers.length; i++) {
((SubscribeWorker) _workers[i]).setWindowInterval(windowInterval);
}
}
public void setConnectionTimeout(int timeout) {
this.connectionTimeout = timeout;
}
public void setRequestTimeout(int timeout) {
this.requestTimeout = timeout;
}
public void queue(HttpRequest hreq) {
synchronized (_waiting) {
clearRequestQueue();
super.queue(hreq);
}
}
}
abstract class AbstractNonSubscribeManager extends RequestManager {
public AbstractNonSubscribeManager(String name, int connectionTimeout,
int requestTimeout) {
super(name, connectionTimeout, requestTimeout);
}
public Worker getWorker() {
return new NonSubscribeWorker(_waiting, connectionTimeout,
requestTimeout, headers);
}
public void setConnectionTimeout(int timeout) {
this.connectionTimeout = timeout;
for (int i = 0; i < _workers.length; i++) {
_workers[i].setConnectionTimeout(timeout);
}
}
public void setRequestTimeout(int timeout) {
this.requestTimeout = timeout;
for (int i = 0; i < _workers.length; i++) {
_workers[i].setRequestTimeout(timeout);
}
}
}
abstract class AbstractSubscribeWorker extends Worker {
protected volatile int maxRetries = 5;
protected volatile int retryInterval = 5000;
protected volatile int windowInterval = 0;
AbstractSubscribeWorker(Vector _requestQueue, int connectionTimeout,
int requestTimeout, int maxRetries, int retryInterval, Hashtable headers) {
super(_requestQueue, connectionTimeout, requestTimeout, headers);
this.maxRetries = maxRetries;
this.retryInterval= retryInterval;
}
AbstractSubscribeWorker(Vector _requestQueue, int connectionTimeout,
int requestTimeout, int maxRetries, int retryInterval, int windowInterval, Hashtable headers) {
super(_requestQueue, connectionTimeout, requestTimeout, headers);
this.maxRetries = maxRetries;
this.retryInterval= retryInterval;
this.windowInterval = windowInterval;
}
public void setMaxRetries(int maxRetries) {
this.maxRetries = maxRetries;
}
public void setRetryInterval(int retryInterval) {
this.retryInterval = retryInterval;
}
public void setWindowInterval(int windowInterval) {
this.windowInterval = windowInterval;
}
}