|
18 | 18 |
|
19 | 19 | // [START bigquery_set_custom_retry_algorithm] |
20 | 20 | import com.google.api.gax.retrying.ResultRetryAlgorithm; |
| 21 | +import com.google.cloud.BaseService; |
21 | 22 | import com.google.cloud.ExceptionHandler; |
22 | 23 | import com.google.cloud.bigquery.BigQuery; |
23 | 24 | import com.google.cloud.bigquery.BigQueryOptions; |
24 | | -import java.util.concurrent.TimeoutException; |
25 | 25 |
|
26 | 26 | 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 | | - |
41 | 27 | 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. |
44 | 32 | String projectId = "project-id"; |
45 | 33 | ResultRetryAlgorithm<?> retryAlgorithm = |
46 | 34 | ExceptionHandler.newBuilder() |
47 | 35 | .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) |
50 | 44 | .build(); |
51 | 45 | setCustomRetryAlgorithm(projectId, retryAlgorithm); |
52 | 46 | } |
|
0 commit comments