I have a Java EE application in which the user performs a series of actions over many pages in a linear direction. On one of the first pages I have the details needed to perform a database query, of which the result is required on one of the last. I do not want to make the user to wait for this database query to execute between pages, instead I want to run it in the background whilst the user is continuing.
What is the best way of doing this?
I know use of threads in a Java EE application is discouraged.
You can use the data provided in the first step to invoke an operation on asynchronous EJB or Servlet (although if you use Java EE you should go with EJB as it’s better suited for this kind of job).
Take a look here: Execute subprocesses in JavaEE 6
You can save the
Future<?>object that asynchronous methods returns and, on the last page, invoke itsget(-)command. You can even block the user at this last stage as you probably want to show him the results instead of allowing him to proceed.