Blog

Community Answer: Processing a File in a Background Thread

By  
Alejandro Duarte
Alejandro Duarte
·
On Dec 7, 2017 8:31:00 AM
·

A couple of weeks ago I found this interesting question in the forum:

I figured it was a perfect chance to try some live coding and record a video explaining how to implement a simple application with these requirements:

1. Allow users to upload a file.

2. Show a spinner in the UI while the file is being uploaded.

3. When the file is uploaded, process the file in a background thread in the server.

4. Show the progress of the processing task in the UI.

Here’s the video showing how to implement the app from scratch using Spring Boot and Vaadin Framework 8 (code available on GitHub):

The main takeaway is to enable push and use the UI.access(Runnable) method when making changes to the UI from a new thread. For example:

@Push // enable UI updates from background threads
public class VaadinUI extends UI {
    ...

    private void someMethod() {
        ...

        new Thread(() -> {
            ...

            access(() -> {
                // UI modification code here
            });
            ...

        }).start();
    }
}

Stay tuned and see you next week with another community inspired answer!

Alejandro Duarte
Alejandro Duarte
Software Engineer and Developer Relations Manager at Vaadin Ltd. Author of Practical Vaadin (Apress), Data-Centric Applications with Vaadin 8 (Packt), and Vaadin 7 UI Design by Example (Packt). Passionate about software development with Java and open-source technologies. Contact him on Twitter @alejandro_du or through his personal blog at www.programmingbrain.com.
Other posts by Alejandro Duarte