Package com.google.cloud.spanner
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
Modifier and TypeMethodDescription<ReqT,RespT>
com.google.api.gax.rpc.ApiCallContextconfigure(com.google.api.gax.rpc.ApiCallContext context, ReqT request, io.grpc.MethodDescriptor<ReqT, RespT> method) Configure aApiCallContextfor a specific RPC call.
-
Method Details
-
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 aApiCallContextfor 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
ApiCallContextthat will be merged with the defaultApiCallContext. Ifnullis returned, no changes to the defaultApiCallContextwill be made.
-