How could I reliably detect, deep in my code, whether the current invocation of the handler is being called from the Task Queue or not?
I understand that, in GAE/J, if I checked the HttpServletRequest object, I could check whether the following headers are set:
X-AppEngine-QueueNameX-AppEngine-TaskNameX-AppEngine-TaskRetryCountX-AppEngine-FailFastX-AppEngine-TaskETA
Where the existence of any of those headers would indicate that the handler is being invoked by a task queue.
But say that the part of my code that need to do the detection is deep within several abstraction layers, where I could not access the HttpServletRequest object, is there any way where I could reliably detect if the current execution environment is being invoked from a task queue or not?
What I am hoping is that there could be something easily accessible like:
SystemProperty.environment.value() == Value.TaskQueue
analogous to the way we could check whether the code is being executed at GAE or at the dev server by using SystemProperty.environment.value() == Value.Development.
I ended up checking the header of the request whether it contains
X-AppEngine-TaskNameor not, and, thanks to AlexR’s suggestion, store the check result in a thread local variable.Based on Eric Willigers comment below, turns out Google already discards
X-AppEngine-TaskNameheader from external requests, protecting the app from malicious attackers that tried to fake the header, so this seems to be the safest approach so far.