use of com.google.apphosting.api.ApiProxy.ApiConfig in project appengine-java-standard by GoogleCloudPlatform.
the class QueueStatistics method fetchForQueuesAsync.
/**
* See {@link Queue#fetchStatistics()}.
*/
static Future<List<QueueStatistics>> fetchForQueuesAsync(final List<Queue> queues, QueueApiHelper helper, double deadlineInSeconds) {
TaskQueueFetchQueueStatsRequest.Builder statsRequest = TaskQueueFetchQueueStatsRequest.newBuilder();
for (Queue queue : queues) {
statsRequest.addQueueName(ByteString.copyFromUtf8(queue.getQueueName()));
}
statsRequest.setMaxNumTasks(0);
ApiConfig apiConfig = new ApiConfig();
apiConfig.setDeadlineInSeconds(deadlineInSeconds);
Future<TaskQueueFetchQueueStatsResponse> responseFuture = helper.makeAsyncCall("FetchQueueStats", statsRequest.build(), TaskQueueFetchQueueStatsResponse.getDefaultInstance(), apiConfig);
return new FutureAdapter<TaskQueueFetchQueueStatsResponse, List<QueueStatistics>>(responseFuture) {
@Override
protected List<QueueStatistics> wrap(TaskQueueFetchQueueStatsResponse statsResponse) {
if (statsResponse.getQueueStatsCount() != queues.size()) {
throw new QueueFailureException("Unable to obtain queue statistics");
}
List<QueueStatistics> resultList = new ArrayList<QueueStatistics>();
for (int i = 0; i < statsResponse.getQueueStatsCount(); ++i) {
TaskQueueFetchQueueStatsResponse.QueueStats stats = statsResponse.getQueueStats(i);
if (!stats.hasScannerInfo()) {
throw new TransientFailureException("Queue statistics temporarily unavailable");
}
TaskQueueScannerQueueInfo scannerInfo = stats.getScannerInfo();
if (!scannerInfo.hasRequestsInFlight()) {
throw new TransientFailureException("Queue statistics temporarily unavailable");
}
if (!scannerInfo.hasEnforcedRate()) {
throw new TransientFailureException("Queue statistics temporarily unavailable");
}
resultList.add(new QueueStatistics(queues.get(i).getQueueName(), stats));
}
return resultList;
}
};
}
use of com.google.apphosting.api.ApiProxy.ApiConfig in project appengine-java-standard by GoogleCloudPlatform.
the class QueueImpl method leaseTasksInternal.
private Future<List<TaskHandle>> leaseTasksInternal(LeaseOptions options) {
long leaseMillis = options.getUnit().toMillis(options.getLease());
if (leaseMillis > QueueConstants.maxLease(MILLISECONDS)) {
throw new IllegalArgumentException(String.format("A lease period can be no longer than %d seconds", QueueConstants.maxLease(SECONDS)));
}
if (options.getCountLimit() > QueueConstants.maxLeaseCount()) {
throw new IllegalArgumentException(String.format("No more than %d tasks can be leased in one call", QueueConstants.maxLeaseCount()));
}
TaskQueueQueryAndOwnTasksRequest.Builder leaseRequest = TaskQueueQueryAndOwnTasksRequest.newBuilder().setQueueName(ByteString.copyFromUtf8(queueName)).setLeaseSeconds(leaseMillis / 1000.0).setMaxTasks(options.getCountLimit());
if (options.getGroupByTag()) {
// You can groupByTag with a null tag. This means "return tasks grouped by the same
// tag as the task of minimum eta".
leaseRequest.setGroupByTag(true);
if (options.getTag() != null) {
leaseRequest.setTag(ByteString.copyFrom(options.getTag()));
}
}
ApiConfig apiConfig = new ApiConfig();
if (options.getDeadlineInSeconds() == null) {
apiConfig.setDeadlineInSeconds(DEFAULT_LEASE_TASKS_DEADLINE_SECONDS);
} else {
apiConfig.setDeadlineInSeconds(options.getDeadlineInSeconds());
}
Future<TaskQueueQueryAndOwnTasksResponse> responseFuture = apiHelper.makeAsyncCall("QueryAndOwnTasks", leaseRequest.build(), TaskQueueQueryAndOwnTasksResponse.getDefaultInstance(), apiConfig);
return new FutureAdapter<TaskQueueQueryAndOwnTasksResponse, List<TaskHandle>>(responseFuture) {
@Override
protected List<TaskHandle> wrap(TaskQueueQueryAndOwnTasksResponse leaseResponse) {
List<TaskHandle> result = new ArrayList<>();
for (TaskQueueQueryAndOwnTasksResponse.Task response : leaseResponse.getTaskList()) {
TaskOptions taskOptions = TaskOptions.Builder.withTaskName(response.getTaskName().toStringUtf8()).payload(response.getBody().toByteArray()).method(TaskOptions.Method.PULL);
if (response.hasTag()) {
taskOptions.tag(response.getTag().toByteArray());
}
TaskHandle handle = new TaskHandle(taskOptions, queueName, response.getRetryCount());
result.add(handle.etaUsec(response.getEtaUsec()));
}
return result;
}
};
}
Aggregations