Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
1932732
Pass used services to operations
markusheiden Mar 1, 2024
609fc62
Pass used service client to operations
markusheiden Mar 1, 2024
2be2c83
Make fields final
markusheiden Mar 1, 2024
2d1a651
Pass used service client to operations
markusheiden Mar 1, 2024
dce15dd
Let status provider know its service client
markusheiden Mar 1, 2024
b37bc8f
Configure tracker via status provider
markusheiden Mar 1, 2024
b9af05d
Remove unused imports
markusheiden Mar 1, 2024
a9bf681
Reduce code duplication
markusheiden Mar 1, 2024
fd388e6
Simplify http file service
markusheiden Mar 1, 2024
f4668ce
Remove unused imports
markusheiden Mar 1, 2024
3e90366
Remove annotation
markusheiden Mar 1, 2024
92be2e7
Consolidate code
markusheiden Mar 1, 2024
4661a39
Consolidate reporting tracking
markusheiden Mar 2, 2024
45c4b3c
Format code
markusheiden Mar 2, 2024
b0ab994
Consolidate code
markusheiden Mar 2, 2024
3d20ae8
Implement pooling HTTP client
markusheiden Mar 2, 2024
f68d98f
Close pool
markusheiden Mar 2, 2024
167fbaf
Use a single client if the timeout is the same
markusheiden Mar 3, 2024
25d84dc
Expose connection pool controls
markusheiden Mar 3, 2024
b3d1de9
Fix delegation
markusheiden Mar 3, 2024
bb5d8ab
Simplify code
markusheiden Mar 3, 2024
653d991
Use correct client
markusheiden Mar 3, 2024
23ac804
Extract method
markusheiden Mar 3, 2024
2cf8954
Close stream
markusheiden Mar 3, 2024
01bc75c
Close idle connections
markusheiden Mar 3, 2024
9071d01
Enhance configurability
markusheiden Mar 3, 2024
8f3a736
Rework wording
markusheiden Mar 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
import java.util.function.Function;

import com.microsoft.bingads.AsyncCallback;
import com.microsoft.bingads.ServiceClient;
import com.microsoft.bingads.internal.functionalinterfaces.BiConsumer;
import com.microsoft.bingads.internal.functionalinterfaces.Consumer;
import com.microsoft.bingads.internal.functionalinterfaces.TriConsumer;

public class OperationStatusRetry<TOperationStatus, TOperationStatusProvider, TService> {
private static final int INTERVAL_OF_RETRY = 1000; // TimeUnit Milliseconds
Expand All @@ -28,43 +27,43 @@ public OperationStatusRetry(Function<Exception, Integer> f) {
}

public void executeWithRetry(
final TriConsumer<TOperationStatusProvider, ServiceClient<TService>, AsyncCallback<TOperationStatus>> action,
final TOperationStatusProvider statusProvider, final ServiceClient<TService> serviceClient,
final Consumer<TOperationStatus> statusConsumer, final Consumer<Exception> exceptionConsumer,
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, serviceClient, statusConsumer, exceptionConsumer, maxRetryCount);
doPollOperationStatus(action, statusProvider, statusConsumer, exceptionConsumer, maxRetryCount);
}

private void doPollOperationStatus(
final TriConsumer<TOperationStatusProvider, ServiceClient<TService>, AsyncCallback<TOperationStatus>> action,
final TOperationStatusProvider statusProvider, final ServiceClient<TService> serviceClient,
final BiConsumer<TOperationStatusProvider, AsyncCallback<TOperationStatus>> action,
final TOperationStatusProvider statusProvider,
final Consumer<TOperationStatus> statusConsumer, final Consumer<Exception> exceptionConsumer,
final int maxRetryCount) {
action.accept(statusProvider, serviceClient, new AsyncCallback<TOperationStatus>() {
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, serviceClient, statusConsumer, exceptionConsumer,
retryWhenException(action, statusProvider, statusConsumer, exceptionConsumer,
maxRetryCount, exception);
} catch (ExecutionException exception) {
retryWhenException(action, statusProvider, serviceClient, statusConsumer, exceptionConsumer,
retryWhenException(action, statusProvider, statusConsumer, exceptionConsumer,
maxRetryCount, exception);
}
}

private void retryWhenException(
final TriConsumer<TOperationStatusProvider, ServiceClient<TService>, AsyncCallback<TOperationStatus>> action,
final TOperationStatusProvider statusProvider, final ServiceClient<TService> serviceClient,
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, serviceClient, statusConsumer, exceptionConsumer, maxRetryCount - 1,
exception);
retry(action, statusProvider, statusConsumer, exceptionConsumer, maxRetryCount - 1, exception);
} else {
executor.shutdown();
exceptionConsumer.accept(exception);
Expand All @@ -74,16 +73,15 @@ private void retryWhenException(
}

private void retry(
final TriConsumer<TOperationStatusProvider, ServiceClient<TService>, AsyncCallback<TOperationStatus>> action,
final TOperationStatusProvider statusProvider, final ServiceClient<TService> serviceClient,
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, serviceClient, statusConsumer, exceptionConsumer,
maxRetryCount);
doPollOperationStatus(action, statusProvider, statusConsumer, exceptionConsumer, maxRetryCount);
}
}, interval, TimeUnit.MILLISECONDS);

Expand Down
Loading