গুরুত্বপূর্ণ: আমরা আর প্লে EMM API-এর জন্য নতুন নিবন্ধন গ্রহণ করছি না।
আরও জানুন
ব্যাচ অনুরোধ পাঠান
সেভ করা পৃষ্ঠা গুছিয়ে রাখতে 'সংগ্রহ' ব্যবহার করুন
আপনার পছন্দ অনুযায়ী কন্টেন্ট সেভ করুন ও সঠিক বিভাগে রাখুন।
এই নথিটি দেখায় যে কীভাবে আপনার ক্লায়েন্টকে সংযোগ করতে হবে তা কমাতে API কলগুলিকে একসাথে ব্যাচ করতে হয়।
এই নথিটি বিশেষভাবে জাভা ক্লায়েন্ট লাইব্রেরি ব্যবহার করে একটি ব্যাচ অনুরোধ করার বিষয়ে। .NET-এর জন্য Google API ক্লায়েন্ট লাইব্রেরিতে একটি মৌলিক উদাহরণও পাওয়া যায়। Google Play EMM API-এর ব্যাচ সিস্টেম OData ব্যাচ প্রসেসিং সিস্টেমের মতো একই HTTP সিনট্যাক্স ব্যবহার করে।
ওভারভিউ
Google Play EMM API-এর মাধ্যমে আপনার ক্লায়েন্টের প্রতিটি অনুরোধের ফলে একটি নির্দিষ্ট পরিমাণ ওভারহেড হয়। Google Play EMM API ব্যাচিং সমর্থন করে, আপনার ক্লায়েন্টকে একটি একক অনুরোধে একাধিক API কল করার অনুমতি দিতে।
এখানে এমন কিছু উদাহরণ রয়েছে যেখানে আপনি ব্যাচিং ব্যবহার করতে চাইতে পারেন:
- একটি ডোমেন সবেমাত্র নথিভুক্ত করা হয়েছে এবং এখন আপলোড করার জন্য প্রচুর ডেটা রয়েছে৷
- আপনার অ্যাপ্লিকেশন অফলাইনে থাকাকালীন একজন ব্যবহারকারী ডেটাতে পরিবর্তন করেছেন, তাই আপনার অ্যাপ্লিকেশনটিকে সার্ভারের সাথে প্রচুর স্থানীয় ডেটা সিঙ্ক্রোনাইজ করতে হবে৷
এই ধরনের ক্ষেত্রে, প্রতিটি কল আলাদাভাবে পাঠানোর পরিবর্তে আপনি একটি একক অনুরোধে তাদের একসাথে গ্রুপ করতে পারেন। এমনকি আপনি একাধিক ব্যবহারকারীর জন্য বা একাধিক Google API-এর জন্য গ্রুপ অনুরোধ করতে পারেন।
তবে, আপনি একটি একক ব্যাচের অনুরোধে 1000টি কলের মধ্যে সীমাবদ্ধ। আপনি যদি এর চেয়ে বেশি কল করতে চান তবে একাধিক ব্যাচের অনুরোধ ব্যবহার করুন।
ব্যাচের বিবরণ
একটি ব্যাচ অনুরোধ একটি JSON-RPC অনুরোধের সাথে মিলিত একাধিক API কল নিয়ে গঠিত। এই বিভাগটি ব্যাচের অনুরোধের সিনট্যাক্সকে বিস্তারিতভাবে বর্ণনা করে, নিম্নলিখিত বিভাগে একটি উদাহরণ সহ।
দ্রষ্টব্য : একত্রে ব্যাচ করা n অনুরোধের একটি সেট আপনার ব্যবহারের সীমা n অনুরোধ হিসাবে গণনা করে, একটি অনুরোধ হিসাবে নয়। ব্যাচ অনুরোধ প্রক্রিয়াকরণের আগে অনুরোধের একটি সেট আলাদা করা হয়.
জাভা ক্লায়েন্ট লাইব্রেরিতে প্রতিটি Google Play EMM API কলের জন্য অনুরোধ তৈরি করার জন্য কল রয়েছে। উদাহরণস্বরূপ, একটি ডিভাইসে ইনস্টল করা সমস্ত অ্যাপ তালিকাভুক্ত করতে, আপনি নিম্নলিখিতগুলি ব্যবহার করবেন:
AndroidEnterprise enterprise = ...;
InstallsListResponse response = enterprise.installs().list(enterpriseId, userId, deviceId)
.execute();
এখানে একটি অতিরিক্ত batch()
কল রয়েছে যা বেশ কয়েকটি অনুরোধ সারিবদ্ধ করতে পারে, যেমনটি এখানে দেখা গেছে:
AndroidEnterprise enterprise = ...;
BatchRequest batchRequest = enterprise.batch();
enterprise.installs().list(enterpriseId, userId, deviceId1).queue(batchRequest, callback1);
enterprise.installs().list(enterpriseId, userId, deviceId2).queue(batchRequest, callback2);
enterprise.installs().list(enterpriseId, userId, deviceId3).queue(batchRequest, callback3);
batchRequest.execute();
যখন batchRequest.execute()
কল করা হয়, তখন সমস্ত সারিবদ্ধ অনুরোধগুলি একবারে JSON অ্যারে হিসাবে সার্ভারে পাঠানো হয়। সার্ভার প্রতিটি অংশে বাইরের অনুরোধের ক্যোয়ারী প্যারামিটার এবং শিরোনাম (যথাযথ হিসাবে) প্রয়োগ করে এবং তারপর প্রতিটি অংশকে এমনভাবে আচরণ করে যেন এটি একটি পৃথক JSON অনুরোধ। একটি ব্যাচ অনুরোধের প্রতিক্রিয়া
সার্ভার প্রতিটি পৃথক অনুরোধ চালায়, এবং ফলাফলকে একটি একক অ্যারের তৈরি একক প্রতিক্রিয়াতে গোষ্ঠীভুক্ত করে। ক্লায়েন্ট লাইব্রেরি এই প্রতিক্রিয়াটিকে পৃথক প্রতিক্রিয়াগুলিতে বিভক্ত করে, এবং প্রতিটিকে queue()
এ পাঠানো কলব্যাক ফাংশনে পাঠানো হয়। কলব্যাক হল একটি ইন্টারফেস যা ব্যর্থতার জন্য একটি পদ্ধতি এবং সাফল্যের জন্য একটি পদ্ধতি নির্ধারণ করে। উদাহরণস্বরূপ, callback1
নিম্নলিখিত উদাহরণ হিসাবে প্রয়োগ করা হবে:
private class InstallsCallback implements JsonBatchCallback<InstallsListResponse> {
@Override
public void onSuccess(InstallsListResponse response, HttpHeaders responseHeaders) {
...
}
@Override
public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) {
...
}
}
দ্রষ্টব্য : সার্ভার যেকোনো ক্রমে আপনার কলগুলি সম্পাদন করতে পারে, তাই আপনার অনুরোধে উল্লেখিত ক্রম অনুসারে ফলাফল পাওয়ার উপর নির্ভর করবেন না। আপনি যদি নিশ্চিত করতে চান যে দুটি কল একটি প্রদত্ত ক্রমে হয়, আপনি সেগুলিকে একক অনুরোধে পাঠাতে পারবেন না; পরিবর্তে, প্রথম অনুরোধ নিজেই পাঠান, এবং দ্বিতীয়টি পাঠানোর আগে একটি প্রতিক্রিয়ার জন্য অপেক্ষা করুন।
উদাহরণ ব্যাচ অনুরোধ
নিম্নলিখিত উদাহরণটি দেখায় কিভাবে একটি প্রদত্ত ব্যবহারকারীর ডিভাইসে ইনস্টল করা সমস্ত অ্যাপ তালিকাভুক্ত করা যায়। প্রথম কলগুলি এন্টারপ্রাইজ এবং ব্যবহারকারীর আইডি প্রাপ্ত করতে ব্যবহৃত হয় এবং সেই অনুযায়ী ক্রমানুসারে কার্যকর করা আবশ্যক। একবার enterprise.devices().list()
দিয়ে সমস্ত ডিভাইস আইডি প্রাপ্ত হয়ে গেলে, আমরা একবারে সমস্ত ব্যবহারকারীর ডিভাইসে সমস্ত অ্যাপ্লিকেশন পুনরুদ্ধার করার জন্য একটি ব্যাচ অনুরোধ করতে পারি।
package com.google.playenterprise.example;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.batch.BatchRequest;
import com.google.api.client.googleapis.batch.json.JsonBatchCallback;
import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.androidenterprise.AndroidEnterprise;
import com.google.api.services.androidenterprise.AndroidEnterprise.Installs;
import com.google.api.services.androidenterprise.AndroidEnterpriseScopes;
import com.google.api.services.androidenterprise.model.Device;
import com.google.api.services.androidenterprise.model.DevicesListResponse;
import com.google.api.services.androidenterprise.model.Enterprise;
import com.google.api.services.androidenterprise.model.Install;
import com.google.api.services.androidenterprise.model.InstallsListResponse;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* Lists all the apps installed on all devices of a given user.
*/
public class ListAllInstalls {
private AndroidEnterprise enterprise;
private final List<String> installList = new ArrayList<>();
public static void main(String[] argv) throws Exception {
if (argv.length != 2) {
throw new IllegalArgumentException("Usage: ListAllInstalls email jsonFilename");
} else if (!argv[0].contains("@")) {
throw new IllegalArgumentException("First parameter should be a valid email.");
}
new ListAllInstalls().run(argv[0], argv[1]);
}
private void run(String userEmail, String jsonKeyPath) throws IOException {
enterprise = createAndroidEnterprise(jsonKeyPath);
// Get the enterprise id, user id, and user devices.
String domain = userEmail.split("@")[1];
List<Enterprise> results = enterprise.enterprises().list(domain).execute().getEnterprise();
if (results.isEmpty()) {
throw new RuntimeException("No enterprise found.");
}
String enterpriseId = results.get(0).getId();
String userId = enterprise
.users()
.list(enterpriseId, userEmail)
.execute()
.getUser()
.get(0)
.getId();
List<Device> devices = getAllDevices(enterpriseId, userId);
// Batch all calls to get installs on all user devices.
gatherAllInstalls(enterpriseId, userId, devices);
for (String entry : installList) {
// Do something.
System.out.println(entry);
}
}
private List<Device> getAllDevices(String enterpriseId, String userId) throws IOException {
DevicesListResponse devices = enterprise.devices().list(enterpriseId, userId).execute();
return devices.getDevice();
}
private void gatherAllInstalls(String enterpriseId, String userId, List<Device> devices)
throws IOException {
BatchRequest batchRequest = enterprise.batch();
for (Device device : devices) {
Installs.List list = enterprise
.installs().list(enterpriseId, userId, device.getAndroidId());
// Each callback can take the specifics of the associated request in its constructor.
list.queue(batchRequest, new InstallsCallback(device.getAndroidId()));
}
// Executes all the queued requests and their callbacks, single-threaded.
batchRequest.execute();
}
private class InstallsCallback extends JsonBatchCallback<InstallsListResponse> {
private final String androidId;
InstallsCallback(String androidId) {
this.androidId = androidId;
}
@Override
public void onSuccess(InstallsListResponse response, HttpHeaders responseHeaders) {
for (Install install : response.getInstall()) {
installList.add(androidId + "," + install.getProductId());
}
}
@Override
public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) {
throw new RuntimeException("Error fetching a device");
}
}
private AndroidEnterprise createAndroidEnterprise(String jsonKeyPath) throws IOException {
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
InputStream is = new BufferedInputStream(new FileInputStream(jsonKeyPath));
final Credential credential = GoogleCredential.fromStream(is, httpTransport, jsonFactory)
.createScoped(AndroidEnterpriseScopes.all());
HttpRequestInitializer httpRequestInitializer = new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) throws IOException {
credential.initialize(request);
}
};
return new AndroidEnterprise.Builder(httpTransport, jsonFactory, httpRequestInitializer)
.build();
}
}
অন্য কিছু উল্লেখ না করা থাকলে, এই পৃষ্ঠার কন্টেন্ট Creative Commons Attribution 4.0 License-এর অধীনে এবং কোডের নমুনাগুলি Apache 2.0 License-এর অধীনে লাইসেন্স প্রাপ্ত। আরও জানতে, Google Developers সাইট নীতি দেখুন। Java হল Oracle এবং/অথবা তার অ্যাফিলিয়েট সংস্থার রেজিস্টার্ড ট্রেডমার্ক।
2025-05-08 UTC-তে শেষবার আপডেট করা হয়েছে।
[null,null,["2025-05-08 UTC-তে শেষবার আপডেট করা হয়েছে।"],[[["\u003cp\u003eBatching API calls in the Google Play EMM API reduces overhead by combining multiple calls into a single HTTP request.\u003c/p\u003e\n"],["\u003cp\u003eThis document focuses on batch requests using the Java client library, with an example demonstrating how to list all apps installed on a user's devices.\u003c/p\u003e\n"],["\u003cp\u003eBatch requests are limited to 1000 calls and count toward usage limits as individual requests.\u003c/p\u003e\n"],["\u003cp\u003eThe server may process batched calls in any order, requiring separate requests for sequential execution.\u003c/p\u003e\n"],["\u003cp\u003eResponses to batch requests are handled by callbacks defined for each individual request within the batch.\u003c/p\u003e\n"]]],[],null,["# Send Batch Requests\n\nThis document shows how to batch API calls together to reduce the number of connections your client has to make.\n\nThis document is specifically about making a batch request using the Java client library.\nA basic example is also available in the [Google API Client Library for .NET](https://developers.google.com/api-client-library/dotnet/guide/batch). The batch system for the Google Play EMM API uses the same HTTP\nsyntax as the [OData batch processing](http://www.odata.org/documentation/odata-version-3-0/batch-processing/) system.\n\nOverview\n--------\n\nEach request that your client makes via the Google Play EMM API results in a certain amount of overhead. The Google Play EMM API supports batching, to allow your client to put several API calls into a single request.\n\nHere are some examples of situations in which you might want to use batching:\n\n- A domain has just been enrolled and now has a lot of data to upload.\n- A user made changes to data while your application was offline, so your application needs to synchronize a great deal of local data with the server.\n\nIn cases such as these, instead of sending each call separately you can group them together into a single request. You can even group requests for multiple users, or for multiple Google APIs.\n\nYou are, however, limited to 1000 calls in a single batch request. If you need to make more calls than that, use multiple batch requests.\n\nBatch details\n-------------\n\nA batch request consists of multiple API calls combined into one JSON-RPC request. This section describes the batch request syntax in detail, with an [example](#example) in the section following.\n\n**Note** : A set of \u003cvar translate=\"no\"\u003en\u003c/var\u003e requests batched together counts toward your usage limit as \u003cvar translate=\"no\"\u003en\u003c/var\u003e requests, not as one request. The batch request is taken apart into a set of requests before processing.\n\n### Format of a batch request\n\nThe Java client library contains calls to create requests for each Google Play EMM API call. For instance, to list all apps installed on a device, you would use the following: \n\n```\nAndroidEnterprise enterprise = ...;\nInstallsListResponse response = enterprise.installs().list(enterpriseId, userId, deviceId)\n .execute();\n```\n\nThere is an additional `batch()` call that can queue several requests, as seen here: \n\n```\nAndroidEnterprise enterprise = ...;\nBatchRequest batchRequest = enterprise.batch();\nenterprise.installs().list(enterpriseId, userId, deviceId1).queue(batchRequest, callback1);\nenterprise.installs().list(enterpriseId, userId, deviceId2).queue(batchRequest, callback2);\nenterprise.installs().list(enterpriseId, userId, deviceId3).queue(batchRequest, callback3);\nbatchRequest.execute();\n```\nWhen `batchRequest.execute()` is called, all the queued requests are sent at once to the server as a JSON array. The server applies the outer request's query parameters and headers (as appropriate) to each part, and then treats each part as if it were a separate JSON request.\n\n\u003cbr /\u003e\n\n### Response to a batch request\n\nThe server executes each separate request, and groups the result into single response made of a single array. The client library splits this response into individual responses, and each one is sent to the callback function passed to `queue()`. The callback is an interface defining a method for failure and a method for success. For instance, `callback1` would be implemented as an instance of the following: \n\n```\nprivate class InstallsCallback implements JsonBatchCallback\u003cInstallsListResponse\u003e {\n\n @Override\n public void onSuccess(InstallsListResponse response, HttpHeaders responseHeaders) {\n ...\n }\n\n @Override\n public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) {\n ...\n }\n}\n```\n\n**Note**: The server may perform your calls in any order, so don't rely on receiving results in the order specified in your request. If you want to ensure that two calls occur in a given order, you can't send them in a single request; instead, send the first request by itself, and wait for a response before sending the second.\n\nExample batch request\n---------------------\n\nThe following example shows how to list all the apps installed on all of a given user devices. The first calls are used to obtain the id of the enterprise and of the user, and accordingly must be executed sequentially. Once all the device ids have been obtained with `enterprise.devices().list()`, we can do a batch request to retrieve all of the applications on all the user's devices at once. \n\n```\npackage com.google.playenterprise.example;\n\nimport com.google.api.client.auth.oauth2.Credential;\nimport com.google.api.client.googleapis.auth.oauth2.GoogleCredential;\nimport com.google.api.client.googleapis.batch.BatchRequest;\nimport com.google.api.client.googleapis.batch.json.JsonBatchCallback;\nimport com.google.api.client.googleapis.json.GoogleJsonError;\nimport com.google.api.client.http.HttpHeaders;\nimport com.google.api.client.http.HttpRequest;\nimport com.google.api.client.http.HttpRequestInitializer;\nimport com.google.api.client.http.HttpTransport;\nimport com.google.api.client.http.javanet.NetHttpTransport;\nimport com.google.api.client.json.JsonFactory;\nimport com.google.api.client.json.jackson.JacksonFactory;\nimport com.google.api.services.androidenterprise.AndroidEnterprise;\nimport com.google.api.services.androidenterprise.AndroidEnterprise.Installs;\nimport com.google.api.services.androidenterprise.AndroidEnterpriseScopes;\nimport com.google.api.services.androidenterprise.model.Device;\nimport com.google.api.services.androidenterprise.model.DevicesListResponse;\nimport com.google.api.services.androidenterprise.model.Enterprise;\nimport com.google.api.services.androidenterprise.model.Install;\nimport com.google.api.services.androidenterprise.model.InstallsListResponse;\n\nimport java.io.BufferedInputStream;\nimport java.io.FileInputStream;\nimport java.io.IOException;\nimport java.io.InputStream;\nimport java.util.ArrayList;\nimport java.util.List;\n\n/**\n * Lists all the apps installed on all devices of a given user.\n */\npublic class ListAllInstalls {\n private AndroidEnterprise enterprise;\n private final List\u003cString\u003e installList = new ArrayList\u003c\u003e();\n\n public static void main(String[] argv) throws Exception {\n if (argv.length != 2) {\n throw new IllegalArgumentException(\"Usage: ListAllInstalls email jsonFilename\");\n } else if (!argv[0].contains(\"@\")) {\n throw new IllegalArgumentException(\"First parameter should be a valid email.\");\n }\n new ListAllInstalls().run(argv[0], argv[1]);\n }\n\n private void run(String userEmail, String jsonKeyPath) throws IOException {\n enterprise = createAndroidEnterprise(jsonKeyPath);\n\n // Get the enterprise id, user id, and user devices.\n String domain = userEmail.split(\"@\")[1];\n List\u003cEnterprise\u003e results = enterprise.enterprises().list(domain).execute().getEnterprise();\n if (results.isEmpty()) {\n throw new RuntimeException(\"No enterprise found.\");\n }\n String enterpriseId = results.get(0).getId();\n String userId = enterprise\n .users()\n .list(enterpriseId, userEmail)\n .execute()\n .getUser()\n .get(0)\n .getId();\n List\u003cDevice\u003e devices = getAllDevices(enterpriseId, userId);\n\n // Batch all calls to get installs on all user devices.\n gatherAllInstalls(enterpriseId, userId, devices);\n\n for (String entry : installList) {\n // Do something.\n System.out.println(entry);\n }\n }\n\n private List\u003cDevice\u003e getAllDevices(String enterpriseId, String userId) throws IOException {\n DevicesListResponse devices = enterprise.devices().list(enterpriseId, userId).execute();\n return devices.getDevice();\n }\n\n private void gatherAllInstalls(String enterpriseId, String userId, List\u003cDevice\u003e devices)\n throws IOException {\n BatchRequest batchRequest = enterprise.batch();\n for (Device device : devices) {\n Installs.List list = enterprise\n .installs().list(enterpriseId, userId, device.getAndroidId());\n // Each callback can take the specifics of the associated request in its constructor.\n list.queue(batchRequest, new InstallsCallback(device.getAndroidId()));\n }\n // Executes all the queued requests and their callbacks, single-threaded.\n batchRequest.execute();\n }\n\n private class InstallsCallback extends JsonBatchCallback\u003cInstallsListResponse\u003e {\n private final String androidId;\n\n InstallsCallback(String androidId) {\n this.androidId = androidId;\n }\n\n @Override\n public void onSuccess(InstallsListResponse response, HttpHeaders responseHeaders) {\n for (Install install : response.getInstall()) {\n installList.add(androidId + \",\" + install.getProductId());\n }\n }\n\n @Override\n public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) {\n throw new RuntimeException(\"Error fetching a device\");\n }\n }\n\n private AndroidEnterprise createAndroidEnterprise(String jsonKeyPath) throws IOException {\n HttpTransport httpTransport = new NetHttpTransport();\n JsonFactory jsonFactory = new JacksonFactory();\n\n InputStream is = new BufferedInputStream(new FileInputStream(jsonKeyPath));\n final Credential credential = GoogleCredential.fromStream(is, httpTransport, jsonFactory)\n .createScoped(AndroidEnterpriseScopes.all());\n\n HttpRequestInitializer httpRequestInitializer = new HttpRequestInitializer() {\n @Override\n public void initialize(HttpRequest request) throws IOException {\n credential.initialize(request);\n }\n };\n return new AndroidEnterprise.Builder(httpTransport, jsonFactory, httpRequestInitializer)\n .build();\n }\n}\n```"]]