Batching operations in Google Apps Admin Java API
Asked Answered
U

1

15

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?

Uncork answered 22/4, 2015 at 6:12 Comment(0)
M
5

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)

Morpheme answered 21/1, 2016 at 6:19 Comment(2)
Could you show me how you would modify the example code in my question to be queued and ready for batch execution? I can't see where variables like httpRequest1, in your example, would come from in my code snippet.Uncork
httpRequest1 will be one whole request from your code (whole try block). Then, you need to wrap it into httpRequest and do this for all iterations. Finally, you can execute batch. Batch Java API specify how the httpRequest should look like.Morpheme

© 2022 - 2024 — McMap. All rights reserved.