-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathOperationStatusRetry.java
More file actions
99 lines (83 loc) · 4.38 KB
/
OperationStatusRetry.java
File metadata and controls
99 lines (83 loc) · 4.38 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
package com.microsoft.bingads.internal;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import com.microsoft.bingads.AsyncCallback;
import com.microsoft.bingads.internal.functionalinterfaces.BiConsumer;
import com.microsoft.bingads.internal.functionalinterfaces.Consumer;
public class OperationStatusRetry<TOperationStatus, TOperationStatusProvider, TService> {
private static final int INTERVAL_OF_RETRY = 1000; // TimeUnit Milliseconds
private static final int[] INTERVAL_OF_RETRY_RATE_LIMIT = { 15000, 20000, 25000, 30000 }; // TimeUnit
private static final int RATELIMIT_CODE = 117;
private ScheduledExecutorService executor;
private Function<Exception, Integer> errorCodeFunc;
public OperationStatusRetry(Function<Exception, Integer> f) {
errorCodeFunc = f;
}
public void executeWithRetry(
final BiConsumer<TOperationStatusProvider, AsyncCallback<TOperationStatus>> action,
final TOperationStatusProvider statusProvider,
final Consumer<TOperationStatus> statusConsumer,
final Consumer<Exception> exceptionConsumer,
final int maxRetryCount) {
executor = Executors.newSingleThreadScheduledExecutor();
doPollOperationStatus(action, statusProvider, statusConsumer, exceptionConsumer, maxRetryCount);
}
private void doPollOperationStatus(
final BiConsumer<TOperationStatusProvider, AsyncCallback<TOperationStatus>> action,
final TOperationStatusProvider statusProvider,
final Consumer<TOperationStatus> statusConsumer, final Consumer<Exception> exceptionConsumer,
final int maxRetryCount) {
action.accept(statusProvider, new AsyncCallback<TOperationStatus>() {
@Override
public void onCompleted(Future<TOperationStatus> result) {
try {
statusConsumer.accept(result.get());
executor.shutdown();
} catch (InterruptedException exception) {
retryWhenException(action, statusProvider, statusConsumer, exceptionConsumer,
maxRetryCount, exception);
} catch (ExecutionException exception) {
retryWhenException(action, statusProvider, statusConsumer, exceptionConsumer,
maxRetryCount, exception);
}
}
private void retryWhenException(
final BiConsumer<TOperationStatusProvider, AsyncCallback<TOperationStatus>> action,
final TOperationStatusProvider statusProvider,
final Consumer<TOperationStatus> statusConsumer, final Consumer<Exception> exceptionConsumer,
final int maxRetryCount, Exception exception) {
if (maxRetryCount > 0) {
retry(action, statusProvider, statusConsumer, exceptionConsumer, maxRetryCount - 1, exception);
} else {
executor.shutdown();
exceptionConsumer.accept(exception);
}
}
});
}
private void retry(
final BiConsumer<TOperationStatusProvider, AsyncCallback<TOperationStatus>> action,
final TOperationStatusProvider statusProvider,
final Consumer<TOperationStatus> statusConsumer, final Consumer<Exception> exceptionConsumer,
final int maxRetryCount, Exception exception) {
int interval = getInterval(exception, maxRetryCount);
executor.schedule(new Runnable() {
@Override
public void run() {
doPollOperationStatus(action, statusProvider, statusConsumer, exceptionConsumer, maxRetryCount);
}
}, interval, TimeUnit.MILLISECONDS);
}
private int getInterval(Exception exception, int maxRetryCount) {
int errorCode = errorCodeFunc.apply(exception);
if (errorCode == RATELIMIT_CODE) {
return INTERVAL_OF_RETRY_RATE_LIMIT[INTERVAL_OF_RETRY_RATE_LIMIT.length - 1 - maxRetryCount];
} else {
return INTERVAL_OF_RETRY;
}
}
}