15

我编写了一个 Java 应用程序,用于在我们的 Google Apps for Education 域上同步 Google 群组(功能类似于Google Apps School Directory Sync,但针对我们的某些特定需求进行了定制)。

同步有效,但速度很慢,因为它是单独执行每个任务。我知道有用于批处理操作的 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
}

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

4

1 回答 1

5

看这里:批处理 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();

记住,那个:

每个部分的正文本身就是一个完整的 HTTP 请求,有自己的动词、URL、标头和正文。HTTP 请求必须只包含 URL 的路径部分;批处理请求中不允许使用完整的 URL。

更新

另请参阅此处,如何使用 Google Batch 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();

从这里开始:使用 Google Storage Json Api (JAVA) 进行批量请求

于 2016-01-21T06:19:50.800 回答