0
# Batch Operations
1
2
Efficient batch request processing to combine multiple API calls into a single HTTP request, reducing network overhead and improving performance.
3
4
## Core Imports
5
6
```java
7
import com.google.api.client.googleapis.batch.BatchRequest;
8
import com.google.api.client.googleapis.batch.BatchCallback;
9
import com.google.api.client.googleapis.batch.json.JsonBatchCallback;
10
import com.google.api.client.googleapis.batch.BatchUnparsedResponse;
11
import com.google.api.client.googleapis.batch.HttpRequestContent;
12
```
13
14
## Batch Request Management
15
16
### BatchRequest
17
18
Container for multiple HTTP requests to be executed as a single batch operation.
19
20
```java { .api }
21
public final class BatchRequest {
22
public BatchRequest(HttpTransport transport, HttpRequestInitializer httpRequestInitializer);
23
24
public <T, E> BatchRequest queue(HttpRequest request, Class<T> dataClass, Class<E> errorClass,
25
BatchCallback<T, E> callback) throws IOException;
26
27
public <T, E> BatchRequest queue(AbstractGoogleClientRequest<T> request, Class<T> dataClass,
28
Class<E> errorClass, BatchCallback<T, E> callback) throws IOException;
29
30
public void execute() throws IOException;
31
32
public int size();
33
34
public BatchRequest setSleeper(Sleeper sleeper);
35
public Sleeper getSleeper();
36
}
37
```
38
39
**Usage Example:**
40
41
```java
42
import com.google.api.client.googleapis.batch.BatchRequest;
43
import com.google.api.client.googleapis.batch.BatchCallback;
44
import com.google.api.client.http.HttpHeaders;
45
46
// Create batch request
47
BatchRequest batch = new BatchRequest(transport, credential);
48
49
// Define callback for handling responses
50
BatchCallback<MyResource, GoogleJsonError> callback = new BatchCallback<MyResource, GoogleJsonError>() {
51
@Override
52
public void onSuccess(MyResource resource, HttpHeaders responseHeaders) throws IOException {
53
System.out.println("Successfully processed: " + resource.getId());
54
}
55
56
@Override
57
public void onFailure(GoogleJsonError error, HttpHeaders responseHeaders) throws IOException {
58
System.err.println("Error: " + error.getMessage());
59
}
60
};
61
62
// Queue multiple requests
63
for (String itemId : itemIds) {
64
MyApiRequest request = myApiClient.items().get(itemId);
65
batch.queue(request, MyResource.class, GoogleJsonError.class, callback);
66
}
67
68
// Execute all requests as a single batch
69
batch.execute();
70
```
71
72
## Batch Callbacks
73
74
### BatchCallback
75
76
Interface for handling batch request responses.
77
78
```java { .api }
79
public interface BatchCallback<T, E> {
80
void onSuccess(T t, HttpHeaders responseHeaders) throws IOException;
81
void onFailure(E e, HttpHeaders responseHeaders) throws IOException;
82
}
83
```
84
85
### JsonBatchCallback
86
87
JSON-specific batch callback implementation.
88
89
```java { .api }
90
public abstract class JsonBatchCallback<T> implements BatchCallback<T, GoogleJsonError> {
91
public abstract void onSuccess(T t, HttpHeaders responseHeaders) throws IOException;
92
public abstract void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) throws IOException;
93
}
94
```
95
96
**Usage Example:**
97
98
```java
99
import com.google.api.client.googleapis.batch.json.JsonBatchCallback;
100
import com.google.api.client.googleapis.json.GoogleJsonError;
101
102
// Use JsonBatchCallback for simplified JSON error handling
103
JsonBatchCallback<MyResource> jsonCallback = new JsonBatchCallback<MyResource>() {
104
@Override
105
public void onSuccess(MyResource resource, HttpHeaders responseHeaders) throws IOException {
106
System.out.println("Resource created: " + resource.getName());
107
System.out.println("ETag: " + responseHeaders.getETag());
108
}
109
110
@Override
111
public void onFailure(GoogleJsonError error, HttpHeaders responseHeaders) throws IOException {
112
System.err.println("Request failed with code: " + error.getCode());
113
System.err.println("Error message: " + error.getMessage());
114
if (error.getErrors() != null) {
115
for (GoogleJsonError.ErrorInfo errorInfo : error.getErrors()) {
116
System.err.println(" - " + errorInfo.getReason() + ": " + errorInfo.getMessage());
117
}
118
}
119
}
120
};
121
122
// Queue request with JSON callback
123
batch.queue(createRequest, MyResource.class, GoogleJsonError.class, jsonCallback);
124
```
125
126
## Advanced Batch Operations
127
128
### Batch Response Handling
129
130
```java
131
// Custom callback with detailed response handling
132
BatchCallback<MyResource, GoogleJsonError> detailedCallback = new BatchCallback<MyResource, GoogleJsonError>() {
133
@Override
134
public void onSuccess(MyResource resource, HttpHeaders responseHeaders) throws IOException {
135
// Process successful response
136
String requestId = responseHeaders.getFirst("X-Request-Id");
137
long rateLimitRemaining = Long.parseLong(responseHeaders.getFirst("X-RateLimit-Remaining"));
138
139
System.out.println("Request ID: " + requestId);
140
System.out.println("Rate limit remaining: " + rateLimitRemaining);
141
142
// Handle the resource data
143
processResource(resource);
144
}
145
146
@Override
147
public void onFailure(GoogleJsonError error, HttpHeaders responseHeaders) throws IOException {
148
// Handle different types of errors
149
switch (error.getCode()) {
150
case 404:
151
System.err.println("Resource not found: " + error.getMessage());
152
break;
153
case 403:
154
System.err.println("Permission denied: " + error.getMessage());
155
break;
156
case 429:
157
System.err.println("Rate limit exceeded, retry after: " +
158
responseHeaders.getRetryAfter());
159
break;
160
default:
161
System.err.println("Unexpected error: " + error.getMessage());
162
}
163
}
164
};
165
```
166
167
### Batch Size Management
168
169
```java
170
import java.util.List;
171
import java.util.ArrayList;
172
173
// Process large number of requests in manageable batches
174
public void processBatchesInChunks(List<String> itemIds, int batchSize) throws IOException {
175
List<List<String>> chunks = partition(itemIds, batchSize);
176
177
for (List<String> chunk : chunks) {
178
BatchRequest batch = new BatchRequest(transport, credential);
179
180
for (String itemId : chunk) {
181
MyApiRequest request = myApiClient.items().get(itemId);
182
batch.queue(request, MyResource.class, GoogleJsonError.class, callback);
183
}
184
185
System.out.println("Executing batch of " + batch.size() + " requests");
186
batch.execute();
187
188
// Optional delay between batches to respect rate limits
189
Thread.sleep(1000);
190
}
191
}
192
193
private <T> List<List<T>> partition(List<T> list, int size) {
194
List<List<T>> chunks = new ArrayList<>();
195
for (int i = 0; i < list.size(); i += size) {
196
chunks.add(list.subList(i, Math.min(i + size, list.size())));
197
}
198
return chunks;
199
}
200
```
201
202
## Internal Batch Components
203
204
### BatchUnparsedResponse
205
206
Represents an unparsed response within a batch operation.
207
208
```java { .api }
209
public class BatchUnparsedResponse {
210
public String getContentId();
211
public int getStatusCode();
212
public String getReasonPhrase();
213
public HttpHeaders getHeaders();
214
public InputStream getContent();
215
}
216
```
217
218
### HttpRequestContent
219
220
Represents HTTP request content for batch operations.
221
222
```java { .api }
223
public class HttpRequestContent implements HttpContent {
224
public HttpRequestContent(HttpRequest request);
225
226
public long getLength() throws IOException;
227
public String getType();
228
public boolean retrySupported();
229
public void writeTo(OutputStream out) throws IOException;
230
}
231
```
232
233
## Error Handling in Batches
234
235
### Individual Request Failures
236
237
```java
238
// Callback that tracks both successes and failures
239
public class BatchTrackingCallback implements BatchCallback<MyResource, GoogleJsonError> {
240
private final AtomicInteger successCount = new AtomicInteger(0);
241
private final AtomicInteger failureCount = new AtomicInteger(0);
242
private final List<String> errors = new ArrayList<>();
243
244
@Override
245
public void onSuccess(MyResource resource, HttpHeaders responseHeaders) throws IOException {
246
successCount.incrementAndGet();
247
// Process successful resource
248
}
249
250
@Override
251
public void onFailure(GoogleJsonError error, HttpHeaders responseHeaders) throws IOException {
252
failureCount.incrementAndGet();
253
errors.add("Error " + error.getCode() + ": " + error.getMessage());
254
}
255
256
public void printSummary() {
257
System.out.println("Batch completed: " + successCount.get() + " successes, " +
258
failureCount.get() + " failures");
259
if (!errors.isEmpty()) {
260
System.out.println("Errors:");
261
errors.forEach(System.out::println);
262
}
263
}
264
}
265
```
266
267
### Retry Logic for Failed Batches
268
269
```java
270
public void executeWithRetry(BatchRequest batch, int maxRetries) throws IOException {
271
int attempt = 0;
272
while (attempt < maxRetries) {
273
try {
274
batch.execute();
275
return; // Success
276
} catch (IOException e) {
277
attempt++;
278
if (attempt >= maxRetries) {
279
throw e; // Final attempt failed
280
}
281
282
// Exponential backoff
283
try {
284
Thread.sleep(Math.min(1000 * (1L << attempt), 30000)); // Max 30 seconds
285
} catch (InterruptedException ie) {
286
Thread.currentThread().interrupt();
287
throw new IOException("Interrupted during retry", ie);
288
}
289
}
290
}
291
}
292
```
293
294
## Best Practices
295
296
### Optimal Batch Sizes
297
298
```java
299
// Recommended batch sizes for different operations
300
public static final int OPTIMAL_READ_BATCH_SIZE = 100;
301
public static final int OPTIMAL_WRITE_BATCH_SIZE = 50;
302
public static final int MAX_BATCH_SIZE = 1000; // Google API limit
303
304
// Adjust batch size based on operation type
305
public BatchRequest createOptimalBatch(String operationType) {
306
BatchRequest batch = new BatchRequest(transport, credential);
307
308
// Configure batch based on operation type
309
switch (operationType) {
310
case "read":
311
// Larger batches for read operations
312
return batch;
313
case "write":
314
// Smaller batches for write operations to handle errors better
315
return batch;
316
default:
317
return batch;
318
}
319
}
320
```
321
322
## Types
323
324
### HttpTransport
325
326
HTTP transport interface for network communication.
327
328
### HttpRequestInitializer
329
330
Interface for initializing HTTP requests.
331
332
### HttpRequest
333
334
HTTP request representation.
335
336
### HttpHeaders
337
338
HTTP headers container.
339
340
### AbstractGoogleClientRequest
341
342
Base class for Google API client requests.
343
344
### GoogleJsonError
345
346
Google JSON error response representation.
347
348
### InputStream
349
350
Java input stream for reading response content.
351
352
### OutputStream
353
354
Java output stream for writing request content.
355
356
### IOException
357
358
Exception for I/O operations.
359
360
### Sleeper
361
362
Interface for controlling sleep behavior during batch operations.
363
364
### Class<T>
365
366
Java class type parameter for response type specification.
367
368
### AtomicInteger
369
370
Thread-safe integer for counting operations in concurrent scenarios.