BatchRequestService

public class BatchRequestService extends AbstractIdleService

Batch multiple requests. Use add(AsyncRequest) to batch requests. Caller can use returned ListenableFuture to get result of batched request. Use isRunning() to determine if more requests can be batched. Set BatchPolicy to specify max batch request size or batching delay. Requests will be automatically flushed once flush policy is satisfied. Use flush() to flush current batched requests.

TODO(tvartak) : Use executor service to set values or exceptions on settable future with timeout. Caller can potentially add long running callback or listener with newDirectExecutorService(). These should not block other threads.

Nested Class Summary

class BatchRequestService.Builder Builder object for creating an instance of BatchRequestService  
interface BatchRequestService.ExecutorFactory Factory for obtaining ExecutorService instance. 
class BatchRequestService.ExecutorFactoryImpl BatchRequestService.ExecutorFactory implementation to get ExecutorService and ScheduledExecutorService instances used by BatchRequestService
class BatchRequestService.SystemTimeProvider BatchRequestService.TimeProvider implementation to get current system time. 
interface BatchRequestService.TimeProvider Interface for classes that can provide the current time. 

Public Constructor Summary

Public Method Summary

<T> void
add(AsyncRequest<T> request)
Adds an request to batch request.
ListenableFuture<Integer>
flush()
Flushes all enqueued requests for batched execution.
int
getCurrentBatchSize()
Returns number of elements enqueued for batched execution.

Inherited Method Summary

Public Constructors

public BatchRequestService (BatchRequestService.Builder builder)

Create an instance of BatchRequestService

Parameters
builder for creating BatchRequestService

Public Methods

public void add (AsyncRequest<T> request)

Adds an request to batch request. If current batch size is greater than or equal to getMaxBatchSize(), current batched requests will be automatically flushed. Operation might block if BatchRequestService can not accept more request immediately.

Parameters
request to be batched
Throws
InterruptedException

public ListenableFuture<Integer> flush ()

Flushes all enqueued requests for batched execution.

Returns
  • number of requests flushed for batched execution.
Throws
InterruptedException if interrupted while executing batch requests.

public int getCurrentBatchSize ()

Returns number of elements enqueued for batched execution.

Returns
  • number of elements enqueued for batched execution.