Need confirmation/consultation on Asyncronous vs Thread implementation
posted 1 year ago
Good day everyone,
I've been trying to implement AsyncContext process with no success so far but using Thread class is working. What I have is a page, that, when being loaded, another process needs to sort items in database to have them ready presented later in the web app. So while a user is on a view.jsp page, a background process is sorting items in database, storing them in session object, and has them ready presented on another view2.page. What I have so far is MyShowViewServlet:
This seems to be working. So far I just have a for-loop with large value to iterate over to see how it works SortAsync:
So, I am acheiving a result I want without using AsyncContext - the approach with Thread seems to be working.
My question to gurus: is this the right approach to have separate Thread run like this or there will be bad side-effects I am not aware of? I just need to get a second opinion. Please advise.
ADDED: I have worked more on this implementation and already ran into a problem (for me) when trying to use shared session object. Of course I should have realized that I am creating a separate thread so I am unable to store results in this thread in a session object and use results session object in another servlet.
Is there a way to implement interprocess communication? Does AsyncSupport provide a way to share an object between processes? What I need is to store database results in thread1 and have thread2 be able to access them. I tried session object but it's not working. Any suggestions?