Skip to content
This repository was archived by the owner on Mar 23, 2026. It is now read-only.

Commit 321026f

Browse files
committed
replace custom interceptor with BaseService interceptor
1 parent 317dc35 commit 321026f

1 file changed

Lines changed: 13 additions & 19 deletions

File tree

samples/snippets/src/main/java/com/example/bigquery/SetCustomRetryAlgorithm.java

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,29 @@
1818

1919
// [START bigquery_set_custom_retry_algorithm]
2020
import com.google.api.gax.retrying.ResultRetryAlgorithm;
21+
import com.google.cloud.BaseService;
2122
import com.google.cloud.ExceptionHandler;
2223
import com.google.cloud.bigquery.BigQuery;
2324
import com.google.cloud.bigquery.BigQueryOptions;
24-
import java.util.concurrent.TimeoutException;
2525

2626
public class SetCustomRetryAlgorithm {
27-
// In order to use a custom retry algorithm, you must implement a custom
28-
// Interceptor that implements the ExceptionHandler.Interceptor interface.
29-
public static final ExceptionHandler.Interceptor EXCEPTION_HANDLER_INTERCEPTOR =
30-
new ExceptionHandler.Interceptor() {
31-
public ExceptionHandler.Interceptor.RetryResult afterEval(
32-
Exception exception, ExceptionHandler.Interceptor.RetryResult retryResult) {
33-
return RetryResult.CONTINUE_EVALUATION;
34-
}
35-
36-
public ExceptionHandler.Interceptor.RetryResult beforeEval(Exception exception) {
37-
return RetryResult.CONTINUE_EVALUATION;
38-
}
39-
};
40-
4127
public static void main(String... args) {
42-
// TODO(developer): Replace projectId and exception classes before running
43-
// the sample.
28+
// TODO(developer): Replace projectId and retryAlgorithm classes before
29+
// running the sample. The ResultRetryAlgorithm abortOn and retryOn methods
30+
// can be used to specify retry behavior when the client encounters
31+
// exceptions during its execution.
4432
String projectId = "project-id";
4533
ResultRetryAlgorithm<?> retryAlgorithm =
4634
ExceptionHandler.newBuilder()
4735
.abortOn(RuntimeException.class)
48-
.retryOn(TimeoutException.class)
49-
.addInterceptors(EXCEPTION_HANDLER_INTERCEPTOR)
36+
.retryOn(java.net.ConnectException.class)
37+
.retryOn(java.net.UnknownHostException.class)
38+
.retryOn(java.net.SocketException.class)
39+
// Alternatively, you can create your own Interceptor object that
40+
// implements the com.google.cloud.ExceptionHandler.Interceptor
41+
// interface. See
42+
// https://github.com/googleapis/sdk-platform-java/blob/f18318660c05d0d8466e3ead7127f0747fac2e2e/java-core/google-cloud-core/src/main/java/com/google/cloud/ExceptionHandler.java#L49
43+
.addInterceptors(BaseService.EXCEPTION_HANDLER_INTERCEPTOR)
5044
.build();
5145
setCustomRetryAlgorithm(projectId, retryAlgorithm);
5246
}

0 commit comments

Comments
 (0)