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().isEmpty()) {
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()) {
taskName = taskResult.getChosenTaskName();
}
TaskOptions taskResultOptions = new TaskOptions(options);
taskResultOptions.taskName(taskName).payload(addRequest.getBodyAsBytes());
TaskHandle handle = new TaskHandle(taskResultOptions, queueName);
tasks.add(handle.etaUsec(addRequest.getEtaUsec()));
} else if (taskResult.getResult() != TaskQueueServiceError.ErrorCode.SKIPPED.getValue()) {
if (taskqueueException == null ||
taskqueueException instanceof TaskAlreadyExistsException) {
int result = taskResult.getResult();
String detail = (result == TaskQueueServiceError.ErrorCode.UNKNOWN_QUEUE.getValue())