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.
}
}
}
Modifier and Type | Method and Description |
---|---|
<ReqT,RespT> |
configure(com.google.api.gax.rpc.ApiCallContext context,
ReqT request,
io.grpc.MethodDescriptor<ReqT,RespT> method)
Configure a
ApiCallContext for a specific RPC call. |
@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)
ApiCallContext
for a specific RPC call.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.ApiCallContext
that will be merged with the default ApiCallContext
. If null
is returned, no changes to the default ApiCallContext
will be made.Copyright © 2021 Google LLC. All rights reserved.