public Future<List<TaskHandle>> addAsync(
com.google.appengine.api.datastore.Transaction txn, Iterable<TaskOptions> taskOptions) {
final List<TaskOptions> taskOptionsList = new ArrayList<TaskOptions>();
Set<String> taskNames = new HashSet<String>();
final TaskQueueBulkAddRequest bulkAddRequest = new TaskQueueBulkAddRequest();
boolean hasPushTask = false;
boolean hasPullTask = false;
for (TaskOptions option : taskOptions) {
TaskQueueAddRequest addRequest = bulkAddRequest.addAddRequest();
validateAndFillAddRequest(txn, option, addRequest);
if (addRequest.getMode() == TaskQueueMode.Mode.PULL.getValue()) {
hasPullTask = true;
} else {
hasPushTask = true;
}
taskOptionsList.add(option);
if (option.getTaskName() != null && !option.getTaskName().equals("")) {
if (!taskNames.add(option.getTaskName())) {
throw new IllegalArgumentException(
String.format("Identical task names in request : \"%s\" duplicated",
option.getTaskName()));
}
}
}
if (bulkAddRequest.addRequestSize() > QueueConstants.maxTasksPerAdd()) {
throw new IllegalArgumentException(
String.format("No more than %d tasks can be added in a single add call",
QueueConstants.maxTasksPerAdd()));
}
if (hasPullTask && hasPushTask) {
throw new IllegalArgumentException(
"May not add both push tasks and pull tasks in the same call.");
}
if (txn != null &&
bulkAddRequest.encodingSize() > QueueConstants.maxTransactionalRequestSizeBytes()) {
throw new IllegalArgumentException(
String.format("Transactional add may not be larger than %d bytes: %d bytes requested.",
QueueConstants.maxTransactionalRequestSizeBytes(),
bulkAddRequest.encodingSize()));
}
Future<TaskQueueBulkAddResponse> responseFuture = makeAsyncCall(
"BulkAdd", bulkAddRequest, new TaskQueueBulkAddResponse());
return new FutureAdapter<TaskQueueBulkAddResponse, List<TaskHandle>>(responseFuture) {
@Override
protected List<TaskHandle> wrap(TaskQueueBulkAddResponse bulkAddResponse) {
if (bulkAddResponse.taskResultSize() != bulkAddRequest.addRequestSize()) {
throw new InternalFailureException(
String.format("expected %d results from BulkAdd(), got %d",
bulkAddRequest.addRequestSize(), bulkAddResponse.taskResultSize()));
}
List<TaskHandle> tasks = new ArrayList<TaskHandle>();
RuntimeException taskqueueException = null;
for (int i = 0; i < bulkAddResponse.taskResultSize(); ++i) {
TaskQueueBulkAddResponse.TaskResult taskResult = bulkAddResponse.taskResults().get(i);
TaskQueueAddRequest addRequest = bulkAddRequest.getAddRequest(i);
TaskOptions options = taskOptionsList.get(i);
if (taskResult.getResult() == TaskQueueServiceError.ErrorCode.OK.getValue()) {
String taskName = options.getTaskName();
if (taskResult.hasChosenTaskName()) {