Interface SpannerOptions.CallContextConfigurator

  • All Known Implementing Classes:
    SpannerOptions.SpannerCallContextTimeoutConfigurator
    Enclosing class:
    SpannerOptions

    public static interface SpannerOptions.CallContextConfigurator
    SpannerOptions.CallContextConfigurator can be used to modify the ApiCallContext for one or more specific RPCs. This can be used to set specific timeout value for RPCs or use specific CallCredentials for an RPC. The SpannerOptions.CallContextConfigurator must be set as a value on the Context using the SpannerOptions.CALL_CONTEXT_CONFIGURATOR_KEY key.

    This API is meant for advanced users. Most users should instead use the SpannerOptions.SpannerCallContextTimeoutConfigurator for setting timeouts per RPC.

    Example usage:

    
     CallContextConfigurator configurator =
         new CallContextConfigurator() {
           public <ReqT, RespT> ApiCallContext configure(
               ApiCallContext context, ReqT request, MethodDescriptor<ReqT, RespT> method) {
             if (method == SpannerGrpc.getExecuteBatchDmlMethod()) {
               return GrpcCallContext.createDefault()
                   .withCallOptions(CallOptions.DEFAULT.withDeadlineAfter(60L, TimeUnit.SECONDS));
             }
             return null;
           }
         };
     Context context =
         Context.current().withValue(SpannerOptions.CALL_CONTEXT_CONFIGURATOR_KEY, configurator);
     context.run(
         () -> {
           try {
             client
                 .readWriteTransaction()
                 .run(
                     new TransactionCallable<long[]>() {
                       public long[] run(TransactionContext transaction) throws Exception {
                         return transaction.batchUpdate(
                             ImmutableList.of(statement1, statement2));
                       }
                     });
           } catch (SpannerException e) {
             if (e.getErrorCode() == ErrorCode.DEADLINE_EXCEEDED) {
               // handle timeout exception.
             }
           }
         }
     
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      <ReqT,​RespT>
      com.google.api.gax.rpc.ApiCallContext
      configure​(com.google.api.gax.rpc.ApiCallContext context, ReqT request, io.grpc.MethodDescriptor<ReqT,​RespT> method)
      Configure a ApiCallContext for a specific RPC call.
    • Method Detail

      • configure

        @Nullable
        <ReqT,​RespT> com.google.api.gax.rpc.ApiCallContext configure​(com.google.api.gax.rpc.ApiCallContext context,
                                                                           ReqT request,
                                                                           io.grpc.MethodDescriptor<ReqT,​RespT> method)
        Configure a ApiCallContext for a specific RPC call.
        Parameters:
        context - The default context. This can be used to inspect the current values.
        request - The request that will be sent.
        method - The method that is being called.
        Returns:
        An ApiCallContext that will be merged with the default ApiCallContext. If null is returned, no changes to the default ApiCallContext will be made.