Request Batching

Each HTTP connection that your client makes results in overhead. To reduce overhead, you can batch multiple API calls together into a single HTTP request.

The main classes of interest are BatchRequest and JsonBatchCallback. The following example shows how to use these classes with service-specific generated libraries:

JsonBatchCallback<Calendar> callback = new JsonBatchCallback<Calendar>() {

  public void onSuccess(Calendar calendar, HttpHeaders responseHeaders) {

  public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) {
    System.out.println("Error Message: " + e.getMessage());


Calendar client = Calendar.builder(transport, jsonFactory, credential)
BatchRequest batch = client.batch();

Calendar entry1 = new Calendar().setSummary("Calendar for Testing 1");
client.calendars().insert(entry1).queue(batch, callback);

Calendar entry2 = new Calendar().setSummary("Calendar for Testing 2");
client.calendars().insert(entry2).queue(batch, callback);


A complete example of batch using the Calendar API is available in the calendar-cmdline-sample.

You can also use the batch feature without service-specific generated libraries. To do this:

  1. Change the batch URL to your own URL.
  2. Call BatchRequest.setBatchUrl.
  3. Queue the HTTP requests by calling BatchRequest.queue.

The following example shows how to use batch in this generic way:

  BatchRequest batch = new BatchRequest(httpTransport, httpRequestInitializer);
  batch.setBatchUrl(new GenericUrl(/*your customized batch URL goes here*/));
  batch.queue(httpRequest1, dataClass, errorClass, callback);
  batch.queue(httpRequest1, dataClass, errorClass, callback);