Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4339216
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:11:12+00:00 2026-05-21T11:11:12+00:00

I have a servlet that handles a particular incoming request. We’ll call it `UpdateUserStats’.

  • 0

I have a servlet that handles a particular incoming request. We’ll call it `UpdateUserStats’. I want the call to be fast, but I also need the request to do a reasonably expensive task. I thought that it might be a nice solution if I let the UpdateUserStats servlet do all of the work that determines success or failure, persist the new state, and then enqueue some work for a background thread to take care of and return success immediately.

This seems nice, but I’m wondering how to handle shutdown. When the server shuts down, I want to make sure that it doesn’t kill the background thread until no more work remains. I was thinking of using a tomcat ServletContextListener to let the background thread know that the server is going down and it is alright to quit out once the queue ends, but I’m not 100% sure of how this will behave. Will contextDestroyed() only after all requests are done processing? The interface seems to imply this, but I didn’t find it in the documentation. Also, I wonder how long Tomcat is willing to wait around before getting angry that a background thread isn’t terminating.

Alternately, is there a better way to handle this problem? It’s a Spring app if perhaps Spring has some magic post-request background work functionality.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T11:11:13+00:00Added an answer on May 21, 2026 at 11:11 am

    Based on servlet spec documentation

    “On application shutdown, listeners are notified in reverse order to their declarations
    with notifications to session listeners preceeding notifications to context listeners.
    Session listeners must be notified of session invalidations prior to context listeners
    being notified of application shutdown.”

    So this should be the first listener which handles notifying the thread to complete its job.

    I have used tomcat and as far a I have seen, it would not wait for more than 10-15 seconds before giving up doing a clean shutdown if the listener does not finish by then.

    Typically all containers like Spring use listener for a clean exit during shutdown

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a servlet filter that handles errors for both vanilla servlets and JSF
On Java side, we have a servlet filter that handles authentication. We don't have
I have a servlet which handles http get requests that I'd like to be
I have a servlet that is used for many different actions, used in the
I have a servlet that I would like to run within ColdFusion MX 7.
I have a file upload form that is being posted back to a servlet
I have a servlet S which handles callbacks from a 3rd party site. The
I have many years of experience in Java including Swing, Servlet and JDBC, but
I have a servlet which handles a multipart form post. The post is actually
I have a servlet class which handles login. At the end of my login

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.