Google Apps Admin Java API 中的批处理操作

Batching operations in Google Apps Admin Java API

我已经编写了一个 Java 应用程序,可以同步我们 Google 教育应用程序域中的 Google 组(功能类似于 Google Apps School Directory Sync,但针对某些应用程序进行了定制我们的具体需求)。

同步有效,但速度很慢,因为它是单独执行每个任务。我知道 batching operations 有 API 接口,但我找不到有关如何使用 Java API.

实现的任何示例

我使用的代码看起来与此类似(身份验证和其他设置在别处处理):

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

我不想一个一个地执行这些操作,而是想将它们批处理。谁能告诉我怎么做?

看这里:Batch Java API

例如:

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();

请记住:

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.

更新

也看看这里,如何使用 Google 构建批处理 API:

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

更新 2

尝试这样的事情:

// 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("user-guy@example.com").setRole("READER"))
    .queue(batch, callback);
storage.objectAccessControls().insert("bucket-name", "object-key3",
    new ObjectAccessControl().setEntity("group-foo@googlegroups.com").setRole("OWNER"))
    .queue(batch, callback);

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

从这里开始:Batch request with Google Storage Json Api (JAVA)