Search code examples
javastrutsexecutorservicefuturethreadpoolexecutor

How to monitor background task done by future task


Hi all i am using Struts1.1, on click of a button in JSP we have a lots of data to process which involves DB validation and for millions of record it takes more than 2 hours. So we want to execute in the background using executor service and future task of java asynchronously.So that user can freely move to other pages while background processing is done and when that process is completed user can come back to that same jsp to click another button(activated after processing is done) for viewing the result of data processing: Here is my action class:

ServletContext servletContext=getServlet().getServletContext();
        ExecutorService poolService = (ExecutorService) servletContext.getAttribute("threadPoolAlias");
        AdminBlocageBackgroundProcessing adminBlocageBackgroundProcessing= new AdminBlocageBackgroundProcessing(fichier,blocage);
        Future<ArrayList> future= poolService.submit(adminBlocageBackgroundProcessing);

This is my model class where the database validations are done :

public class AdminBlocageBackgroundProcessing implements Callable {
private FormFile fichier;
private String blocage;
public ArrayList data=null;
public AdminBlocageBackgroundProcessing(FormFile fichier, String data) {
this.fichier=fichier;
this.blocage=data;
    // TODO Auto-generated constructor stub
}

public Object call( ) {
    // TODO Auto-generated method stub
    try{
    data = ImportMetier.extractFromCSV(
        new String(fichier.getFileData(), 
                   "ISO-8859-1"),blocage);

    }
    catch (Exception e) {
        e.printStackTrace();
    }
    return data;
}

Now my question is how can i manage to monitor execution of this "call" method on application level so that when its completed i can show the results of data processing.

and most importantly i want to execute it in background so that user can move freely in other parts of application so i cannot use

data=future.get();

because it will cause main thread to wait till the call method is not completed.

Thanks in advance :-)


Solution

  • Why can't pass the timeout with Future#get() or use Future#isDone() to check if the task has been completed?