Class BatchRequest (2.1.0)

public final class BatchRequest

An instance of this class represents a single batch of requests.

Sample use:


 // client is a AbstractGoogleClient (e.g.
 // com.google.api.services.books.Books)
 BatchRequest batch = client.batch(httpRequestInitializer);
 batch.queue(volumesList, Volumes.class, GoogleJsonErrorContainer.class,
     new BatchCallback<Volumes, GoogleJsonErrorContainer>() {

       public void onSuccess(Volumes volumes, HttpHeaders responseHeaders) {
         log("Success");
         printVolumes(volumes.getItems());
       }

       public void onFailure(GoogleJsonErrorContainer e, HttpHeaders responseHeaders) {
         log(e.getError().getMessage());
       }
     });
 batch.queue(volumesList, Volumes.class, GoogleJsonErrorContainer.class,
     new BatchCallback<Volumes, GoogleJsonErrorContainer>() {

       public void onSuccess(Volumes volumes, HttpHeaders responseHeaders) {
         log("Success");
         printVolumes(volumes.getItems());
       }

       public void onFailure(GoogleJsonErrorContainer e, HttpHeaders responseHeaders) {
         log(e.getError().getMessage());
       }
     });
 batch.execute();
 

The content of each individual response is stored in memory. There is thus a potential of encountering an OutOfMemoryError for very large responses.

Redirects are currently not followed in BatchRequest.

Implementation is not thread-safe.

Note: When setting an HttpUnsuccessfulResponseHandler by calling to HttpRequest#setUnsuccessfulResponseHandler, the handler is called for each unsuccessful part. As a result it's not recommended to use HttpBackOffUnsuccessfulResponseHandler on a batch request, since the back-off policy is invoked for each unsuccessful part.

Inheritance

java.lang.Object > BatchRequest

Constructors

BatchRequest(HttpTransport transport, HttpRequestInitializer httpRequestInitializer) (deprecated)

public BatchRequest(HttpTransport transport, HttpRequestInitializer httpRequestInitializer)

Deprecated. Please use AbstractGoogleClient#batch(HttpRequestInitializer) to instantiate your batch request.

Construct the BatchRequest.

Parameters
NameDescription
transportcom.google.api.client.http.HttpTransport

The transport to use for requests

httpRequestInitializercom.google.api.client.http.HttpRequestInitializer

The initializer to use when creating an HttpRequest or null for none

Methods

<T,E>queue(HttpRequest httpRequest, Class<T> dataClass, Class<E> errorClass, BatchCallback<T,E> callback)

public BatchRequest <T,E>queue(HttpRequest httpRequest, Class<T> dataClass, Class<E> errorClass, BatchCallback<T,E> callback)

Queues the specified HttpRequest for batched execution. Batched requests are executed when #execute() is called.

Parameters
NameDescription
httpRequestcom.google.api.client.http.HttpRequest

HTTP Request

dataClassClass<T>

Data class the response will be parsed into or Void.class to ignore the content

errorClassClass<E>

Data class the unsuccessful response will be parsed into or Void.class to ignore the content

callbackBatchCallback<T,E>

Batch Callback

Returns
TypeDescription
BatchRequest

this Batch request

Exceptions
TypeDescription
IOException

If building the HTTP Request fails

execute()

public void execute()

Executes all queued HTTP requests in a single call, parses the responses and invokes callbacks.

Calling #execute() executes and clears the queued requests. This means that the BatchRequest object can be reused to #queue and #execute() requests again.

Exceptions
TypeDescription
IOException

getBatchUrl()

public GenericUrl getBatchUrl()

Returns the URL that will be hit when #execute() is called.

Returns
TypeDescription
com.google.api.client.http.GenericUrl

getSleeper()

public Sleeper getSleeper()

Returns the sleeper.

Returns
TypeDescription
com.google.api.client.util.Sleeper

setBatchUrl(GenericUrl batchUrl)

public BatchRequest setBatchUrl(GenericUrl batchUrl)

Sets the URL that will be hit when #execute() is called. The default value is https://www.googleapis.com/batch.

Parameter
NameDescription
batchUrlcom.google.api.client.http.GenericUrl
Returns
TypeDescription
BatchRequest

setSleeper(Sleeper sleeper)

public BatchRequest setSleeper(Sleeper sleeper)

Sets the sleeper. The default value is Sleeper#DEFAULT.

Parameter
NameDescription
sleepercom.google.api.client.util.Sleeper
Returns
TypeDescription
BatchRequest

size()

public int size()

Returns the number of queued requests in this batch request.

Returns
TypeDescription
int