|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.bigquery; |
| 18 | + |
| 19 | +// [START bigquery_set_custom_retry_algorithm] |
| 20 | +import com.google.api.gax.retrying.ResultRetryAlgorithm; |
| 21 | +import com.google.cloud.ExceptionHandler; |
| 22 | +import com.google.cloud.bigquery.BigQuery; |
| 23 | +import com.google.cloud.bigquery.BigQueryOptions; |
| 24 | +import java.util.concurrent.TimeoutException; |
| 25 | + |
| 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 | + public static void main(String... args) { |
| 42 | + // TODO(developer): Replace projectId and exception classes before running |
| 43 | + // the sample. |
| 44 | + String projectId = "project-id"; |
| 45 | + ResultRetryAlgorithm<?> retryAlgorithm = |
| 46 | + ExceptionHandler.newBuilder() |
| 47 | + .abortOn(RuntimeException.class) |
| 48 | + .retryOn(TimeoutException.class) |
| 49 | + .addInterceptors(EXCEPTION_HANDLER_INTERCEPTOR) |
| 50 | + .build(); |
| 51 | + setCustomRetryAlgorithm(projectId, retryAlgorithm); |
| 52 | + } |
| 53 | + |
| 54 | + public static void setCustomRetryAlgorithm( |
| 55 | + String projectId, ResultRetryAlgorithm<?> retryAlgorithm) { |
| 56 | + BigQueryOptions options = |
| 57 | + BigQueryOptions.newBuilder() |
| 58 | + .setProjectId(projectId) |
| 59 | + .setResultRetryAlgorithm(retryAlgorithm) |
| 60 | + .build(); |
| 61 | + |
| 62 | + BigQuery bigquery = options.getService(); |
| 63 | + |
| 64 | + System.out.println(bigquery.getOptions().getResultRetryAlgorithm()); |
| 65 | + } |
| 66 | +} |
| 67 | +// [END bigquery_set_custom_retry_algorithm] |
0 commit comments