Skip to content

Commit

Permalink
#2, #3 Improve thread and exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
fluorumlabs committed Mar 5, 2019
1 parent a861310 commit 2753f76
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ to pool size of 25 threads. You can change that with `AsyncManager.getInstance()
<dependency>
<groupId>org.vaadin.helper</groupId>
<artifactId>async-manager</artifactId>
<version>1.0.0-beta1</version>
<version>1.0.0-beta2</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.vaadin.helper</groupId>
<artifactId>async-manager</artifactId>
<version>1.0.0-beta1</version>
<version>1.0.0-beta2</version>
<name>Async Manager</name>
<description>Async Manager for Vaadin Flow</description>

Expand Down
54 changes: 35 additions & 19 deletions src/main/java/org/vaadin/flow/helper/AsyncTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public class AsyncTask {
* push is used for current task
*/
private AtomicInteger missedPolls = new AtomicInteger();
/**
* {@code true}, if thread may be interrupted if UI/Component detaches
*/
private boolean mayInterrupt = true;

/**
* Create a new task
Expand All @@ -78,33 +82,31 @@ public void push(Command command) {
if (parentUI == null) {
return;
}
if (missedPolls.get() == PUSH_ACTIVE && parentUI.getPushConfiguration().getPushMode() == PushMode.MANUAL) {
parentUI.accessSynchronously(() -> {
try {
command.execute();
boolean mustPush = missedPolls.get() == PUSH_ACTIVE && parentUI.getPushConfiguration().getPushMode() == PushMode.MANUAL;
parentUI.accessSynchronously(() -> {
try {
command.execute();
if (mustPush) {
parentUI.push();
} catch (UIDetachedException ignore) {
// Do not report
// How could this even happen?
} catch (Exception e) {
// Dump
asyncManager.handleException(this, e);
}
});
} else {
// Automatic -- changes will be pushed automatically
// Disabled -- we're using polling and this is called
// within UIDLRequestHandler
parentUI.accessSynchronously(command);
}
} catch (UIDetachedException ignore) {
// Do not report
// How could this even happen?
} catch (Exception e) {
// Dump
asyncManager.handleException(this, e);
}
});
}

/**
* Cancel and unregister the task
* Cancel and unregister the task. Thread interruption behaviour is controlled
* by {@link AsyncTask#allowThreadInterrupt()} and
* {@link AsyncTask#preventThreadInterrupt()} methods.
*/
public void cancel() {
if (!task.isCancelled() && !task.isDone()) {
task.cancel(true);
task.cancel(mayInterrupt);
}
remove();
}
Expand All @@ -129,6 +131,20 @@ public void await() throws ExecutionException, InterruptedException {
task.get();
}

/**
* Allow worker thread to be interrupted when UI or Component detaches. Default behaviour.
*/
public void allowThreadInterrupt() {
this.mayInterrupt = true;
}

/**
* Prevent worker thread interruption when UI or Component detaches.
*/
public void preventThreadInterrupt() {
this.mayInterrupt = false;
}

//--- Implementation

/**
Expand Down

0 comments on commit 2753f76

Please sign in to comment.