Class BatchHelper


  • public class BatchHelper
    extends Object
    BatchHelper abstracts out the logic for maximum requests per batch, and also allows a workaround for the fact that BatchRequest was made a "final class" for some reason, making it impossible to unittest. Instead, batch interactions with a Storage API client will be funneled through this class, while unittests can inject a mock batch helper.

    This class is thread-safe, because if numThreads is greater than 0, request callbacks will be executed on a different thread(s) than a client thread that queues requests.

    Expected usage is to create a new BatchHelper instance per client operation (copy, rename, delete, etc.) that represent logical grouping of requests.

    Instance of this class can not be used again after flush() method call.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  BatchHelper.Factory
      Since each BatchHelper instance should be tied to a particular related set of requests, use cases will generally interact via an injectable BatchHelper.Factory.
      protected static interface  BatchHelper.QueueRequestCallback
      Callback that causes a single StorageRequest to be added to the BatchRequest.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void flush()
      Sends any currently remaining requests in the batch; should be called at the end of any series of batched requests to ensure everything has been sent.
      boolean isEmpty()
      Returns true if there are no currently queued entries in the batch helper.
      <T> void queue​(com.google.api.services.storage.StorageRequest<T> req, com.google.api.client.googleapis.batch.json.JsonBatchCallback<T> callback)
      Adds an additional request to the batch, and possibly flushes the current contents of the batch if maxRequestsPerBatch has been reached.
    • Method Detail

      • queue

        public <T> void queue​(com.google.api.services.storage.StorageRequest<T> req,
                              com.google.api.client.googleapis.batch.json.JsonBatchCallback<T> callback)
                       throws IOException
        Adds an additional request to the batch, and possibly flushes the current contents of the batch if maxRequestsPerBatch has been reached.
        Throws:
        IOException
      • flush

        public void flush()
                   throws IOException
        Sends any currently remaining requests in the batch; should be called at the end of any series of batched requests to ensure everything has been sent.
        Throws:
        IOException
      • isEmpty

        public boolean isEmpty()
        Returns true if there are no currently queued entries in the batch helper.