简体   繁体   中英

Background task or Async Task in user session Spring MVC

How can I implement a background task in user's session, to check if the user is making any progress or not? When the user takes too long, then the reserved resources can be released.

The requirement is when user selects the reservation, the resources are reserved for this user and the server is now waiting for next request to proceed.

If the user doesn't click proceed in next 60 seconds, the server should automatically release the reserved resources.

You could using java.util.Timer, java.util.TimerTask to schedule and execute a cancel. Another popular scheduling library is quartz . But this could be an overkill.

A different approach to addressing this could be create a client side timer task which gets invokes a server 'cancel' function when no user interaction for certain period. Even then you would have to have a server process to cleanup resources when the user orphans the session by closing the browser for instance.

Also you could consider using session timeout .

Ok So I figured out how to put the solution in place.

Here are the steps that I followed

  1. When initial request is received and is ok before sending the response create an entry in a different table with “Entry Time” and “TransactionID” ( Transaction Id is created when the resources are reserved and will be used in all subsequent request").
  2. Delete this record when “proceedWithReservaration” is received .
  3. Create a scheduler to check this table to find out the “TransactionID” older than 60 seconds .
  4. Create a Async task to send the "releaseNotification" request so that we can process “Release” in parallel.

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