0

I have a Vaadin application and I'm implementing some asynchronous background processing. As far as I know, all requests from the client are processed by one of the threads from the Tomcat's thread pool and after a request is processed the response (the updated application's state) is returned to the client and rendered.

Because I have some tasks I want to perform in background, I need to display a 'Loading...' label in the main HTTP thread, and after it's displayed I need to start a background Thread which performs the task and when it finishes, the application's state is pushed to the client (using ICEPush add-on).

The problem is that it seems to me that sometimes the background thread finishes BEFORE the main HTTP thread returns the respons to the client, therefore no 'Loading...' label is displayed and sometimes the application's state is not fully updated on the client because of that. Therefore I need to start the background thread AFTER the main HTTP thread returns the response.

Is there a way to do that? Or am I completely wrong about this approach?

Thanks!

janhink
  • 4,943
  • 3
  • 29
  • 37
  • It sounds like you should be prepared for the background thread to finish quickly, not rely on it taking more time. . . Update the UI code. – Jasper Blues Jan 31 '13 at 10:08

1 Answers1

0

As I have been learned, updating UI in a thread must be done together with locking mechanism. Here is an example:

class Calculation implements Runnable {

    private long result = 0;

    private final Label label;

    public Calculation(Label label) {
        this.label = label;
    }

    @Override
    public void run() {
           // calculate or fetch the result (here is the time consuming operation) 
           getSession().getLockInstance().lock();
           try {
               // inform UI about result
               label.setValue("Result is: " + result);
           } finally {
               getSession().getLockInstance().unlock();
           }
     }
}
Ondrej Kvasnovsky
  • 4,592
  • 3
  • 30
  • 40
  • I cannot seem to find the method getLockInstance() anywhere in the Vaadin code. Is this Vaadin 7? I use Vaadin 6.8.8. – janhink Feb 06 '13 at 13:35
  • Yes, it is just in Vaadin 7. In Vaadin 6, try this: http://stackoverflow.com/questions/11553576/asynchronously-update-vaadin-components – Ondrej Kvasnovsky Feb 08 '13 at 13:40