Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
How to interrupt a server request?
I have a very simple vaadin web application: The user enters some text in a text field, presses a button, the server does some computation, the result is displayed in a table.
The problem I have is that said computation can take a while (depending on the user input). I want the users to be able to abort the computation if they run out of patience. However, I can't figure out how to do this. The web-application is unresponsive until the computation finishes (the yellow-then-red-circle in the upper right corner is displayed) so I can't just add an "abort" button. The "Stop loading this page" button in the browser navigation is also disabled.
It is probably possible to do something very complicated involving the button to trigger a backgorund thread to do the computation and then reuglar polling if the result is available, but this is rather messy and resource wasting (polling requests).
I also don't want to abort based on a server based timeout, as I want aborts only to happen if the user runs out of patience.
Is there a simple solution to this?
Any long running computation should be forked to a thread anyway.
When you click on the button, you create a Thread object and keep a variable myThread.
When the user clicks on the interrupt button, you call myThread.interrupt().
Because your Thread will be doing some heavy computation, it will need to check it's interrupted status periodically (this is inexpensive) See http://java.sun.com/docs/books/tutorial/essential/concurrency/interrupt.html
thanks for your reply. In most cases I'd agree with you,
My application however is of a very synchronous nature. There is nothing useful the user can do until the server request has finished; apart from aborting the whole thing. So in addition to starting the background thread, I'd also have to disable and then reenable all GUI elements (and add an abort button).
Also, the polling issue I'm worried about is less on the backround thread, but on the GUI. As, as far as I understand, Vaadin currently does not support server-push, the client somehow has to frequently poll the server to check if results are available. This seems possible with some add-ons, but it is less than straight forward.
I'd have hoped there is a less involved sollution for this problem; e.g. an abort option in the upper-right-corner-progress-wheel, or enable the browser "abort page load" button.
In order to do this, you must have the first request return immediately, which in turn requires you to start a thread for the long running task, even if your app is otherwise synchronous.
In your case you might want to pop up a modal window (so that you don't have to disable everything) with a ProgressIndicator (no need for add-ons for this - use indeterminate mode if you can't estimate the length of the task) and a cancel button.
This sounds like the right solution, thanks. Is there a mode for the ProgressIndicator when you don't know how long the background task will take? E.g. the download bars often go continuously back and forth if the program can't determine how big the file is (and thus how long it will take).
Yes there is -- that's what the message from Mark referred to as "indeterminate mode"