public final class BatchRequest extends Object
Sample use:
BatchRequest batch = new BatchRequest(transport, 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(com.google.api.client.http.HttpUnsuccessfulResponseHandler)
, 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.
Constructor and Description |
---|
BatchRequest(com.google.api.client.http.HttpTransport transport,
com.google.api.client.http.HttpRequestInitializer httpRequestInitializer)
Construct the
BatchRequest . |
Modifier and Type | Method and Description |
---|---|
void |
execute()
Executes all queued HTTP requests in a single call, parses the responses and invokes callbacks.
|
com.google.api.client.http.GenericUrl |
getBatchUrl()
Returns the URL that will be hit when
execute() is called. |
com.google.api.client.util.Sleeper |
getSleeper()
Returns the sleeper.
|
<T,E> BatchRequest |
queue(com.google.api.client.http.HttpRequest httpRequest,
Class<T> dataClass,
Class<E> errorClass,
BatchCallback<T,E> callback)
Queues the specified
HttpRequest for batched execution. |
BatchRequest |
setBatchUrl(com.google.api.client.http.GenericUrl batchUrl)
Sets the URL that will be hit when
execute() is called. |
BatchRequest |
setSleeper(com.google.api.client.util.Sleeper sleeper)
Sets the sleeper.
|
int |
size()
Returns the number of queued requests in this batch request.
|
public BatchRequest(com.google.api.client.http.HttpTransport transport, com.google.api.client.http.HttpRequestInitializer httpRequestInitializer)
BatchRequest
.transport
- The transport to use for requestshttpRequestInitializer
- The initializer to use when creating an HttpRequest
or
null
for nonepublic BatchRequest setBatchUrl(com.google.api.client.http.GenericUrl batchUrl)
execute()
is called. The default value is
https://www.googleapis.com/batch
.public com.google.api.client.http.GenericUrl getBatchUrl()
execute()
is called.public com.google.api.client.util.Sleeper getSleeper()
public BatchRequest setSleeper(com.google.api.client.util.Sleeper sleeper)
Sleeper.DEFAULT
.public <T,E> BatchRequest queue(com.google.api.client.http.HttpRequest httpRequest, Class<T> dataClass, Class<E> errorClass, BatchCallback<T,E> callback) throws IOException
HttpRequest
for batched execution. Batched requests are executed
when execute()
is called.T
- destination class typeE
- error class typehttpRequest
- HTTP RequestdataClass
- Data class the response will be parsed into or Void.class
to ignore
the contenterrorClass
- Data class the unsuccessful response will be parsed into or
Void.class
to ignore the contentcallback
- Batch CallbackIOException
- If building the HTTP Request failspublic int size()
public void execute() throws IOException
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<T>, java.lang.Class<E>, com.google.api.client.googleapis.batch.BatchCallback<T, E>)
and execute()
requests
again.
IOException
Copyright © 2010–2019 Google. All rights reserved.