Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batching operations in Google Apps Admin Java API

I have written a Java app that synchronises Google Groups on our Google Apps for Education domain (similar in function to Google Apps School Directory Sync, but customised for some of our specific needs).

The synchronisation works, but it is slow because it is performing each task individually. I know that there are API interfaces for batching operations, but I can't find any examples of how this is implemented with the Java API.

The code I'm using looks similar to this (authentication and other setup is taken care of elsewhere):

try
{
    Member m = new Member ();
    m.setEmail (member);
    m.setRole ("MEMBER");
    service.members ().insert (group, m).execute ();
}
catch (Exception e)
{
    // ERROR handling
}

Instead of executing these operations one-by-one, I would like to batch them instead. Can anyone tell me how?

like image 773
Philip Avatar asked Apr 22 '15 06:04

Philip


1 Answers

Look here: Batch Java API

For example:

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

Remember, that:

The body of each part is itself a complete HTTP request, with its own verb, URL, headers, and body. The HTTP request must only contain the path portion of the URL; full URLs are not allowed in batch requests.

UPDATE

Look also here, how to build batch with Google Batch API:

https://github.com/google/google-api-java-client

UPDATE 2

Try something like this:

// Create the Storage service object
Storage storage = new Storage(httpTransport, jsonFactory, credential);

// Create a new batch request
BatchRequest batch = storage.batch();

// Add some requests to the batch request
storage.objectAccessControls().insert("bucket-name", "object-key1",
    new ObjectAccessControl().setEntity("user-123423423").setRole("READER"))
    .queue(batch, callback);
storage.objectAccessControls().insert("bucket-name", "object-key2",
    new ObjectAccessControl().setEntity("[email protected]").setRole("READER"))
    .queue(batch, callback);
storage.objectAccessControls().insert("bucket-name", "object-key3",
    new ObjectAccessControl().setEntity("[email protected]").setRole("OWNER"))
    .queue(batch, callback);

// Execute the batch request. The individual callbacks will be called when requests finish.
batch.execute();

From here: Batch request with Google Storage Json Api (JAVA)

like image 89
m.aibin Avatar answered Oct 18 '22 23:10

m.aibin