Architecture of the application: request processin

2019-02-28 23:47发布

问题:

This question already has an answer here:

  • Background timer task in JSP/Servlet web application [duplicate] 1 answer

I have the following problem:

I am writing a web application with servlets and JSP which should query a separate service. But the query takes a lot of time - around 30 seconds.

The structure of my app is simple - Filter->Controller->jsp. I was thinking what if in the filter I send the request into a separate thread to be processed while the controller goes on executing its job. What shall I do in order to make the querying process and controller processing simultaneous? + I need the query to be renewed every 5 mins (so that the user does have the relevant info). What is the optimum way to do that?

回答1:

Use a ServletContextListener to start a thread when the web application starts.

The thread should write the data into the ServletContext (e.g. via setAttribute()).

The ServletContextListener should terminate the thread when the application is stopped.

The Servlet should read the data from the ServletContext as it needs it.