com.google.api.client.googleapis.batch
Class BatchRequest

java.lang.Object
  extended by com.google.api.client.googleapis.batch.BatchRequest

public final class BatchRequest
extends Object

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

Sample use:

   BatchRequest batch = new BatchRequest(transport, httpRequestInitializer);
   batch.queue(volumesList, Volumes.class, GoogleJsonErrorContainer.class,
       new BatchCallback<Volumes, GoogleJsonErrorContainer>() {

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

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

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

     public void onFailure(GoogleJsonErrorContainer e, GoogleHeaders 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.

Since:
1.9
Author:
[email protected] (Ravi Mistry)

Constructor Summary
BatchRequest(HttpTransport transport, HttpRequestInitializer httpRequestInitializer)
          Construct the BatchRequest.
 
Method Summary
 void execute()
          Executes all queued HTTP requests in a single call, parses the responses and invokes callbacks.
 GenericUrl getBatchUrl()
          Returns the URL that will be hit when execute() is called.
<T,E> BatchRequest
queue(HttpRequest httpRequest, Class<T> dataClass, Class<E> errorClass, BatchCallback<T,E> callback)
          Queues the specified HttpRequest for batched execution.
 BatchRequest setBatchUrl(GenericUrl batchUrl)
          Sets the URL that will be hit when execute() is called.
 int size()
          Returns the number of queued requests in this batch request.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BatchRequest

public BatchRequest(HttpTransport transport,
                    HttpRequestInitializer httpRequestInitializer)
Construct the BatchRequest.

Parameters:
transport - The transport to use for requests
httpRequestInitializer - The initializer to use when creating an HttpRequest or null for none
Method Detail

setBatchUrl

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.


getBatchUrl

public GenericUrl getBatchUrl()
Returns the URL that will be hit when execute() is called.


queue

public <T,E> BatchRequest queue(HttpRequest httpRequest,
                                Class<T> dataClass,
                                Class<E> errorClass,
                                BatchCallback<T,E> callback)
                   throws IOException
Queues the specified HttpRequest for batched execution. Batched requests are executed when execute() is called.

Type Parameters:
T - destination class type
E - error class type
Parameters:
httpRequest - HTTP Request
dataClass - Data class the response will be parsed into or Void.class to ignore the content
errorClass - Data class the unsuccessful response will be parsed into or Void.class to ignore the content
callback - Batch Callback
Returns:
this Batch request
Throws:
IOException - If building the HTTP Request fails

size

public int size()
Returns the number of queued requests in this batch request.


execute

public void execute()
             throws IOException
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(com.google.api.client.http.HttpRequest, java.lang.Class, java.lang.Class, com.google.api.client.googleapis.batch.BatchCallback) and execute() requests again.

Throws:
IOException


Copyright © 2010-2012 Google. All Rights Reserved.