You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by jg...@apache.org on 2018/12/06 22:23:16 UTC

[12/27] tomee git commit: TOMEE-2301 - improving javadoc and adding output messages to track the execution. Use TimeUnit Sleep.

TOMEE-2301 - improving javadoc and adding output messages to track the execution. Use TimeUnit Sleep.

Signed-off-by: brunobat <br...@gmail.com>


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/de075f8d
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/de075f8d
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/de075f8d

Branch: refs/heads/master
Commit: de075f8d9d7c96f26f9684119b150e7f52eaa225
Parents: d0c7290
Author: brunobat <br...@gmail.com>
Authored: Mon Dec 3 16:16:12 2018 +0000
Committer: brunobat <br...@gmail.com>
Committed: Wed Dec 5 15:27:04 2018 +0000

----------------------------------------------------------------------
 .../org/superbiz/executor/ManagedService.java   | 27 +++++++++++---------
 .../superbiz/executor/ManagedServiceTest.java   | 14 +++++-----
 2 files changed, 22 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/de075f8d/examples/concurrency-utils/src/main/java/org/superbiz/executor/ManagedService.java
----------------------------------------------------------------------
diff --git a/examples/concurrency-utils/src/main/java/org/superbiz/executor/ManagedService.java b/examples/concurrency-utils/src/main/java/org/superbiz/executor/ManagedService.java
index 89c89fe..eecbb46 100644
--- a/examples/concurrency-utils/src/main/java/org/superbiz/executor/ManagedService.java
+++ b/examples/concurrency-utils/src/main/java/org/superbiz/executor/ManagedService.java
@@ -21,6 +21,7 @@ import javax.annotation.Resource;
 import javax.enterprise.concurrent.ManagedExecutorService;
 import javax.enterprise.context.RequestScoped;
 import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
 import java.util.function.Supplier;
 
 import static java.util.Objects.nonNull;
@@ -40,8 +41,9 @@ public class ManagedService {
      * @return A {@link CompletableFuture} that will return immediately.
      */
     public CompletableFuture<Integer> asyncTask(final int value) {
+        System.out.println("Create asyncTask");
         return CompletableFuture
-                .supplyAsync(delayedTask(value, 100, null), executor) // Execute asynchronously.
+                .supplyAsync(longTask(value, 100, null), executor) // Execute asynchronously.
                 .thenApply(i -> i + 1); // After the return of the task, do something else with the result.
     }
 
@@ -53,22 +55,23 @@ public class ManagedService {
      * @return A {@link CompletableFuture} that will return immediately.
      */
     public CompletableFuture<Integer> asyncTaskWithException(final int value) {
+        System.out.println("Create asyncTaskWithException");
         return CompletableFuture
-                .supplyAsync(delayedTask(value, 100, "Planned exception"), executor) // Execute asynchronously.
+                .supplyAsync(longTask(value, 100, "Planned exception"), executor) // Execute asynchronously.
                 .thenApply(i -> i + 1); // After the return of the task, do something else with the result.
     }
 
     /**
      * Method to simulate an asynchronous task. Will add 1 to the value for each invocation.
      *
-     * @param value        The demo data.
-     * @param delayMs      How long the task will take to complete. In ms.
-     * @param errorMessage Message for the exception simulating an execution problem
-     * @return
+     * @param value          The demo data.
+     * @param taskDurationMs How long the task will take to complete. In ms.
+     * @param errorMessage   Message for the exception simulating an execution problem
+     * @return a {@link Supplier} function processing the new value
      */
-    private Supplier<Integer> delayedTask(final int value,
-                                          final int delayMs,
-                                          final String errorMessage) {
+    private Supplier<Integer> longTask(final int value,
+                                       final int taskDurationMs,
+                                       final String errorMessage) {
         return () -> {
             if (nonNull(errorMessage)) {
                 System.out.println("Exception will be thrown");
@@ -76,12 +79,12 @@ public class ManagedService {
             }
 
             try {
-                // simulate long processing task
-                Thread.sleep(delayMs);
+                // Simulate a long processing task using TimeUnit to sleep.
+                TimeUnit.MILLISECONDS.sleep(taskDurationMs);
             } catch (InterruptedException e) {
                 throw new RuntimeException("Problem while waiting");
             }
-            System.out.println("delayedTask complete");
+            System.out.println("longTask complete");
             return value + 1;
         };
     }

http://git-wip-us.apache.org/repos/asf/tomee/blob/de075f8d/examples/concurrency-utils/src/test/java/org/superbiz/executor/ManagedServiceTest.java
----------------------------------------------------------------------
diff --git a/examples/concurrency-utils/src/test/java/org/superbiz/executor/ManagedServiceTest.java b/examples/concurrency-utils/src/test/java/org/superbiz/executor/ManagedServiceTest.java
index 1dd6ff9..30b6f6f 100644
--- a/examples/concurrency-utils/src/test/java/org/superbiz/executor/ManagedServiceTest.java
+++ b/examples/concurrency-utils/src/test/java/org/superbiz/executor/ManagedServiceTest.java
@@ -66,9 +66,9 @@ public class ManagedServiceTest {
     /**
      * Request timeout. The result will take at least 100ms and we want it after 10ms.
      *
-     * @throws InterruptedException
-     * @throws ExecutionException
-     * @throws TimeoutException
+     * @throws InterruptedException we don't expect it
+     * @throws ExecutionException   we don't expect it
+     * @throws TimeoutException     Expected exception
      */
     @Test(expected = TimeoutException.class)
     public void managedInvocationTestWithTimeout() throws InterruptedException, ExecutionException, TimeoutException {
@@ -78,11 +78,11 @@ public class ManagedServiceTest {
 
     /**
      * The execution ended with an exception.
-     * Handle the exception apropriately.
+     * Handle the exception appropriately.
      *
-     * @throws InterruptedException
-     * @throws ExecutionException
-     * @throws TimeoutException
+     * @throws InterruptedException we don't expect it
+     * @throws ExecutionException   Expected exception
+     * @throws TimeoutException     we don't expect it
      */
     @Test
     public void managedInvocationTestWithException() {