App Engine Java SDK version 1.9.14
[gae.git] / java / src / main / com / google / appengine / api / taskqueue / Queue.java
blobd51e65fd422d54578fe0e3043118212e6b055b01
1 // Copyright 2010 Google Inc. All rights reserved.
2 package com.google.appengine.api.taskqueue;
4 import com.google.appengine.api.datastore.Transaction;
5 import java.util.List;
6 import java.util.concurrent.Future;
7 import java.util.concurrent.TimeUnit;
9 /**
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
44 * lease.
47 public interface Queue {
48 /**
49 * The default queue name.
51 String DEFAULT_QUEUE = "default";
53 /**
54 * The default queue path.
56 String DEFAULT_QUEUE_PATH = "/_ah/queue";
58 /**
59 * Returns the queue name.
61 String getQueueName();
63 /**
64 * Submits a task to this queue with an auto generated name with default
65 * options.
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.
76 TaskHandle add();
78 /**
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);
92 /**
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
95 * the queue.
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 any of the provided {@code TaskOptions} contained a name
101 * of a task that was previously created, and if no other {@code Exception} would be thrown. Note
102 * that if a {@code TaskAlreadyExistsException} is caught, the caller can be guaranteed that for
103 * each one of the provided {@code TaskOptions}, either the corresponding task was successfully
104 * added, or a task with the given name was successfully added in the past.
105 * @throws TransientFailureException Attempting the request after this exception may succeed.
106 * @throws UnsupportedTranslationException If chosen character encoding is unsupported.
107 * @throws InvalidQueueModeException task method is {@link TaskOptions.Method#PULL} and queue is
108 * push queue or vice versa.
110 List<TaskHandle> add(Iterable<TaskOptions> taskOptions);
113 * Submits a task to this queue in the provided Transaction.
114 * <p>A task is added if and only if the transaction is applied successfully.
115 * @param txn an enclosing {@link Transaction} or null, if not null a task cannot be named.
116 * @param taskOptions The definition of the task.
117 * @return A {@link TaskHandle}.
118 * @throws InternalFailureException
119 * @throws IllegalStateException If the queue does not exist. (see queue.xml)
120 * @throws TaskAlreadyExistsException if a task with the same name was previously created.
121 * @throws TransientFailureException Attempting the request after this exception may succeed.
122 * @throws UnsupportedTranslationException If chosen character encoding is unsupported.
123 * @throws InvalidQueueModeException task method is {@link TaskOptions.Method#PULL} and queue is
124 * push queue or vice versa.
126 TaskHandle add(Transaction txn, TaskOptions taskOptions);
129 * Submits tasks to this queue in the provided Transaction.
130 * <p>The tasks are added if and only if the transaction is applied successfully.
131 * @param txn an enclosing {@link Transaction} or null, if not null a task cannot be named.
132 * @param taskOptions An iterable over task definitions.
133 * @return A list containing a {@link TaskHandle} for each added task.
134 * @throws InternalFailureException
135 * @throws IllegalStateException If the queue does not exist. (see queue.xml)
136 * @throws TaskAlreadyExistsException if a task with the same name was previously created.
137 * @throws TransientFailureException Attempting the request after this exception may succeed.
138 * @throws UnsupportedTranslationException If chosen character encoding is unsupported.
139 * @throws InvalidQueueModeException task method is {@link TaskOptions.Method#PULL} and queue is
140 * push queue or vice versa.
142 List<TaskHandle> add(Transaction txn, Iterable<TaskOptions> taskOptions);
145 * Asynchronously submits a task to this queue with an auto generated name with default
146 * options.
147 * <p>This method is similar to calling {@link #addAsync(TaskOptions)} with
148 * a {@link TaskOptions} object returned by
149 * {@link TaskOptions.Builder#withDefaults()}.
150 * @return A {@code Future} with a result type of {@link TaskHandle}.
152 Future<TaskHandle> addAsync();
155 * Asynchronously submits a task to this queue.
156 * @param taskOptions The definition of the task.
157 * @return A {@code Future} with a result type of {@link TaskHandle}.
158 * @throws UnsupportedTranslationException If chosen character encoding is unsupported.
160 Future<TaskHandle> addAsync(TaskOptions taskOptions);
163 * Asynchronously submits tasks to this queue.
164 * <p>Submission is not atomic i.e. if this method fails then some tasks may have been added to
165 * the queue.
166 * @param taskOptions An iterable over task definitions.
167 * @return A {@code Future} whose result is a list containing a {@link TaskHandle} for each added
168 * task.
169 * @throws UnsupportedTranslationException If chosen character encoding is unsupported.
171 Future<List<TaskHandle>> addAsync(Iterable<TaskOptions> taskOptions);
174 * Asynchronously submits a task to this queue in the provided Transaction.
175 * <p>A task is added if and only if the transaction is applied successfully.
176 * @param txn an enclosing {@link Transaction} or null, if not null a task cannot be named.
177 * @param taskOptions The definition of the task.
178 * @return A {@code Future} with a result type of {@link TaskHandle}.
179 * @throws UnsupportedTranslationException If chosen character encoding is unsupported.
181 Future<TaskHandle> addAsync(Transaction txn, TaskOptions taskOptions);
184 * Asynchronously submits tasks to this queue in the provided Transaction.
185 * <p>The tasks are added if and only if the transaction is applied successfully.
186 * @param txn an enclosing {@link Transaction} or null, if not null a task cannot be named.
187 * @param taskOptions An iterable over task definitions.
188 * @return A {@code Future} whose result is a list containing a {@link TaskHandle} for each added
189 * task.
190 * @throws UnsupportedTranslationException If chosen character encoding is unsupported.
192 Future<List<TaskHandle>> addAsync(
193 Transaction txn, Iterable<TaskOptions> taskOptions);
196 * Deletes a task from this {@link Queue}. Task is identified by taskName.
197 * @param taskName name of the task to delete.
198 * @return True if the task was sucessfully deleted. False if the task was not found or was
199 * previously deleted.
200 * @throws IllegalArgumentException if the provided name is null, empty or doesn't match the
201 * expected pattern.
202 * @throws InternalFailureException
203 * @throws IllegalStateException If the queue does not exist. (see queue.xml)
204 * @throws TransientFailureException Attempting the request after this exception may succeed.
206 boolean deleteTask(String taskName);
209 * Deletes a task from this {@link Queue}. Task is identified by a TaskHandle.
210 * @param taskHandle handle of the task to delete.
211 * @return True if the task was sucessfully deleted. False if the task was not found or was
212 * previously deleted.
213 * @throws IllegalArgumentException if the provided name is null, empty or doesn't match the
214 * expected pattern.
215 * @throws InternalFailureException
216 * @throws IllegalStateException If the queue does not exist. (see queue.xml)
217 * @throws QueueNameMismatchException if the task handle refers to a different named queue.
218 * @throws TransientFailureException Attempting the request after this exception may succeed.
220 boolean deleteTask(TaskHandle taskHandle);
223 * Deletes a list of tasks from this {@link Queue}. The tasks are identified
224 * by a list of TaskHandles.
225 * @param taskHandles list of handles of tasks to delete.
226 * @return List<Booloean> that represents the result of deleting each task in
227 * the same order as the input handles. True if a task was sucessfully deleted.
228 * False if the task was not found or was previously deleted.
229 * @throws IllegalArgumentException if the provided name is null, empty or doesn't match the
230 * expected pattern.
231 * @throws InternalFailureException
232 * @throws IllegalStateException If the queue does not exist. (see queue.xml)
233 * @throws QueueNameMismatchException if the task handle refers to a different named queue.
234 * @throws TransientFailureException Attempting the request after this exception may succeed.
236 List<Boolean> deleteTask(List<TaskHandle> taskHandles);
239 * Asynchronously deletes a task from this {@link Queue}. Task is identified by taskName.
240 * @param taskName name of the task to delete.
241 * @return A {@code Future} whose result is True if the task was sucessfully deleted, False if the
242 * task was not found or was previously deleted.
243 * @throws IllegalArgumentException if the provided name is null, empty or doesn't match the
244 * expected pattern.
246 Future<Boolean> deleteTaskAsync(String taskName);
249 * Asynchronously deletes a task from this {@link Queue}. Task is identified by a TaskHandle.
250 * @param taskHandle handle of the task to delete.
251 * @return A {@code Future} whose result is True if the task was sucessfully deleted, False if the
252 * task was not found or was previously deleted.
253 * @throws IllegalArgumentException if the provided name is null, empty or doesn't match the
254 * expected pattern.
255 * @throws QueueNameMismatchException if the task handle refers to a different named queue.
257 Future<Boolean> deleteTaskAsync(TaskHandle taskHandle);
260 * Asynchronously deletes a list of tasks from this {@link Queue}. The tasks are identified
261 * by a list of TaskHandles.
262 * @param taskHandles list of handles of tasks to delete.
263 * @return A {@code Future} whose result is a List<Booloean> that represents the result of
264 * deleting each task in the same order as the input handles. True if a task was
265 * sucessfully deleted. False if the task was not found or was previously deleted.
266 * @throws IllegalArgumentException if the provided name is null, empty or doesn't match the
267 * expected pattern.
268 * @throws QueueNameMismatchException if the task handle refers to a different named queue.
270 Future<List<Boolean>> deleteTaskAsync(List<TaskHandle> taskHandles);
273 * Leases up to {@code countLimit} tasks from this queue for a period specified by
274 * {@code lease} and {@code unit}. If fewer tasks than {@code countLimit} are available, all
275 * available tasks in this {@link Queue} will be returned. The available tasks are those in the
276 * queue having the earliest eta such that eta is prior to the time at which the lease is
277 * requested. It is guaranteed that the leased tasks will be unavailable for lease to others in
278 * the lease period. You must call deleteTask to prevent the task from being leased again after
279 * the lease period. This method supports leasing a maximum of 1000 tasks for no more than one
280 * week. If you generate more than 10 LeaseTasks requests per second, only the first 10 requests
281 * will return results. The others will return no results.
282 * @param lease Number of {@code unit}s in the lease period
283 * @param unit Time unit of the lease period
284 * @param countLimit maximum number of tasks to lease
285 * @return A list of {@link TaskHandle} for each leased task.
286 * @throws InvalidQueueModeException if the target queue is not in pull mode.
287 * @throws IllegalArgumentException if lease < 0, countLimit <= 0 or either is too large.
288 * @throws InternalFailureException
289 * @throws IllegalStateException If the queue does not exist. (see queue.xml)
290 * @throws TransientFailureException Attempting the request after this exception may succeed.
292 List<TaskHandle> leaseTasks(long lease, TimeUnit unit, long countLimit);
295 * Leases up to {@code countLimit} tasks from this queue for a period specified by
296 * {@code lease} and {@code unit}, having tag {@code tag}. If {@code tag} is {@code null}, tasks
297 * having the same tag as the task with earliest eta will be returned. If fewer such tasks than
298 * {@code countLimit} are available, all available such tasks in this {@link Queue} will be
299 * returned. The available tasks are those in the queue having the earliest eta such that eta is
300 * prior to the time at which the lease is requested.
301 * It is guaranteed that the leased tasks will be unavailable for lease to others in the
302 * lease period. You must call deleteTask to prevent the task from being leased again after
303 * the lease period. This method supports leasing a maximum of 1000 tasks for no more than one
304 * week.
305 * @param lease Number of {@code unit}s in the lease period
306 * @param unit Time unit of the lease period
307 * @param countLimit maximum number of tasks to lease
308 * @param tag User defined tag required for returned tasks. If {@code null}, the tag of the task
309 * with earliest eta will be used.
310 * @return A list of {@link TaskHandle} for each leased task.
311 * @throws InvalidQueueModeException if the target queue is not in pull mode.
312 * @throws IllegalArgumentException if lease < 0, countLimit <= 0 or either is too large.
313 * @throws InternalFailureException
314 * @throws IllegalStateException If the queue does not exist. (see queue.xml)
315 * @throws TransientFailureException Attempting the request after this exception may succeed.
317 List<TaskHandle> leaseTasksByTagBytes(long lease, TimeUnit unit, long countLimit, byte[] tag);
320 * Leases up to {@code countLimit} tasks from this queue for a period specified by
321 * {@code lease} and {@code unit}, having tag {@code tag}. If {@code tag} is {@code null}, tasks
322 * having the same tag as the task with earliest eta will be returned. If fewer such tasks than
323 * {@code countLimit} are available, all available such tasks in this {@link Queue} will be
324 * returned. The available tasks are those in the queue having the earliest eta such that eta is
325 * prior to the time at which the lease is requested.
326 * <p>It is guaranteed that the leased tasks will be unavailable for lease to others in the
327 * lease period. You must call deleteTask to prevent the task from being leased again after
328 * the lease period. This method supports leasing a maximum of 1000 tasks for no more than one
329 * week.
330 * @param lease Number of {@code unit}s in the lease period
331 * @param unit Time unit of the lease period
332 * @param countLimit maximum number of tasks to lease
333 * @param tag User defined {@code String} tag required for returned tasks. If {@code null}, the
334 * tag of the task with earliest eta will be used.
335 * @return A list of {@link TaskHandle} for each leased task.
336 * @throws InvalidQueueModeException if the target queue is not in pull mode.
337 * @throws IllegalArgumentException if lease < 0, countLimit <= 0 or either is too large.
338 * @throws InternalFailureException
339 * @throws IllegalStateException If the queue does not exist. (see queue.xml)
340 * @throws TransientFailureException Attempting the request after this exception may succeed.
342 List<TaskHandle> leaseTasksByTag(long lease, TimeUnit unit, long countLimit, String tag);
345 * Leases tasks from this queue, with lease period and other options specified by
346 * {@code options}. The available tasks are those in the queue having the earliest eta such that
347 * eta is prior to the time at which the lease is requested.
348 * <p> If {@code options} specifies a tag, only tasks having that tag will be returned.
349 * If {@code options} specifies no tag, but does specify {@code groupByTag}, only tasks
350 * having the same tag as the task with earliest eta will be returned.
351 * <p> It is guaranteed that the leased tasks will be unavailable for lease to others in
352 * the lease period. You must call deleteTask to prevent the task from being leased again after
353 * the lease period. This method supports leasing a maximum of 1000 tasks for no more than one
354 * week.
355 * @param options Specific options for this lease request
356 * @return A list of {@link TaskHandle} for each leased task.
357 * @throws InvalidQueueModeException if the target queue is not in pull mode.
358 * @throws IllegalArgumentException if lease period or countLimit is null or either is too large.
359 * @throws InternalFailureException
360 * @throws IllegalStateException If the queue does not exist. (see queue.xml)
361 * @throws TransientFailureException Attempting the request after this exception may succeed.
363 List<TaskHandle> leaseTasks(LeaseOptions options);
366 * Asynchronously leases up to {@code countLimit} tasks from this queue for a period specified by
367 * {@code lease} and {@code unit}. If fewer tasks than {@code countLimit} are available, all
368 * available tasks in this {@link Queue} will be returned. The available tasks are those in the
369 * queue having the earliest eta such that eta is prior to the time at which the lease is
370 * requested. It is guaranteed that the leased tasks will be unavailable for lease to others in
371 * the lease period. You must call deleteTask to prevent the task from being leased again after
372 * the lease period. This method supports leasing a maximum of 1000 tasks for no more than one
373 * week.
374 * @param lease Number of {@code unit}s in the lease period
375 * @param unit Time unit of the lease period
376 * @param countLimit maximum number of tasks to lease
377 * @return A {@code Future} whose result is a list of {@link TaskHandle} for each leased task.
378 * @throws IllegalArgumentException if lease < 0, countLimit <= 0 or either is too large.
380 Future<List<TaskHandle>> leaseTasksAsync(
381 long lease, TimeUnit unit, long countLimit);
384 * Asynchronously leases up to {@code countLimit} tasks from this queue for a period specified by
385 * {@code lease} and {@code unit}, having tag {@code tag}. If {@code tag} is {@code null}, tasks
386 * having the same tag as the task with earliest eta will be returned. If fewer such tasks than
387 * {@code countLimit} are available, all available such tasks in this {@link Queue} will be
388 * returned. The available tasks are those in the queue having the earliest eta such that eta is
389 * prior to the time at which the lease is requested.
390 * It is guaranteed that the leased tasks will be unavailable for lease to others in the
391 * lease period. You must call deleteTask to prevent the task from being leased again after
392 * the lease period. This method supports leasing a maximum of 1000 tasks for no more than one
393 * week.
394 * @param lease Number of {@code unit}s in the lease period
395 * @param unit Time unit of the lease period
396 * @param countLimit maximum number of tasks to lease
397 * @param tag User defined tag required for returned tasks. If {@code null}, the tag of the task
398 * with earliest eta will be used.
399 * @return A {@code Future} whose result is a list of {@link TaskHandle} for each leased task.
400 * @throws IllegalArgumentException if lease < 0, countLimit <= 0 or either is too large.
402 Future<List<TaskHandle>> leaseTasksByTagBytesAsync(
403 long lease, TimeUnit unit, long countLimit, byte[] tag);
406 * Asynchronously leases up to {@code countLimit} tasks from this queue for a period specified by
407 * {@code lease} and {@code unit}, having tag {@code tag}. If {@code tag} is {@code null}, tasks
408 * having the same tag as the task with earliest eta will be returned. If fewer such tasks than
409 * {@code countLimit} are available, all available such tasks in this {@link Queue} will be
410 * returned. The available tasks are those in the queue having the earliest eta such that eta is
411 * prior to the time at which the lease is requested.
412 * <p>It is guaranteed that the leased tasks will be unavailable for lease to others in the
413 * lease period. You must call deleteTask to prevent the task from being leased again after
414 * the lease period. This method supports leasing a maximum of 1000 tasks for no more than one
415 * week.
416 * @param lease Number of {@code unit}s in the lease period
417 * @param unit Time unit of the lease period
418 * @param countLimit maximum number of tasks to lease
419 * @param tag User defined {@code String} tag required for returned tasks. If {@code null}, the
420 * tag of the task with earliest eta will be used.
421 * @return A {@code Future} whose result is a list of {@link TaskHandle} for each leased task.
422 * @throws IllegalArgumentException if lease < 0, countLimit <= 0 or either is too large.
424 Future<List<TaskHandle>> leaseTasksByTagAsync(
425 long lease, TimeUnit unit, long countLimit, String tag);
428 * Asynchronously leases tasks from this queue, with lease period and other options specified by
429 * {@code options}. The available tasks are those in the queue having the earliest eta such that
430 * eta is prior to the time at which the lease is requested.
431 * <p> If {@code options} specifies a tag, only tasks having that tag will be returned.
432 * If {@code options} specifies no tag, but does specify {@code groupByTag}, only tasks
433 * having the same tag as the task with earliest eta will be returned.
434 * <p> It is guaranteed that the leased tasks will be unavailable for lease to others in
435 * the lease period. You must call deleteTask to prevent the task from being leased again after
436 * the lease period. This method supports leasing a maximum of 1000 tasks for no more than one
437 * week.
438 * @param options Specific options for this lease request
439 * @return A {@code Future} whose result is a list of {@link TaskHandle} for each leased task.
440 * @throws IllegalArgumentException if lease period or countLimit is null or either is too large.
442 Future<List<TaskHandle>> leaseTasksAsync(LeaseOptions options);
445 * Clears all the tasks in this {@link Queue}. This function returns
446 * immediately. Some delay may apply on the server before the Queue is
447 * actually purged. Tasks being executed at the time the purge call is
448 * made will continue executing, other tasks in this Queue will continue
449 * being dispatched and executed before the purge call takes effect.
450 * @throws IllegalStateException If the Queue does not exist. (see queue.xml)
451 * @throws TransientFailureException Attempting the request after this exception may succeed.
452 * @throws InternalFailureException
454 void purge();
457 * Modify the lease of the specified task in this {@link Queue} for a period of time specified
458 * by {@code lease} and {@code unit}. A lease time of 0 will relinquish the lease on the
459 * task and make it avaible to be leased by calling {@link #leaseTasks(LeaseOptions) leaseTasks}.
460 * @param taskHandle handle of the task that is having its lease modified.
461 * @param lease Number of {@code unit}s in the lease period.
462 * @param unit Time unit of the lease period.
463 * @return Updated {@link TaskHandle} with the new lease period.
464 * @throws InvalidQueueModeException if the target queue is not in pull mode.
465 * @throws IllegalArgumentException if lease < 0 or too large.
466 * @throws InternalFailureException
467 * @throws IllegalStateException If the queue does not exist, or the task lease has expired
468 * or the queue has been paused.
469 * @throws TransientFailureException Attempting the request after this exception may succeed.
471 TaskHandle modifyTaskLease(TaskHandle taskHandle, long lease, TimeUnit unit);
474 * Obtain statistics for this {@link Queue}.
475 * @return The current {@link QueueStatistics} for this queue.
476 * @throws IllegalStateException If the Queue does not exist. (see queue.xml)
477 * @throws TransientFailureException Attempting the request after this exception may succeed.
478 * @throws InternalFailureException
480 QueueStatistics fetchStatistics();
483 * Asynchronously obtains statistics for this {@link Queue}.
484 * @param deadlineInSeconds the maximum duration, in seconds, that the fetch statistics
485 * request can run. A default deadline will be used if {@code null} is supplied.
486 * @throws IllegalArgumentException if deadlineInSeconds <= 0.
487 * @return A {@code Future} with a result type of {@link QueueStatistics}.
489 Future<QueueStatistics> fetchStatisticsAsync( Double deadlineInSeconds);