Class SpannerOptions.SpannerCallContextTimeoutConfigurator

java.lang.Object
com.google.cloud.spanner.SpannerOptions.SpannerCallContextTimeoutConfigurator
All Implemented Interfaces:
SpannerOptions.CallContextConfigurator
Enclosing class:
SpannerOptions

public static class SpannerOptions.SpannerCallContextTimeoutConfigurator extends Object implements SpannerOptions.CallContextConfigurator
Helper class to configure timeouts for specific Spanner RPCs. The SpannerOptions.SpannerCallContextTimeoutConfigurator must be set as a value on the Context using the SpannerOptions.CALL_CONTEXT_CONFIGURATOR_KEY key.

Example usage:


 // Create a context with a ExecuteQuery timeout of 10 seconds.
 Context context =
     Context.current()
         .withValue(
             SpannerOptions.CALL_CONTEXT_CONFIGURATOR_KEY,
             SpannerCallContextTimeoutConfigurator.create()
                 .withExecuteQueryTimeout(Duration.ofSeconds(10L)));
 context.run(
     () -> {
       try (ResultSet rs =
           client
               .singleUse()
               .executeQuery(
                   Statement.of(
                       "SELECT SingerId, FirstName, LastName FROM Singers ORDER BY LastName"))) {
         while (rs.next()) {
           System.out.printf("%d %s %s%n", rs.getLong(0), rs.getString(1), rs.getString(2));
         }
       } catch (SpannerException e) {
         if (e.getErrorCode() == ErrorCode.DEADLINE_EXCEEDED) {
           // Handle timeout.
         }
       }
     }