Closed seinecle closed 5 months ago
The statement "the single thread model of JSF" makes no sense. It only shows a misunderstanding of how HTTP and web apps normally work.
The solution you're looking for is here: https://stackoverflow.com/q/3787514. More background info is here: https://stackoverflow.com/q/2803160.
Thanks!
[tl;dr] https://twitter.com/seinecle/status/1748615007363829827
Background: how is this part of the app architectured
This (free, open source) app helps users analyze their data with text mining and network analysis. Different operations are possible, they are called "functions".
Functions typically take 1 second to a few minutes to run. The function called "Cowo" is the one where I experiment new ways to make the wait and the experience as good to the user as I can, while the function is running. When I'll get it right, I'll model the other functions in the same way.
The architecture is quite exotic, but I stand by it. Here it is:
the user uploads one or several files containing the text they want to analyze. The text content is persisted on disk as a single file, the name of the file is a "data persistence id" which is unique to this dataset.
the user then navigates to the cowo.html page, where they can set a few options to fine tune how the function will run. On this page, they click on a
CommandButton
"compute" which executes therunAnalysis()
method in CowoBean, which is a Session Scoped Bean hosting the fields and methods related to this function.the method
runAnalysis()
launches two methods in succession, each designed to return quickly, so therunAnalysis()
method returns almost immediately:sendCallToCowoFunction()
sends a GET request to the API endpoint (locally hosted) which runs the actual function Cowo. The API endpoint executes the function asynchronously, so the GET request returns almost immediately.The results of the function are stored on disk with a file.
getTopNodes()
is used to extract a sample of the results returned by Cowo. The method creates a new thread and executes all its code in this new thread, so the method returns immediately.Communication and orchestration between the front-end and the different parts of the backend
How are all these different steps orchestrated, and how is the user alerted that the end results have arrived?
sendCallToCowoFunction()
sends back (also with http calls) intermediary messages about its operations.getTopNodes()
function awaits for a "results have arrived" message by checking with the WatchTower at regular interval, and moves on to execute once it has arrived.compute
button had returned very quickly, since all the operations are non blocking. A poll (front end side, backend side) tells the frontend to navigate to the results page when the WatchTower has received a msg saying topNodes have arrived.Things that are weird (but I am ok with them)
I see it as a way to avoid using frameworks that would professionally handle of all of this. I have a taste for keeping things as framework-free as a I can.
Things that are not satisfying (and finally, the question!)
If that would work, that would be more elegant than a polling or a while () loop running and waiting for a result, right?
But
FacesContext context = FacesContext.getCurrentInstance();
returns an error because the multi-thread design doesn't go well with the single thread model of JSF (or is it JakartaEE?). As a solution, it might be possible to:startLongRunningOperation()
But I don't have a clear view on how doing that. Thanks for your help!