1 // Copyright 2011 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.taskqueue
;
5 import com
.google
.apphosting
.api
.ApiProxy
;
9 import javax
.servlet
.http
.HttpServlet
;
10 import javax
.servlet
.http
.HttpServletRequest
;
11 import javax
.servlet
.http
.HttpServletResponse
;
14 * Resources for managing {@link DeferredTask}.
17 public class DeferredTaskContext
{
19 * The content type of a serialized {@link DeferredTask}.
21 public static final String RUNNABLE_TASK_CONTENT_TYPE
=
22 "application/x-binary-app-engine-java-runnable-task";
25 * The URL the DeferredTask servlet is mapped to by default.
27 public static final String DEFAULT_DEFERRED_URL
= "/_ah/queue/__deferred__";
29 static final String DEFERRED_TASK_SERVLET_KEY
=
30 DeferredTaskContext
.class.getName() + ".httpServlet";
31 static final String DEFERRED_TASK_REQUEST_KEY
=
32 DeferredTaskContext
.class.getName() + ".httpServletRequest";
33 static final String DEFERRED_TASK_RESPONSE_KEY
=
34 DeferredTaskContext
.class.getName() + ".httpServletResponse";
35 static final String DEFERRED_DO_NOT_RETRY_KEY
=
36 DeferredTaskContext
.class.getName() + ".doNotRetry";
37 static final String DEFERRED_MARK_RETRY_KEY
=
38 DeferredTaskContext
.class.getName() + ".markRetry";
41 * Returns the {@link HttpServlet} instance for the current running
42 * deferred task for the current thread or {@code null} if there is
43 * no current deferred task active for this thread.
45 public static HttpServlet
getCurrentServlet() {
46 Map
<String
, Object
> attributes
= ApiProxy
.getCurrentEnvironment().getAttributes();
47 return (HttpServlet
) attributes
.get(DEFERRED_TASK_SERVLET_KEY
);
51 * Returns the {@link HttpServletRequest} instance for the current running
52 * deferred task for the current thread or {@code null} if there is
53 * no current deferred task active for this thread.
55 public static HttpServletRequest
getCurrentRequest() {
56 Map
<String
, Object
> attributes
= ApiProxy
.getCurrentEnvironment().getAttributes();
57 return (HttpServletRequest
) attributes
.get(DEFERRED_TASK_REQUEST_KEY
);
61 * Returns the {@link HttpServletResponse} instance for the current running
62 * deferred task for the current thread or {@code null} if there is
63 * no current deferred task active for this thread.
65 public static HttpServletResponse
getCurrentResponse() {
66 Map
<String
, Object
> attributes
= ApiProxy
.getCurrentEnvironment().getAttributes();
67 return (HttpServletResponse
) attributes
.get(DEFERRED_TASK_RESPONSE_KEY
);
71 * Sets the action on task failure. Normally when an exception is thrown,
72 * the task will be retried, however if {@code setDoNotRetry}
73 * is set to {@code true}, the task will not be retried.
75 public static void setDoNotRetry(boolean value
) {
76 Map
<String
, Object
> attributes
= ApiProxy
.getCurrentEnvironment().getAttributes();
77 attributes
.put(DEFERRED_DO_NOT_RETRY_KEY
, value
);
81 * Request a retry of this task, even if an exception was not thrown.
82 * If an exception was thrown and {@link #setDoNotRetry} is set to {@code true}
83 * the request will not be retried.
85 public static void markForRetry() {
86 Map
<String
, Object
> attributes
= ApiProxy
.getCurrentEnvironment().getAttributes();
87 attributes
.put(DEFERRED_MARK_RETRY_KEY
, true);
90 private DeferredTaskContext() {}