Issue
In a Spring MVC app, if a page (browser window ) is closed while a functionality was running, will it stop the application and the processing that was happening or it will continue to happen at the server.
Suppose I have a functionality to export an excel report , I click the Export button. Now there are 2 cases : Case 1 - If I initiate report generation without using thread , and close the window before report generation, would it throw the error/or abort the report generation OR would the process continue to happen on server and report will be downloaded?
Case 2) If I would have initiated report generation using a thread , and closed the window before report generation , would it throw the error /or abort the report generation OR would the thread continue to work in background and report will be downloaded?
Solution
The short answer is no, closing the browser tab or window will not affect the processing that is happening on the server (unless this processing also involves I/O with the client).
In general, Servlet containers (e.g. Tomcat) that host Spring MVC applications have a built-in thread pool to service the http requests. Once a request arrives, one of these threads will process it. If in the meantime, the TCP connection is closed by the client (which is what hopefully1 happens when closing a browser tab or window), the thread will not be notified or interrupted in any way. Therefore, any processing that might be happening on the server side will continue. I doesn't matter if you use another Thread to do the processing or not. The processing on the server will continue in both cases.
Since you are specifically asking about "downloding" a report file, here is some more info: Downloading involves I/O with the client. If you close the tab or browser before the download begins, the server will get an error as soon as it tries to write the data to the response. So the file will not be downloaded, data will not be transfered. If you close the tab after the download begins, the file will be downloaded normally. If the user cancels the file download, then again the server will get an error and data transfer will stop.
In any case, if the generation of the report happens before sending data back to the client, the generation will continue to work in the background, regardless of whether you use a Thread or not.
1 I say hopefully because the exact behavior depends on the OS / Device / browser. Closing the TCP connection would be the expected behavior.
Answered By - Nazaret K.
Answer Checked By - Mary Flores (JavaFixing Volunteer)