简体   繁体   中英

Send response after task from queue finishes to HttpSession (in GAE)

I'm implementing a basic Task Queue in the Google App Engine. Nothing fancy as I'm learning the introductions. The (push) queue works fine, but I'd like to send a little confirmation (or fail) message to the user of the relevant http session when the task is finished.

The way my structure is setup:

  1. a HttpServlet receives an incoming HttpServletRequest

  2. some info is retrieved from the HttpSession and is used to produce a task + store it in the task queue

  3. another HttpServlet (the worker) receives an incoming request from the queue to execute the task

  4. task gets executed

  5. (this is the step I didn't manage to implement:) Send some info to a .jsp for the HttpSession that inserted the task in the queue

Normally I would do this by retrieving the session from the HttpServletRequest , but in this case, the one initiating the request is the queue itself (and not the user who initiated the task). I can't pass the HttpSession as an array of bytes to the parameter since I need to keep a reference to the same session.

I was able to pass the id from the session as a parameter to my worker, but I couldn't figure out how to find the reference back to the session through this id.

It's possible something could be done with FutureValue here: https://github.com/GoogleCloudPlatform/appengine-pipelines/tree/master/java/src/main/java/com/google/appengine/tools/pipeline but I'm completely lost on how it's used.

Saving http sessions per user in a HashMap / datastore seems like bad practice since the user might not want to keep the data.

So, any ideas on how to send an asynchronous message back to the user that initiated the task upon task completion/failure?

We implemented our own session system on top of appengine but I believe what we did would work the same for you. Save the start of the job in an entity in the datastore linked up to the session id of the user. Make sure to timestamp it and then use polling on your client side to check if the job has been finished. Update the job's status when it is complete and then purge the entity when you next poll for the status update. To clean up the data that sticks around if the user closes the session just use a appengine cron job to go through and clean up the old entities after a set time.

This has worked well for our implementation and it is a bit more work but that's what you are dealing with when your tasks are async.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM