-
Notifications
You must be signed in to change notification settings - Fork 4
Correct usage of AsyncComponent
albrechta edited this page Dec 14, 2014
·
2 revisions
The execute() and rollback() methods returns a Future<T> object, where T is the type of the result.
Possible exceptions can also be retrieved through this Future<T> object.
The following code snippet shows the correct execution and result retrieval of the AsyncComponent. It also shows how to deal with possible exceptions.
The rollback works equivalently, but rollback() throws a ProcessRollbackException instead of a ProcessExecutionException.
AsyncComponent<Integer> asyncComp = new AsyncComponent<>(...);
Future<Integer> futureResult;
int result;
// start the process
try {
futureResult = asyncComp.execute();
// immediate return
} catch (InvalidProcessStateException ex) {
System.err.println("Invalid state for execution.");
} catch (ProcessExecutionException ex) {
System.err.println("AsyncComponent has encountered an error during execution.");
}
// do other stuff meanwhile...
// get the result
try {
result = futureResult.get();
} catch (InterruptedException | CancellationException ex) {
System.err.println("Something went wrong with the thread.");
} catch (ExecutionException ex) {
// the thread threw an exception
if (ex.getCause() instanceof ProcessExecutionException) {
System.err.println("Some async process component threw an exception during execution.");
}
}