Adjust checkstyle rules. Make checkstyle fail the build when violations are found. Correct all current checkstyle violations.
This commit is contained in:
@ -4,24 +4,23 @@ import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* This application demonstrates the async method invocation pattern. Key parts of the pattern are
|
||||
* <code>AsyncResult</code> which is an intermediate container for an asynchronously evaluated
|
||||
* value, <code>AsyncCallback</code> which can be provided to be executed on task completion and
|
||||
* <code>AsyncExecutor</code> that manages the execution of the async tasks.
|
||||
* <code>AsyncResult</code> which is an intermediate container for an asynchronously evaluated value,
|
||||
* <code>AsyncCallback</code> which can be provided to be executed on task completion and <code>AsyncExecutor</code>
|
||||
* that manages the execution of the async tasks.
|
||||
* <p>
|
||||
* The main method shows example flow of async invocations. The main thread starts multiple tasks
|
||||
* with variable durations and then continues its own work. When the main thread has done it's job
|
||||
* it collects the results of the async tasks. Two of the tasks are handled with callbacks, meaning
|
||||
* the callbacks are executed immediately when the tasks complete.
|
||||
* The main method shows example flow of async invocations. The main thread starts multiple tasks with variable
|
||||
* durations and then continues its own work. When the main thread has done it's job it collects the results of the
|
||||
* async tasks. Two of the tasks are handled with callbacks, meaning the callbacks are executed immediately when the
|
||||
* tasks complete.
|
||||
* <p>
|
||||
* Noteworthy difference of thread usage between the async results and callbacks is that the async
|
||||
* results are collected in the main thread but the callbacks are executed within the worker
|
||||
* threads. This should be noted when working with thread pools.
|
||||
* Noteworthy difference of thread usage between the async results and callbacks is that the async results are collected
|
||||
* in the main thread but the callbacks are executed within the worker threads. This should be noted when working with
|
||||
* thread pools.
|
||||
* <p>
|
||||
* Java provides its own implementations of async method invocation pattern. FutureTask,
|
||||
* CompletableFuture and ExecutorService are the real world implementations of this pattern. But due
|
||||
* to the nature of parallel programming, the implementations are not trivial. This example does not
|
||||
* take all possible scenarios into account but rather provides a simple version that helps to
|
||||
* understand the pattern.
|
||||
* Java provides its own implementations of async method invocation pattern. FutureTask, CompletableFuture and
|
||||
* ExecutorService are the real world implementations of this pattern. But due to the nature of parallel programming,
|
||||
* the implementations are not trivial. This example does not take all possible scenarios into account but rather
|
||||
* provides a simple version that helps to understand the pattern.
|
||||
*
|
||||
* @see AsyncResult
|
||||
* @see AsyncCallback
|
||||
@ -33,6 +32,9 @@ import java.util.concurrent.Callable;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*/
|
||||
public static void main(String[] args) throws Exception {
|
||||
// construct a new executor that will run async tasks
|
||||
AsyncExecutor executor = new ThreadAsyncExecutor();
|
||||
@ -41,10 +43,8 @@ public class App {
|
||||
AsyncResult<Integer> asyncResult1 = executor.startProcess(lazyval(10, 500));
|
||||
AsyncResult<String> asyncResult2 = executor.startProcess(lazyval("test", 300));
|
||||
AsyncResult<Long> asyncResult3 = executor.startProcess(lazyval(50L, 700));
|
||||
AsyncResult<Integer> asyncResult4 =
|
||||
executor.startProcess(lazyval(20, 400), callback("Callback result 4"));
|
||||
AsyncResult<String> asyncResult5 =
|
||||
executor.startProcess(lazyval("callback", 600), callback("Callback result 5"));
|
||||
AsyncResult<Integer> asyncResult4 = executor.startProcess(lazyval(20, 400), callback("Callback result 4"));
|
||||
AsyncResult<String> asyncResult5 = executor.startProcess(lazyval("callback", 600), callback("Callback result 5"));
|
||||
|
||||
// emulate processing in the current thread while async tasks are running in their own threads
|
||||
Thread.sleep(350); // Oh boy I'm working hard here
|
||||
@ -66,8 +66,10 @@ public class App {
|
||||
/**
|
||||
* Creates a callable that lazily evaluates to given value with artificial delay.
|
||||
*
|
||||
* @param value value to evaluate
|
||||
* @param delayMillis artificial delay in milliseconds
|
||||
* @param value
|
||||
* value to evaluate
|
||||
* @param delayMillis
|
||||
* artificial delay in milliseconds
|
||||
* @return new callable for lazy evaluation
|
||||
*/
|
||||
private static <T> Callable<T> lazyval(T value, long delayMillis) {
|
||||
@ -81,7 +83,8 @@ public class App {
|
||||
/**
|
||||
* Creates a simple callback that logs the complete status of the async result.
|
||||
*
|
||||
* @param name callback name
|
||||
* @param name
|
||||
* callback name
|
||||
* @return new async callback
|
||||
*/
|
||||
private static <T> AsyncCallback<T> callback(String name) {
|
||||
|
@ -5,8 +5,6 @@ import java.util.concurrent.ExecutionException;
|
||||
/**
|
||||
*
|
||||
* AsyncResult interface
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public interface AsyncResult<T> {
|
||||
|
||||
|
@ -29,13 +29,12 @@ public class ThreadAsyncExecutor implements AsyncExecutor {
|
||||
} catch (Exception ex) {
|
||||
result.setException(ex);
|
||||
}
|
||||
}, "executor-" + idx.incrementAndGet()).start();
|
||||
} , "executor-" + idx.incrementAndGet()).start();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T endProcess(AsyncResult<T> asyncResult) throws ExecutionException,
|
||||
InterruptedException {
|
||||
public <T> T endProcess(AsyncResult<T> asyncResult) throws ExecutionException, InterruptedException {
|
||||
if (asyncResult.isCompleted()) {
|
||||
return asyncResult.getValue();
|
||||
} else {
|
||||
@ -45,9 +44,8 @@ public class ThreadAsyncExecutor implements AsyncExecutor {
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple implementation of async result that allows completing it successfully with a value or
|
||||
* exceptionally with an exception. A really simplified version from its real life cousins
|
||||
* FutureTask and CompletableFuture.
|
||||
* Simple implementation of async result that allows completing it successfully with a value or exceptionally with an
|
||||
* exception. A really simplified version from its real life cousins FutureTask and CompletableFuture.
|
||||
*
|
||||
* @see java.util.concurrent.FutureTask
|
||||
* @see java.util.concurrent.CompletableFuture
|
||||
@ -71,10 +69,11 @@ public class ThreadAsyncExecutor implements AsyncExecutor {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value from successful execution and executes callback if available. Notifies any
|
||||
* thread waiting for completion.
|
||||
* Sets the value from successful execution and executes callback if available. Notifies any thread waiting for
|
||||
* completion.
|
||||
*
|
||||
* @param value value of the evaluated task
|
||||
* @param value
|
||||
* value of the evaluated task
|
||||
*/
|
||||
void setValue(T value) {
|
||||
this.value = value;
|
||||
@ -86,10 +85,11 @@ public class ThreadAsyncExecutor implements AsyncExecutor {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the exception from failed execution and executes callback if available. Notifies any
|
||||
* thread waiting for completion.
|
||||
* Sets the exception from failed execution and executes callback if available. Notifies any thread waiting for
|
||||
* completion.
|
||||
*
|
||||
* @param exception exception of the failed task
|
||||
* @param exception
|
||||
* exception of the failed task
|
||||
*/
|
||||
void setException(Exception exception) {
|
||||
this.exception = exception;
|
||||
|
@ -55,8 +55,7 @@ public class ThreadAsyncExecutorTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test used to verify the happy path of
|
||||
* {@link ThreadAsyncExecutor#startProcess(Callable, AsyncCallback)}
|
||||
* Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable, AsyncCallback)}
|
||||
*/
|
||||
@Test(timeout = 3000)
|
||||
public void testSuccessfulTaskWithCallback() throws Exception {
|
||||
@ -77,8 +76,7 @@ public class ThreadAsyncExecutorTest {
|
||||
verify(task, times(1)).call();
|
||||
|
||||
// ... same for the callback, we expect our object
|
||||
final ArgumentCaptor<Optional<Exception>> optionalCaptor = ArgumentCaptor
|
||||
.forClass((Class) Optional.class);
|
||||
final ArgumentCaptor<Optional<Exception>> optionalCaptor = ArgumentCaptor.forClass((Class) Optional.class);
|
||||
verify(callback, times(1)).onComplete(eq(result), optionalCaptor.capture());
|
||||
|
||||
final Optional<Exception> optionalException = optionalCaptor.getValue();
|
||||
@ -90,8 +88,8 @@ public class ThreadAsyncExecutorTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable)} when a
|
||||
* task takes a while to execute
|
||||
* Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable)} when a task takes a while
|
||||
* to execute
|
||||
*/
|
||||
@Test(timeout = 5000)
|
||||
public void testLongRunningTaskWithoutCallback() throws Exception {
|
||||
@ -101,9 +99,9 @@ public class ThreadAsyncExecutorTest {
|
||||
final Object result = new Object();
|
||||
final Callable<Object> task = mock(Callable.class);
|
||||
when(task.call()).thenAnswer(i -> {
|
||||
Thread.sleep(1500);
|
||||
return result;
|
||||
});
|
||||
Thread.sleep(1500);
|
||||
return result;
|
||||
});
|
||||
|
||||
final AsyncResult<Object> asyncResult = executor.startProcess(task);
|
||||
assertNotNull(asyncResult);
|
||||
@ -111,8 +109,7 @@ public class ThreadAsyncExecutorTest {
|
||||
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail(
|
||||
"Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task");
|
||||
fail("Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task");
|
||||
} catch (IllegalStateException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
}
|
||||
@ -130,9 +127,8 @@ public class ThreadAsyncExecutorTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test used to verify the happy path of
|
||||
* {@link ThreadAsyncExecutor#startProcess(Callable, AsyncCallback)} when a task takes a while to
|
||||
* execute
|
||||
* Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable, AsyncCallback)} when a task
|
||||
* takes a while to execute
|
||||
*/
|
||||
@Test(timeout = 5000)
|
||||
public void testLongRunningTaskWithCallback() throws Exception {
|
||||
@ -142,9 +138,9 @@ public class ThreadAsyncExecutorTest {
|
||||
final Object result = new Object();
|
||||
final Callable<Object> task = mock(Callable.class);
|
||||
when(task.call()).thenAnswer(i -> {
|
||||
Thread.sleep(1500);
|
||||
return result;
|
||||
});
|
||||
Thread.sleep(1500);
|
||||
return result;
|
||||
});
|
||||
|
||||
final AsyncCallback<Object> callback = mock(AsyncCallback.class);
|
||||
final AsyncResult<Object> asyncResult = executor.startProcess(task, callback);
|
||||
@ -155,8 +151,7 @@ public class ThreadAsyncExecutorTest {
|
||||
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail(
|
||||
"Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task");
|
||||
fail("Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task");
|
||||
} catch (IllegalStateException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
}
|
||||
@ -164,8 +159,7 @@ public class ThreadAsyncExecutorTest {
|
||||
// Our task should only execute once, but it can take a while ...
|
||||
verify(task, timeout(3000).times(1)).call();
|
||||
|
||||
final ArgumentCaptor<Optional<Exception>> optionalCaptor = ArgumentCaptor
|
||||
.forClass((Class) Optional.class);
|
||||
final ArgumentCaptor<Optional<Exception>> optionalCaptor = ArgumentCaptor.forClass((Class) Optional.class);
|
||||
verify(callback, timeout(3000).times(1)).onComplete(eq(result), optionalCaptor.capture());
|
||||
|
||||
final Optional<Exception> optionalException = optionalCaptor.getValue();
|
||||
@ -182,9 +176,8 @@ public class ThreadAsyncExecutorTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable)} when a
|
||||
* task takes a while to execute, while waiting on the result using
|
||||
* {@link ThreadAsyncExecutor#endProcess(AsyncResult)}
|
||||
* Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable)} when a task takes a while
|
||||
* to execute, while waiting on the result using {@link ThreadAsyncExecutor#endProcess(AsyncResult)}
|
||||
*/
|
||||
@Test(timeout = 5000)
|
||||
public void testEndProcess() throws Exception {
|
||||
@ -194,9 +187,9 @@ public class ThreadAsyncExecutorTest {
|
||||
final Object result = new Object();
|
||||
final Callable<Object> task = mock(Callable.class);
|
||||
when(task.call()).thenAnswer(i -> {
|
||||
Thread.sleep(1500);
|
||||
return result;
|
||||
});
|
||||
Thread.sleep(1500);
|
||||
return result;
|
||||
});
|
||||
|
||||
final AsyncResult<Object> asyncResult = executor.startProcess(task);
|
||||
assertNotNull(asyncResult);
|
||||
@ -204,8 +197,7 @@ public class ThreadAsyncExecutorTest {
|
||||
|
||||
try {
|
||||
asyncResult.getValue();
|
||||
fail(
|
||||
"Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task");
|
||||
fail("Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task");
|
||||
} catch (IllegalStateException e) {
|
||||
assertNotNull(e.getMessage());
|
||||
}
|
||||
@ -220,8 +212,7 @@ public class ThreadAsyncExecutorTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test used to verify the behaviour of {@link ThreadAsyncExecutor#startProcess(Callable)} when
|
||||
* the callable is 'null'
|
||||
* Test used to verify the behaviour of {@link ThreadAsyncExecutor#startProcess(Callable)} when the callable is 'null'
|
||||
*/
|
||||
@Test(timeout = 3000)
|
||||
public void testNullTask() throws Exception {
|
||||
@ -229,8 +220,7 @@ public class ThreadAsyncExecutorTest {
|
||||
final ThreadAsyncExecutor executor = new ThreadAsyncExecutor();
|
||||
final AsyncResult<Object> asyncResult = executor.startProcess(null);
|
||||
|
||||
assertNotNull("The AsyncResult should not be 'null', even though the task was 'null'.",
|
||||
asyncResult);
|
||||
assertNotNull("The AsyncResult should not be 'null', even though the task was 'null'.", asyncResult);
|
||||
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
|
||||
@ -246,9 +236,8 @@ public class ThreadAsyncExecutorTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test used to verify the behaviour of
|
||||
* {@link ThreadAsyncExecutor#startProcess(Callable, AsyncCallback)} when the callable is 'null',
|
||||
* but the asynchronous callback is provided
|
||||
* Test used to verify the behaviour of {@link ThreadAsyncExecutor#startProcess(Callable, AsyncCallback)} when the
|
||||
* callable is 'null', but the asynchronous callback is provided
|
||||
*/
|
||||
@Test(timeout = 3000)
|
||||
public void testNullTaskWithCallback() throws Exception {
|
||||
@ -257,13 +246,11 @@ public class ThreadAsyncExecutorTest {
|
||||
final AsyncCallback<Object> callback = mock(AsyncCallback.class);
|
||||
final AsyncResult<Object> asyncResult = executor.startProcess(null, callback);
|
||||
|
||||
assertNotNull("The AsyncResult should not be 'null', even though the task was 'null'.",
|
||||
asyncResult);
|
||||
assertNotNull("The AsyncResult should not be 'null', even though the task was 'null'.", asyncResult);
|
||||
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
|
||||
final ArgumentCaptor<Optional<Exception>> optionalCaptor = ArgumentCaptor
|
||||
.forClass((Class) Optional.class);
|
||||
final ArgumentCaptor<Optional<Exception>> optionalCaptor = ArgumentCaptor.forClass((Class) Optional.class);
|
||||
verify(callback, times(1)).onComplete(Matchers.isNull(), optionalCaptor.capture());
|
||||
|
||||
final Optional<Exception> optionalException = optionalCaptor.getValue();
|
||||
@ -286,9 +273,8 @@ public class ThreadAsyncExecutorTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test used to verify the behaviour of
|
||||
* {@link ThreadAsyncExecutor#startProcess(Callable, AsyncCallback)} when both the callable and
|
||||
* the asynchronous callback are 'null'
|
||||
* Test used to verify the behaviour of {@link ThreadAsyncExecutor#startProcess(Callable, AsyncCallback)} when both
|
||||
* the callable and the asynchronous callback are 'null'
|
||||
*/
|
||||
@Test(timeout = 3000)
|
||||
public void testNullTaskWithNullCallback() throws Exception {
|
||||
@ -296,9 +282,7 @@ public class ThreadAsyncExecutorTest {
|
||||
final ThreadAsyncExecutor executor = new ThreadAsyncExecutor();
|
||||
final AsyncResult<Object> asyncResult = executor.startProcess(null, null);
|
||||
|
||||
assertNotNull(
|
||||
"The AsyncResult should not be 'null', even though the task and callback were 'null'.",
|
||||
asyncResult);
|
||||
assertNotNull("The AsyncResult should not be 'null', even though the task and callback were 'null'.", asyncResult);
|
||||
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||
assertTrue(asyncResult.isCompleted());
|
||||
|
||||
|
Reference in New Issue
Block a user