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.

Community
  • 1
  • 1
the-dumb-programmer
  • 485
  • 3
  • 14
  • 27
  • You can use Ajax to request upload status, and as a good practice don't use session for storing such a trivial information – Nitin Dandriyal May 20 '15 at 06:17
  • But how do I check, if thread has finished its run() execution using Ajax ? – the-dumb-programmer May 20 '15 at 06: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. – Nitin Dandriyal May 20 '15 at 06:24
  • 1
    Maybe you can use Asynchronous HTTP Comet technology http://www.javaworld.com/article/2077843/java-web-tier/java-se-asynchronous-http-comet-architectures.html . I haven't used it before but it look like think that you look for. – Paweł Adamski May 20 '15 at 06: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? – the-dumb-programmer May 20 '15 at 06:31
  • @Pawel, I'll check it out – the-dumb-programmer May 20 '15 at 06:31
  • There can be a single map for entire application, When you create the Thread pass on the handle to the map to the Thread so that Thread can update the same, ajax code can call some action that has handle to this global map – Nitin Dandriyal May 20 '15 at 06:35
  • @PawełAdamski It's a great article, learned something new. – the-dumb-programmer May 20 '15 at 07:24

0 Answers0