Package com.google.api.gax.batching
Class BatcherImpl<ElementT,ElementResultT,RequestT,ResponseT>
- java.lang.Object
-
- com.google.api.gax.batching.BatcherImpl<ElementT,ElementResultT,RequestT,ResponseT>
-
- Type Parameters:
ElementT- The type of each individual element to be batched.ElementResultT- The type of the result for each individual element.RequestT- The type of the request that will contain the accumulated elements.ResponseT- The type of the response that will unpack into individual element results.
- All Implemented Interfaces:
Batcher<ElementT,ElementResultT>,AutoCloseable
@InternalApi("For google-cloud-java client use only") public class BatcherImpl<ElementT,ElementResultT,RequestT,ResponseT> extends Object implements Batcher<ElementT,ElementResultT>
Queues up the elements untilflush()is called; once batching is over, returned future resolves.This class is not thread-safe, and expects to be used from a single thread.
-
-
Field Summary
-
Fields inherited from interface com.google.api.gax.batching.Batcher
THROTTLED_TIME_KEY
-
-
Constructor Summary
Constructors Constructor Description BatcherImpl(BatchingDescriptor<ElementT,ElementResultT,RequestT,ResponseT> batchingDescriptor, UnaryCallable<RequestT,ResponseT> unaryCallable, RequestT prototype, BatchingSettings batchingSettings, ScheduledExecutorService executor)Deprecated.Please instantiate the Batcher withFlowControllerandApiCallContextBatcherImpl(BatchingDescriptor<ElementT,ElementResultT,RequestT,ResponseT> batchingDescriptor, UnaryCallable<RequestT,ResponseT> unaryCallable, RequestT prototype, BatchingSettings batchingSettings, ScheduledExecutorService executor, FlowController flowController)Deprecated.Please instantiate the Batcher withApiCallContextBatcherImpl(BatchingDescriptor<ElementT,ElementResultT,RequestT,ResponseT> batchingDescriptor, UnaryCallable<RequestT,ResponseT> unaryCallable, RequestT prototype, BatchingSettings batchingSettings, ScheduledExecutorService executor, FlowController flowController, ApiCallContext callContext)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ApiFuture<ElementResultT>add(ElementT element)Queues the passed in element to be sent at some point in the future.voidclose()Closes this Batcher by preventing new elements from being added, and then flushing the existing elements.ApiFuture<Void>closeAsync()Closes this Batcher by preventing new elements from being added, and then sending outstanding elements.voidflush()Synchronously sends any pending elements as a batch and waits for all outstanding batches to be complete.FlowControllergetFlowController()voidsendOutstanding()Sends accumulated elements asynchronously for batching.
-
-
-
Constructor Detail
-
BatcherImpl
@Deprecated public BatcherImpl(BatchingDescriptor<ElementT,ElementResultT,RequestT,ResponseT> batchingDescriptor, UnaryCallable<RequestT,ResponseT> unaryCallable, RequestT prototype, BatchingSettings batchingSettings, ScheduledExecutorService executor)
Deprecated.Please instantiate the Batcher withFlowControllerandApiCallContext- Parameters:
batchingDescriptor- aBatchingDescriptorfor transforming individual elements into wrappers request and responseunaryCallable- aUnaryCallableobjectprototype- aBatcherImplobjectbatchingSettings- aBatchingSettingswith configuration of thresholds
-
BatcherImpl
@Deprecated public BatcherImpl(BatchingDescriptor<ElementT,ElementResultT,RequestT,ResponseT> batchingDescriptor, UnaryCallable<RequestT,ResponseT> unaryCallable, RequestT prototype, BatchingSettings batchingSettings, ScheduledExecutorService executor, @Nullable FlowController flowController)
Deprecated.Please instantiate the Batcher withApiCallContext- Parameters:
batchingDescriptor- aBatchingDescriptorfor transforming individual elements into wrappers request and responseunaryCallable- aUnaryCallableobjectprototype- aBatcherImplobjectbatchingSettings- aBatchingSettingswith configuration of thresholdsflowController- aFlowControllerfor throttling requests. If it's null, create aFlowControllerobject fromBatchingSettings.getFlowControlSettings().
-
BatcherImpl
public BatcherImpl(BatchingDescriptor<ElementT,ElementResultT,RequestT,ResponseT> batchingDescriptor, UnaryCallable<RequestT,ResponseT> unaryCallable, RequestT prototype, BatchingSettings batchingSettings, ScheduledExecutorService executor, @Nullable FlowController flowController, @Nullable ApiCallContext callContext)
- Parameters:
batchingDescriptor- aBatchingDescriptorfor transforming individual elements into wrappers request and responseunaryCallable- aUnaryCallableobjectprototype- aBatcherImplobjectbatchingSettings- aBatchingSettingswith configuration of thresholdsflowController- aFlowControllerfor throttling requests. If it's null, create aFlowControllerobject fromBatchingSettings.getFlowControlSettings().callContext- aApiCallContextobject that'll be merged in unaryCallable
-
-
Method Detail
-
add
public ApiFuture<ElementResultT> add(ElementT element)
Queues the passed in element to be sent at some point in the future.The element will be sent as part of a larger batch request at some point in the future. The returned
ApiFuturewill be resolved once the result for the element has been extracted from the batch response.Note: Cancelling returned result simply marks the future cancelled, It would not stop the batch request.
- Specified by:
addin interfaceBatcher<ElementT,ElementResultT>
-
flush
public void flush() throws InterruptedExceptionSynchronously sends any pending elements as a batch and waits for all outstanding batches to be complete.- Specified by:
flushin interfaceBatcher<ElementT,ElementResultT>- Throws:
InterruptedException
-
sendOutstanding
public void sendOutstanding()
Sends accumulated elements asynchronously for batching.Note: This method can be invoked concurrently unlike
Batcher.add(ElementT)andBatcher.close(), which can only be called from a single user thread. Please take caution to avoid race condition.- Specified by:
sendOutstandingin interfaceBatcher<ElementT,ElementResultT>
-
close
public void close() throws InterruptedExceptionCloses this Batcher by preventing new elements from being added, and then flushing the existing elements.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceBatcher<ElementT,ElementResultT>- Throws:
InterruptedException
-
closeAsync
public ApiFuture<Void> closeAsync()
Description copied from interface:BatcherCloses this Batcher by preventing new elements from being added, and then sending outstanding elements. The returned future will be resolved when the last element completes- Specified by:
closeAsyncin interfaceBatcher<ElementT,ElementResultT>
-
getFlowController
@InternalApi("For google-cloud-java client use only") public FlowController getFlowController()
-
-