1 // Copyright 2010 Google Inc. All rights reserved.
2 package com
.google
.appengine
.api
.taskqueue
;
4 import com
.google
.appengine
.api
.datastore
.Transaction
;
6 import java
.util
.concurrent
.Future
;
7 import java
.util
.concurrent
.TimeUnit
;
10 * {@link Queue} is used to manage a task queue.
11 * <p>Implementations of this interface must be threadsafe.
13 * <p>Queues are transactional. If a datastore transaction is in progress when
14 * {@link #add()} or {@link #add(TaskOptions)} is invoked, the task will only
15 * be added to the queue if the datastore transaction successfully commits. If
16 * you want to add a task to a queue and have that operation succeed or fail
17 * independently of an existing datastore transaction you can invoke
18 * {@link #add(Transaction, TaskOptions)} with a {@code null} transaction
19 * argument. Note that while the addition of the task to the queue can
20 * participate in an existing transaction, the execution of the task cannot
21 * participate in this transaction. In other words, when the transaction
22 * commits you are guaranteed that your task will be added and run, not that
23 * your task executed successfully.
24 * <p> Queues may be configured in either push or pull mode, but they share the
25 * same interface. However, only tasks with {@link TaskOptions.Method#PULL} may
26 * be added to pull queues. The tasks in push queues must be added with one of
27 * the other available methods.
28 * <p>Pull mode queues do not automatically deliver tasks to the application.
29 * The application is required to call
30 * {@link #leaseTasks(long, TimeUnit, long) leaseTasks} to acquire a lease on
31 * the task and process them explicitly. Attempting to call
32 * {@link #leaseTasks(long, TimeUnit, long) leaseTasks} on a push queue causes
33 * a {@link InvalidQueueModeException} to be thrown. When the task processing
34 * has finished processing a task that is leased, it should call
35 * {@link #deleteTask(String)}. If deleteTask is not called before the lease
36 * expires, the task will again be available for lease.
38 * <p> Queue mode can be switched between push and pull. When switching from
39 * push to pull, tasks will stay in the task queue and are available for lease,
40 * but url and headers information will be ignored when returning the tasks.
41 * When switching from pull to push, existing tasks will remain in the queue but
42 * will fail on auto-execution because they lack a url. If the queue mode is
43 * once again changed to pull, these tasks will eventually be available for
47 public interface Queue
{
49 * The default queue name.
51 String DEFAULT_QUEUE
= "default";
54 * The default queue path.
56 String DEFAULT_QUEUE_PATH
= "/_ah/queue";
59 * Returns the queue name.
61 String
getQueueName();
64 * Submits a task to this queue with an auto generated name with default
66 * <p>This method is similar to calling {@link #add(TaskOptions)} with
67 * a {@link TaskOptions} object returned by
68 * {@link TaskOptions.Builder#withDefaults()}.
69 * @return A {@link TaskHandle}.
70 * @throws InternalFailureException
71 * @throws IllegalStateException If the queue does not exist. (see queue.xml)
72 * @throws TransientFailureException Attempting the request after this exception may succeed.
73 * @throws InvalidQueueModeException task method is {@link TaskOptions.Method#PULL} and queue is
74 * push queue or vice versa.
79 * Submits a task to this queue.
80 * @param taskOptions The definition of the task.
81 * @return A {@link TaskHandle}.
82 * @throws InternalFailureException
83 * @throws IllegalStateException If the queue does not exist. (see queue.xml)
84 * @throws TaskAlreadyExistsException
85 * @throws TransientFailureException Attempting the request after this exception may succeed.
86 * @throws UnsupportedTranslationException If chosen character encoding is unsupported.
87 * @throws InvalidQueueModeException task method is {@link TaskOptions.Method#PULL} and queue is
88 * push queue or vice versa.
90 TaskHandle
add(TaskOptions taskOptions
);
93 * Submits tasks to this queue.
94 * <p>Submission is not atomic i.e. if this method throws then some tasks may have been added to
96 * @param taskOptions An iterable over task definitions.
97 * @return A list containing a {@link TaskHandle} for each added task.
98 * @throws InternalFailureException
99 * @throws IllegalStateException If the queue does not exist. (see queue.xml)
100 * @throws TaskAlreadyExistsException if a task with the same name was previously created.
101 * @throws TransientFailureException Attempting the request after this exception may succeed.
102 * @throws UnsupportedTranslationException If chosen character encoding is unsupported.
103 * @throws InvalidQueueModeException task method is {@link TaskOptions.Method#PULL} and queue is
104 * push queue or vice versa.
106 List
<TaskHandle
> add(Iterable
<TaskOptions
> taskOptions
);
109 * Submits a task to this queue in the provided Transaction.
110 * <p>A task is added if and only if the transaction is applied successfully.
111 * @param txn an enclosing {@link Transaction} or null, if not null a task cannot be named.
112 * @param taskOptions The definition of the task.
113 * @return A {@link TaskHandle}.
114 * @throws InternalFailureException
115 * @throws IllegalStateException If the queue does not exist. (see queue.xml)
116 * @throws TaskAlreadyExistsException if a task with the same name was previously created.
117 * @throws TransientFailureException Attempting the request after this exception may succeed.
118 * @throws UnsupportedTranslationException If chosen character encoding is unsupported.
119 * @throws InvalidQueueModeException task method is {@link TaskOptions.Method#PULL} and queue is
120 * push queue or vice versa.
122 TaskHandle
add(Transaction txn
, TaskOptions taskOptions
);
125 * Submits tasks to this queue in the provided Transaction.
126 * <p>The tasks are added if and only if the transaction is applied successfully.
127 * @param txn an enclosing {@link Transaction} or null, if not null a task cannot be named.
128 * @param taskOptions An iterable over task definitions.
129 * @return A list containing a {@link TaskHandle} for each added task.
130 * @throws InternalFailureException
131 * @throws IllegalStateException If the queue does not exist. (see queue.xml)
132 * @throws TaskAlreadyExistsException if a task with the same name was previously created.
133 * @throws TransientFailureException Attempting the request after this exception may succeed.
134 * @throws UnsupportedTranslationException If chosen character encoding is unsupported.
135 * @throws InvalidQueueModeException task method is {@link TaskOptions.Method#PULL} and queue is
136 * push queue or vice versa.
138 List
<TaskHandle
> add(Transaction txn
, Iterable
<TaskOptions
> taskOptions
);
141 * Asynchronously submits a task to this queue with an auto generated name with default
143 * <p>This method is similar to calling {@link #addAsync(TaskOptions)} with
144 * a {@link TaskOptions} object returned by
145 * {@link TaskOptions.Builder#withDefaults()}.
146 * @return A {@code Future} with a result type of {@link TaskHandle}.
148 Future
<TaskHandle
> addAsync();
151 * Asynchronously submits a task to this queue.
152 * @param taskOptions The definition of the task.
153 * @return A {@code Future} with a result type of {@link TaskHandle}.
154 * @throws UnsupportedTranslationException If chosen character encoding is unsupported.
156 Future
<TaskHandle
> addAsync(TaskOptions taskOptions
);
159 * Asynchronously submits tasks to this queue.
160 * <p>Submission is not atomic i.e. if this method fails then some tasks may have been added to
162 * @param taskOptions An iterable over task definitions.
163 * @return A {@code Future} whose result is a list containing a {@link TaskHandle} for each added
165 * @throws UnsupportedTranslationException If chosen character encoding is unsupported.
167 Future
<List
<TaskHandle
>> addAsync(Iterable
<TaskOptions
> taskOptions
);
170 * Asynchronously submits a task to this queue in the provided Transaction.
171 * <p>A task is added if and only if the transaction is applied successfully.
172 * @param txn an enclosing {@link Transaction} or null, if not null a task cannot be named.
173 * @param taskOptions The definition of the task.
174 * @return A {@code Future} with a result type of {@link TaskHandle}.
175 * @throws UnsupportedTranslationException If chosen character encoding is unsupported.
177 Future
<TaskHandle
> addAsync(Transaction txn
, TaskOptions taskOptions
);
180 * Asynchronously submits tasks to this queue in the provided Transaction.
181 * <p>The tasks are added if and only if the transaction is applied successfully.
182 * @param txn an enclosing {@link Transaction} or null, if not null a task cannot be named.
183 * @param taskOptions An iterable over task definitions.
184 * @return A {@code Future} whose result is a list containing a {@link TaskHandle} for each added
186 * @throws UnsupportedTranslationException If chosen character encoding is unsupported.
188 Future
<List
<TaskHandle
>> addAsync(
189 Transaction txn
, Iterable
<TaskOptions
> taskOptions
);
192 * Deletes a task from this {@link Queue}. Task is identified by taskName.
193 * @param taskName name of the task to delete.
194 * @return True if the task was sucessfully deleted. False if the task was not found or was
195 * previously deleted.
196 * @throws IllegalArgumentException if the provided name is null, empty or doesn't match the
198 * @throws InternalFailureException
199 * @throws IllegalStateException If the queue does not exist. (see queue.xml)
200 * @throws TransientFailureException Attempting the request after this exception may succeed.
202 boolean deleteTask(String taskName
);
205 * Deletes a task from this {@link Queue}. Task is identified by a TaskHandle.
206 * @param taskHandle handle of the task to delete.
207 * @return True if the task was sucessfully deleted. False if the task was not found or was
208 * previously deleted.
209 * @throws IllegalArgumentException if the provided name is null, empty or doesn't match the
211 * @throws InternalFailureException
212 * @throws IllegalStateException If the queue does not exist. (see queue.xml)
213 * @throws QueueNameMismatchException if the task handle refers to a different named queue.
214 * @throws TransientFailureException Attempting the request after this exception may succeed.
216 boolean deleteTask(TaskHandle taskHandle
);
219 * Deletes a list of tasks from this {@link Queue}. The tasks are identified
220 * by a list of TaskHandles.
221 * @param taskHandles list of handles of tasks to delete.
222 * @return List<Booloean> that represents the result of deleting each task in
223 * the same order as the input handles. True if a task was sucessfully deleted.
224 * False if the task was not found or was previously deleted.
225 * @throws IllegalArgumentException if the provided name is null, empty or doesn't match the
227 * @throws InternalFailureException
228 * @throws IllegalStateException If the queue does not exist. (see queue.xml)
229 * @throws QueueNameMismatchException if the task handle refers to a different named queue.
230 * @throws TransientFailureException Attempting the request after this exception may succeed.
232 List
<Boolean
> deleteTask(List
<TaskHandle
> taskHandles
);
235 * Asynchronously deletes a task from this {@link Queue}. Task is identified by taskName.
236 * @param taskName name of the task to delete.
237 * @return A {@code Future} whose result is True if the task was sucessfully deleted, False if the
238 * task was not found or was previously deleted.
239 * @throws IllegalArgumentException if the provided name is null, empty or doesn't match the
242 Future
<Boolean
> deleteTaskAsync(String taskName
);
245 * Asynchronously deletes a task from this {@link Queue}. Task is identified by a TaskHandle.
246 * @param taskHandle handle of the task to delete.
247 * @return A {@code Future} whose result is True if the task was sucessfully deleted, False if the
248 * task was not found or was previously deleted.
249 * @throws IllegalArgumentException if the provided name is null, empty or doesn't match the
251 * @throws QueueNameMismatchException if the task handle refers to a different named queue.
253 Future
<Boolean
> deleteTaskAsync(TaskHandle taskHandle
);
256 * Asynchronously deletes a list of tasks from this {@link Queue}. The tasks are identified
257 * by a list of TaskHandles.
258 * @param taskHandles list of handles of tasks to delete.
259 * @return A {@code Future} whose result is a List<Booloean> that represents the result of
260 * deleting each task in the same order as the input handles. True if a task was
261 * sucessfully deleted. False if the task was not found or was previously deleted.
262 * @throws IllegalArgumentException if the provided name is null, empty or doesn't match the
264 * @throws QueueNameMismatchException if the task handle refers to a different named queue.
266 Future
<List
<Boolean
>> deleteTaskAsync(List
<TaskHandle
> taskHandles
);
269 * Leases up to {@code countLimit} tasks from this queue for a period specified by
270 * {@code lease} and {@code unit}. If fewer tasks than {@code countLimit} are available, all
271 * available tasks in this {@link Queue} will be returned. The available tasks are those in the
272 * queue having the earliest eta such that eta is prior to the time at which the lease is
273 * requested. It is guaranteed that the leased tasks will be unavailable for lease to others in
274 * the lease period. You must call deleteTask to prevent the task from being leased again after
275 * the lease period. This method supports leasing a maximum of 1000 tasks for no more than one
277 * @param lease Number of {@code unit}s in the lease period
278 * @param unit Time unit of the lease period
279 * @param countLimit maximum number of tasks to lease
280 * @return A list of {@link TaskHandle} for each leased task.
281 * @throws InvalidQueueModeException if the target queue is not in pull mode.
282 * @throws IllegalArgumentException if lease < 0, countLimit <= 0 or either is too large.
283 * @throws InternalFailureException
284 * @throws IllegalStateException If the queue does not exist. (see queue.xml)
285 * @throws TransientFailureException Attempting the request after this exception may succeed.
287 List
<TaskHandle
> leaseTasks(long lease
, TimeUnit unit
, long countLimit
);
290 * Leases up to {@code countLimit} tasks from this queue for a period specified by
291 * {@code lease} and {@code unit}, having tag {@code tag}. If {@code tag} is {@code null}, tasks
292 * having the same tag as the task with earliest eta will be returned. If fewer such tasks than
293 * {@code countLimit} are available, all available such tasks in this {@link Queue} will be
294 * returned. The available tasks are those in the queue having the earliest eta such that eta is
295 * prior to the time at which the lease is requested.
296 * It is guaranteed that the leased tasks will be unavailable for lease to others in the
297 * lease period. You must call deleteTask to prevent the task from being leased again after
298 * the lease period. This method supports leasing a maximum of 1000 tasks for no more than one
300 * @param lease Number of {@code unit}s in the lease period
301 * @param unit Time unit of the lease period
302 * @param countLimit maximum number of tasks to lease
303 * @param tag User defined tag required for returned tasks. If {@code null}, the tag of the task
304 * with earliest eta will be used.
305 * @return A list of {@link TaskHandle} for each leased task.
306 * @throws InvalidQueueModeException if the target queue is not in pull mode.
307 * @throws IllegalArgumentException if lease < 0, countLimit <= 0 or either is too large.
308 * @throws InternalFailureException
309 * @throws IllegalStateException If the queue does not exist. (see queue.xml)
310 * @throws TransientFailureException Attempting the request after this exception may succeed.
312 List
<TaskHandle
> leaseTasksByTagBytes(long lease
, TimeUnit unit
, long countLimit
, byte[] tag
);
315 * Leases up to {@code countLimit} tasks from this queue for a period specified by
316 * {@code lease} and {@code unit}, having tag {@code tag}. If {@code tag} is {@code null}, tasks
317 * having the same tag as the task with earliest eta will be returned. If fewer such tasks than
318 * {@code countLimit} are available, all available such tasks in this {@link Queue} will be
319 * returned. The available tasks are those in the queue having the earliest eta such that eta is
320 * prior to the time at which the lease is requested.
321 * <p>It is guaranteed that the leased tasks will be unavailable for lease to others in the
322 * lease period. You must call deleteTask to prevent the task from being leased again after
323 * the lease period. This method supports leasing a maximum of 1000 tasks for no more than one
325 * @param lease Number of {@code unit}s in the lease period
326 * @param unit Time unit of the lease period
327 * @param countLimit maximum number of tasks to lease
328 * @param tag User defined {@code String} tag required for returned tasks. If {@code null}, the
329 * tag of the task with earliest eta will be used.
330 * @return A list of {@link TaskHandle} for each leased task.
331 * @throws InvalidQueueModeException if the target queue is not in pull mode.
332 * @throws IllegalArgumentException if lease < 0, countLimit <= 0 or either is too large.
333 * @throws InternalFailureException
334 * @throws IllegalStateException If the queue does not exist. (see queue.xml)
335 * @throws TransientFailureException Attempting the request after this exception may succeed.
337 List
<TaskHandle
> leaseTasksByTag(long lease
, TimeUnit unit
, long countLimit
, String tag
);
340 * Leases tasks from this queue, with lease period and other options specified by
341 * {@code options}. The available tasks are those in the queue having the earliest eta such that
342 * eta is prior to the time at which the lease is requested.
343 * <p> If {@code options} specifies a tag, only tasks having that tag will be returned.
344 * If {@code options} specifies no tag, but does specify {@code groupByTag}, only tasks
345 * having the same tag as the task with earliest eta will be returned.
346 * <p> It is guaranteed that the leased tasks will be unavailable for lease to others in
347 * the lease period. You must call deleteTask to prevent the task from being leased again after
348 * the lease period. This method supports leasing a maximum of 1000 tasks for no more than one
350 * @param options Specific options for this lease request
351 * @return A list of {@link TaskHandle} for each leased task.
352 * @throws InvalidQueueModeException if the target queue is not in pull mode.
353 * @throws IllegalArgumentException if lease period or countLimit is null or either is too large.
354 * @throws InternalFailureException
355 * @throws IllegalStateException If the queue does not exist. (see queue.xml)
356 * @throws TransientFailureException Attempting the request after this exception may succeed.
358 List
<TaskHandle
> leaseTasks(LeaseOptions options
);
361 * Asynchronously leases up to {@code countLimit} tasks from this queue for a period specified by
362 * {@code lease} and {@code unit}. If fewer tasks than {@code countLimit} are available, all
363 * available tasks in this {@link Queue} will be returned. The available tasks are those in the
364 * queue having the earliest eta such that eta is prior to the time at which the lease is
365 * requested. It is guaranteed that the leased tasks will be unavailable for lease to others in
366 * the lease period. You must call deleteTask to prevent the task from being leased again after
367 * the lease period. This method supports leasing a maximum of 1000 tasks for no more than one
369 * @param lease Number of {@code unit}s in the lease period
370 * @param unit Time unit of the lease period
371 * @param countLimit maximum number of tasks to lease
372 * @return A {@code Future} whose result is a list of {@link TaskHandle} for each leased task.
373 * @throws IllegalArgumentException if lease < 0, countLimit <= 0 or either is too large.
375 Future
<List
<TaskHandle
>> leaseTasksAsync(
376 long lease
, TimeUnit unit
, long countLimit
);
379 * Asynchronously leases up to {@code countLimit} tasks from this queue for a period specified by
380 * {@code lease} and {@code unit}, having tag {@code tag}. If {@code tag} is {@code null}, tasks
381 * having the same tag as the task with earliest eta will be returned. If fewer such tasks than
382 * {@code countLimit} are available, all available such tasks in this {@link Queue} will be
383 * returned. The available tasks are those in the queue having the earliest eta such that eta is
384 * prior to the time at which the lease is requested.
385 * It is guaranteed that the leased tasks will be unavailable for lease to others in the
386 * lease period. You must call deleteTask to prevent the task from being leased again after
387 * the lease period. This method supports leasing a maximum of 1000 tasks for no more than one
389 * @param lease Number of {@code unit}s in the lease period
390 * @param unit Time unit of the lease period
391 * @param countLimit maximum number of tasks to lease
392 * @param tag User defined tag required for returned tasks. If {@code null}, the tag of the task
393 * with earliest eta will be used.
394 * @return A {@code Future} whose result is a list of {@link TaskHandle} for each leased task.
395 * @throws IllegalArgumentException if lease < 0, countLimit <= 0 or either is too large.
397 Future
<List
<TaskHandle
>> leaseTasksByTagBytesAsync(
398 long lease
, TimeUnit unit
, long countLimit
, byte[] tag
);
401 * Asynchronously leases up to {@code countLimit} tasks from this queue for a period specified by
402 * {@code lease} and {@code unit}, having tag {@code tag}. If {@code tag} is {@code null}, tasks
403 * having the same tag as the task with earliest eta will be returned. If fewer such tasks than
404 * {@code countLimit} are available, all available such tasks in this {@link Queue} will be
405 * returned. The available tasks are those in the queue having the earliest eta such that eta is
406 * prior to the time at which the lease is requested.
407 * <p>It is guaranteed that the leased tasks will be unavailable for lease to others in the
408 * lease period. You must call deleteTask to prevent the task from being leased again after
409 * the lease period. This method supports leasing a maximum of 1000 tasks for no more than one
411 * @param lease Number of {@code unit}s in the lease period
412 * @param unit Time unit of the lease period
413 * @param countLimit maximum number of tasks to lease
414 * @param tag User defined {@code String} tag required for returned tasks. If {@code null}, the
415 * tag of the task with earliest eta will be used.
416 * @return A {@code Future} whose result is a list of {@link TaskHandle} for each leased task.
417 * @throws IllegalArgumentException if lease < 0, countLimit <= 0 or either is too large.
419 Future
<List
<TaskHandle
>> leaseTasksByTagAsync(
420 long lease
, TimeUnit unit
, long countLimit
, String tag
);
423 * Asynchronously leases tasks from this queue, with lease period and other options specified by
424 * {@code options}. The available tasks are those in the queue having the earliest eta such that
425 * eta is prior to the time at which the lease is requested.
426 * <p> If {@code options} specifies a tag, only tasks having that tag will be returned.
427 * If {@code options} specifies no tag, but does specify {@code groupByTag}, only tasks
428 * having the same tag as the task with earliest eta will be returned.
429 * <p> It is guaranteed that the leased tasks will be unavailable for lease to others in
430 * the lease period. You must call deleteTask to prevent the task from being leased again after
431 * the lease period. This method supports leasing a maximum of 1000 tasks for no more than one
433 * @param options Specific options for this lease request
434 * @return A {@code Future} whose result is a list of {@link TaskHandle} for each leased task.
435 * @throws IllegalArgumentException if lease period or countLimit is null or either is too large.
437 Future
<List
<TaskHandle
>> leaseTasksAsync(LeaseOptions options
);
440 * Clears all the tasks in this {@link Queue}. This function returns
441 * immediately. Some delay may apply on the server before the Queue is
442 * actually purged. Tasks being executed at the time the purge call is
443 * made will continue executing, other tasks in this Queue will continue
444 * being dispatched and executed before the purge call takes effect.
445 * @throws IllegalStateException If the Queue does not exist. (see queue.xml)
446 * @throws TransientFailureException Attempting the request after this exception may succeed.
447 * @throws InternalFailureException
452 * Modify the lease of the specified task in this {@link Queue} for a period of time specified
453 * by {@code lease} and {@code unit}. A lease time of 0 will relinquish the lease on the
454 * task and make it avaible to be leased by calling {@link #leaseTasks(LeaseOptions) leaseTasks}.
455 * @param taskHandle handle of the task that is having its lease modified.
456 * @param lease Number of {@code unit}s in the lease period.
457 * @param unit Time unit of the lease period.
458 * @return Updated {@link TaskHandle} with the new lease period.
459 * @throws InvalidQueueModeException if the target queue is not in pull mode.
460 * @throws IllegalArgumentException if lease < 0 or too large.
461 * @throws InternalFailureException
462 * @throws IllegalStateException If the queue does not exist, or the task lease has expired
463 * or the queue has been paused.
464 * @throws TransientFailureException Attempting the request after this exception may succeed.
466 TaskHandle
modifyTaskLease(TaskHandle taskHandle
, long lease
, TimeUnit unit
);
469 * Obtain statistics for this {@link Queue}.
470 * @return The current {@link QueueStatistics} for this queue.
471 * @throws IllegalStateException If the Queue does not exist. (see queue.xml)
472 * @throws TransientFailureException Attempting the request after this exception may succeed.
473 * @throws InternalFailureException
475 QueueStatistics
fetchStatistics();
478 * Asynchronously obtains statistics for this {@link Queue}.
479 * @param deadlineInSeconds the maximum duration, in seconds, that the fetch statistics
480 * request can run. A default deadline will be used if {@code null} is supplied.
481 * @throws IllegalArgumentException if deadlineInSeconds <= 0.
482 * @return A {@code Future} with a result type of {@link QueueStatistics}.
484 Future
<QueueStatistics
> fetchStatisticsAsync( Double deadlineInSeconds
);