0

I'm making a web application to use the JAVE library online (http://www.sauronsoftware.it/projects/jave/):

  • The user fills the form
  • uploads the video
  • the video is converted by the server
  • the user downloads the video

The interface is a jsp page with form, an upload progress bar (made with http://malsup.com/jquery/form/#download) and a progress bar made in JS that starts ajax polling on a progress servlet (after the upload progress is complete). The progress servlet takes the encoderListener from HttpSession that gives the % value of the encoding progress. I'm having troubles with the conversion task because it blocks the doPost method of the servlets that handles the form. So the user never receives the response that put the JS snippet of the progress bars in the complete state to start the ajax-polling. I thought to put it in a thread so the doPost method returns while the conversion is going and the JS script keeps track of it through the ProgressServlet. Now the problem is that I cannot print the exception message back to the user anymore. What can I do? Here some code:

The Converter class just executes the enc.encode(...) method in a try/catch.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    Part part = request.getPart("filename");
    InputStream inStream = part.getInputStream();
    String fileName = part.getSubmittedFileName();
    //upload path
    File uploads = new File(System.getProperty("user.home")+System.getProperty("file.separator"));
    File file = new File(uploads,fileName);
    OutputStream outStream = new FileOutputStream(file);
    byte[] buffer = new byte[8 * 1024];
    int bytesRead;
    while ((bytesRead = inStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, bytesRead);
    }
    outStream.close();
    PrintWriter writer = response.getWriter();
    response.setContentType("text/html");
    writer.println("File uploaded");        

    AudioAttributes audio = new AudioAttributes();
    VideoAttributes video = new VideoAttributes();
    EncodingAttributes attrs = new EncodingAttributes();
    Encoder enc = new Encoder();
    EncoderListener elist = new EncoderListener();      
    HttpSession session = request.getSession();
    session.setAttribute("listener", elist);

    File target = new File(System.getProperty("user.home")+System.getProperty("file.separator"),request.getParameter("outFile"));

    attrs.setFormat(request.getParameter("formats"));

    setAttributes(request,audio,video);

    attrs.setVideoAttributes(video);
    attrs.setAudioAttributes(audio);

    //long running task
    Converter conv = new Converter(file,target,enc,elist,attrs);
    Thread t = new Thread(conv);
    t.start();      

}

This is the snippet that handles the upload progress bar, and the conversion progress bar.

<script>
$(function() {

    var bar = $("#bar");
    var percent = $("#percent");
    var status = $('#status');

    $('form').ajaxForm({
        beforeSend: function() {
            status.empty();
            var percentVal = '0%';
            bar.width(percentVal);
            percent.html(percentVal);
            $("#ConversionBar").width(percentVal);
      $("#ConversionPercent").html(percentVal);
        },
        uploadProgress: function(event, position, total, percentComplete) {
            var percentVal = percentComplete + '%';
            bar.width(percentVal);
            percent.html(percentVal);
        },
        complete: function(xhr) {
         status.html(xhr.responseText);
         var IntervalID = setInterval(function(){
             $.get("/ElaboratoAT/ProgressServlet",function(data,status){
              $("#ConversionBar").width(data/10+'%');
              $("#ConversionPercent").html(data/10+'%');
              if( data/10 >= 100)
               clearInterval(IntervalID);
              $("#download").show();
             })
            },100);
        }
    });
}); 
</script>
This is the servlet that gives the progress.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    HttpSession session = request.getSession();
    response.setContentType("text/html");
    EncoderListener elist = (EncoderListener)session.getAttribute("listener");
    response.getWriter().println(elist.getProgress());
}

Thank you for your help and time.

1 Answers1

0

Starting new thread for each request is not a good solution. There is a limit for threads application can start. It will eventually run out of resources.

To perform long running tasks you need asynchronous servlet. Solutions:

  1. Write your own realization using thread pools in servlet and executing tasks within it.
  2. Use existing solution like Asynchronous processing.

Here is code examples.

Community
  • 1
  • 1