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

标签 java google-admin-sdk google-apps-for-education

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

同步有效,但速度很慢,因为它是单独执行每个任务。我知道 batching operations 有 API 接口(interface),但我找不到任何有关如何使用 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 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();

来自这里:Batch request with Google Storage Json Api (JAVA)

关于java - Google Apps Admin Java API 中的批处理操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29789167/

相关文章:

google-apps-script - Google Apps 脚本 : Multiple recipients in GmailApp?

google-oauth - 在没有域管理员启用 API 访问的情况下,如何访问域上的用户信息?

google-api - 如何授权服务帐户访问 Google Admin API

python - 使用python服务帐户管理gmail签名

single-sign-on - 以 G Suite 作为 SAML 身份提供者的 Office365/Azure AD

java - 如何使用EventBus?

java - 使用 RESTEasy/jax-rs 的字符编码响应

java - xstream 使用 Converter 将复杂的 XML 反序列化为更简单的 java

java - 从数据库表中获取数据