1

In my web application, I have a JSP page to which allows a user to upload a CSV file.
When user submits this upload form, control goes to a Struts2 action, from this action I am starting a new Java thread which handles the reading and processing of this CSV.
I don't want to block the view of application as CSV can be very large so thread handles the uploading and reading of CSV and action returns to some confirmation view page.
Now I want to notify user when thread completes its execution and uploading of file is done.
As soon as thread finishes its execution, I want to pop a javascript alert in my web app with a message "Upload complete. Click here to view".

My current approach(not good) is to pass session object to thread and set a completion flag in thread.

Action

//----
new Thread(new UploadThread(session)).start();
//----

UploadThread.java

//----
try {
   //Reading and processing CSV
} catch() {
   //Exception handling
} finally {
   //Set flag in every case
   session.setAttribute("uploadFlag","true");
}
//----

In the meantime, I set a JS method from one of my JSPs to execute in every 5 seconds. In every 5 secs, this method checks "uploadFlag" from session and if its value is set, it pops javascript alert.

This is working but session object should not be in Thread. Is there some way to achieve this alert from Thread's finally block.

I did some googling and found these SO posts- Open local html page - java and Getting java gui to open a webpage in web browser

Apart from these I've tried to use java.net.URL + openConnection and HttpClient also.
But all these return output of target URL in stream.

Kindly suggest.

8
  • You can use Ajax to request upload status, and as a good practice don't use session for storing such a trivial information Commented May 20, 2015 at 6:17
  • But how do I check, if thread has finished its run() execution using Ajax ? Commented May 20, 2015 at 6:21
  • Well you'll have to maintain that in some place, probably generate an Id, pass it back to the browser, keep a map in backend that maintains the generated Id to updateStatus for each, when thread starts it makes entry in map and when thread finishes updates entry in map. You will have to find a way to clear old entries this map though. Commented May 20, 2015 at 6:24
  • 1
    Maybe you can use Asynchronous HTTP Comet technology javaworld.com/article/2077843/java-web-tier/… . I haven't used it before but it look like think that you look for. Commented May 20, 2015 at 6:28
  • @Nitin, in this approach I have to keep this map somewhere, in some scope so that ajax code can access it. Thread is not coupled to any scope. Where can I place this map? Commented May 20, 2015 at 6:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.