tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jgallim...@apache.org
Subject [09/27] tomee git commit: TOMEE-2301 - Add javadoc and use a Logger instead of System.out.println
Date Thu, 06 Dec 2018 22:23:13 GMT
TOMEE-2301 - Add javadoc and use a Logger instead of System.out.println

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


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

Branch: refs/heads/master
Commit: ce01d8d9b362e56a121925dd070dba53290d6ffc
Parents: 934814e
Author: brunobat <brunobat@gmail.com>
Authored: Tue Dec 4 11:43:47 2018 +0000
Committer: brunobat <brunobat@gmail.com>
Committed: Wed Dec 5 15:27:04 2018 +0000

----------------------------------------------------------------------
 examples/concurrency-utils/pom.xml              |  2 +-
 .../executor/ManagedScheduledService.java       | 67 ++++++++++++++++----
 .../org/superbiz/executor/ManagedService.java   | 11 ++--
 .../executor/ManagedScheduledServiceTest.java   | 41 +++++++++---
 .../superbiz/executor/ManagedServiceTest.java   |  5 +-
 5 files changed, 101 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/ce01d8d9/examples/concurrency-utils/pom.xml
----------------------------------------------------------------------
diff --git a/examples/concurrency-utils/pom.xml b/examples/concurrency-utils/pom.xml
index 0e0fba6..8a9de66 100644
--- a/examples/concurrency-utils/pom.xml
+++ b/examples/concurrency-utils/pom.xml
@@ -56,7 +56,7 @@
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-compiler-plugin</artifactId>
-                <version>3.7.1</version>
+                <version>3.7.0</version>
                 <configuration>
                     <source>1.8</source>
                     <target>1.8</target>

http://git-wip-us.apache.org/repos/asf/tomee/blob/ce01d8d9/examples/concurrency-utils/src/main/java/org/superbiz/executor/ManagedScheduledService.java
----------------------------------------------------------------------
diff --git a/examples/concurrency-utils/src/main/java/org/superbiz/executor/ManagedScheduledService.java
b/examples/concurrency-utils/src/main/java/org/superbiz/executor/ManagedScheduledService.java
index 07760a3..e5c1e75 100644
--- a/examples/concurrency-utils/src/main/java/org/superbiz/executor/ManagedScheduledService.java
+++ b/examples/concurrency-utils/src/main/java/org/superbiz/executor/ManagedScheduledService.java
@@ -24,6 +24,7 @@ import java.util.concurrent.Callable;
 import java.util.concurrent.Future;
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
+import java.util.logging.Logger;
 
 import static java.util.Objects.nonNull;
 
@@ -31,41 +32,85 @@ import static java.util.Objects.nonNull;
 @RequestScoped
 public class ManagedScheduledService {
 
+    private static final Logger LOGGER = Logger.getLogger(ManagedScheduledService.class.getSimpleName());
+
     @Resource
     private ManagedScheduledExecutorService executor;
 
-    public Future<Integer> singleFixedDelayTask(final int value, final String errorMessage)
{
-        System.out.println("longCallableTask scheduled");
-        return executor.schedule(longCallableTask(value, 10, errorMessage), 100, TimeUnit.MILLISECONDS);
+    /**
+     * Execute a task after a planned delay and get the result back by using a {@link Callable}
+     *
+     * @param value        The value to compute
+     * @param errorMessage If not null an exception with be thrown with this message
+     * @return the processed result
+     */
+    public Future<Integer> singleFixedDelayTask(final int value,
+                                                final String errorMessage) {
+        LOGGER.info("longCallableTask scheduled");
+        return executor.schedule(
+                longCallableTask(value, 10, errorMessage), 100, TimeUnit.MILLISECONDS);
     }
 
-    public ScheduledFuture<?> periodicFixedDelayTask(final int value, final String
errorMessage) {
-        System.out.println("longRunnableTask scheduled");
-        return executor.scheduleAtFixedRate(longRunnableTask(value, 10, errorMessage), 0,
100, TimeUnit.MILLISECONDS);
+    /**
+     * Execute a task periodically. Although a future is returned, it will not contain a
result because the
+     * executor uses a runnable to perform the operations.<br>
+     * If an exception happens, the task will stop and you can catch the exception with the
{@link ScheduledFuture}.
+     *
+     * @param value        The value to compute
+     * @param errorMessage If not null an exception with be thrown with this message
+     * @return An object where you can cancel the periodic task and check for exceptions.
+     */
+    public ScheduledFuture<?> periodicFixedDelayTask(final int value,
+                                                     final String errorMessage) {
+        LOGGER.info("longRunnableTask scheduled");
+        return executor.scheduleAtFixedRate(
+                longRunnableTask(value, 10, errorMessage), 0, 100, TimeUnit.MILLISECONDS);
     }
 
+    /**
+     * Will simulate a long running operation
+     *
+     * @param value          The value to compute
+     * @param taskDurationMs the time lenght of the operation
+     * @param errorMessage   If not null an exception with be thrown with this message
+     * @return a {@link Runnable}
+     */
     private Runnable longRunnableTask(final int value,
                                       final int taskDurationMs,
                                       final String errorMessage) {
         return () -> {
             if (nonNull(errorMessage)) {
-                System.out.println("Exception will be thrown");
+                LOGGER.severe("Exception will be thrown");
                 throw new RuntimeException(errorMessage);
             }
+            try {
+                // Simulate a long processing task using TimeUnit to sleep.
+                TimeUnit.MILLISECONDS.sleep(taskDurationMs);
+            } catch (InterruptedException e) {
+                throw new RuntimeException("Problem while waiting");
+            }
 
             Integer result = value + 1;
-            System.out.println("longRunnableTask complete. Value is " + result);
+            LOGGER.info("longRunnableTask complete. Value is " + result);
             // Cannot return result with a Runnable.
         };
     }
 
+    /**
+     * Will simulate a long running operation
+     *
+     * @param value          The value to compute
+     * @param taskDurationMs the time lenght of the operation
+     * @param errorMessage   If not null an exception with be thrown with this message
+     * @return a {@link Callable} with the result
+     */
     private Callable<Integer> longCallableTask(final int value,
                                                final int taskDurationMs,
                                                final String errorMessage) {
         return () -> {
-            System.out.println("longCallableTask start");
+            LOGGER.info("longCallableTask start");
             if (nonNull(errorMessage)) {
-                System.out.println("Exception will be thrown");
+                LOGGER.severe("Exception will be thrown");
                 throw new RuntimeException(errorMessage);
             }
 
@@ -75,7 +120,7 @@ public class ManagedScheduledService {
             } catch (InterruptedException e) {
                 throw new RuntimeException("Problem while waiting");
             }
-            System.out.println("longCallableTask complete");
+            LOGGER.info("longCallableTask complete");
             // We can return a result with a Callable.
             return value + 1;
         };

http://git-wip-us.apache.org/repos/asf/tomee/blob/ce01d8d9/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 eecbb46..a8aa2cb 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
@@ -23,6 +23,7 @@ import javax.enterprise.context.RequestScoped;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.TimeUnit;
 import java.util.function.Supplier;
+import java.util.logging.Logger;
 
 import static java.util.Objects.nonNull;
 
@@ -30,6 +31,8 @@ import static java.util.Objects.nonNull;
 @RequestScoped
 public class ManagedService {
 
+    private static final Logger LOGGER = Logger.getLogger(ManagedService.class.getSimpleName());
+
     @Resource
     private ManagedExecutorService executor;
 
@@ -41,7 +44,7 @@ public class ManagedService {
      * @return A {@link CompletableFuture} that will return immediately.
      */
     public CompletableFuture<Integer> asyncTask(final int value) {
-        System.out.println("Create asyncTask");
+        LOGGER.info("Create asyncTask");
         return CompletableFuture
                 .supplyAsync(longTask(value, 100, null), executor) // Execute asynchronously.
                 .thenApply(i -> i + 1); // After the return of the task, do something
else with the result.
@@ -55,7 +58,7 @@ public class ManagedService {
      * @return A {@link CompletableFuture} that will return immediately.
      */
     public CompletableFuture<Integer> asyncTaskWithException(final int value) {
-        System.out.println("Create asyncTaskWithException");
+        LOGGER.info("Create asyncTaskWithException");
         return CompletableFuture
                 .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.
@@ -74,7 +77,7 @@ public class ManagedService {
                                        final String errorMessage) {
         return () -> {
             if (nonNull(errorMessage)) {
-                System.out.println("Exception will be thrown");
+                LOGGER.severe("Exception will be thrown");
                 throw new RuntimeException(errorMessage);
             }
 
@@ -84,7 +87,7 @@ public class ManagedService {
             } catch (InterruptedException e) {
                 throw new RuntimeException("Problem while waiting");
             }
-            System.out.println("longTask complete");
+            LOGGER.info("longTask complete");
             return value + 1;
         };
     }

http://git-wip-us.apache.org/repos/asf/tomee/blob/ce01d8d9/examples/concurrency-utils/src/test/java/org/superbiz/executor/ManagedScheduledServiceTest.java
----------------------------------------------------------------------
diff --git a/examples/concurrency-utils/src/test/java/org/superbiz/executor/ManagedScheduledServiceTest.java
b/examples/concurrency-utils/src/test/java/org/superbiz/executor/ManagedScheduledServiceTest.java
index 4baf428..1a859ba 100644
--- a/examples/concurrency-utils/src/test/java/org/superbiz/executor/ManagedScheduledServiceTest.java
+++ b/examples/concurrency-utils/src/test/java/org/superbiz/executor/ManagedScheduledServiceTest.java
@@ -30,6 +30,7 @@ import java.util.concurrent.Future;
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
+import java.util.logging.Logger;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
@@ -37,6 +38,8 @@ import static org.junit.Assert.fail;
 @RunWith(Arquillian.class)
 public class ManagedScheduledServiceTest {
 
+    private static final Logger LOGGER = Logger.getLogger(ManagedScheduledServiceTest.class.getSimpleName());
+
     @Inject
     private ManagedScheduledService scheduledService;
 
@@ -48,29 +51,44 @@ public class ManagedScheduledServiceTest {
     }
 
 
+    /**
+     * Happy path with multiple tasks to be executed after a planed amount of time.
+     *
+     * @throws InterruptedException we don't expect it
+     * @throws ExecutionException   we don't expect it
+     * @throws TimeoutException     we don't expect it
+     */
     @Test
     public void singleFixedDelayTask() throws InterruptedException, ExecutionException, TimeoutException
{
         final Future<Integer> futureA = scheduledService.singleFixedDelayTask(1, null);
         final Future<Integer> futureB = scheduledService.singleFixedDelayTask(50, null);
-        System.out.println("Do some other work while we wait for the tasks");
+        LOGGER.info("Do some other work while we wait for the tasks");
         assertEquals(2, futureA.get(200, TimeUnit.MILLISECONDS).intValue());
         assertEquals(51, futureB.get(200, TimeUnit.MILLISECONDS).intValue());
 
     }
 
+    /**
+     * Happy path with single task to be executed periodically until it's canceled.
+     *
+     * @throws InterruptedException we don't expect it
+     */
     @Test
-    public void periodicFixedDelayTask() throws InterruptedException, TimeoutException, ExecutionException
{
+    public void periodicFixedDelayTask() throws InterruptedException {
         final ScheduledFuture<?> scheduledFuture = scheduledService.periodicFixedDelayTask(1,
null);
+        LOGGER.info("Do some other work while we wait for the tasks");
         TimeUnit.MILLISECONDS.sleep(500);
         if (!scheduledFuture.isCancelled()) {
             scheduledFuture.cancel(true);
-            System.out.println("task stopped");
+            LOGGER.info("task stopped");
         }
     }
 
-
+    /**
+     * Exception happens while processing the task executed after a planed amount of time.
+     */
     @Test
-    public void singleFixedDelayTaskWithException() throws InterruptedException, ExecutionException,
TimeoutException {
+    public void singleFixedDelayTaskWithException() {
         final Future<Integer> future = scheduledService.singleFixedDelayTask(1, "Planned
exception");
         try {
             future.get(200, TimeUnit.MILLISECONDS);
@@ -82,12 +100,19 @@ public class ManagedScheduledServiceTest {
         }
     }
 
+    /**
+     * Exception happens while processing the periodic task.
+     *
+     * @throws InterruptedException we don't expect it
+     */
     @Test
-    public void periodicFixedDelayTaskWithException() throws InterruptedException {
+    public void periodicFixedDelayTaskWithException() {
         final ScheduledFuture<?> scheduledFuture = scheduledService.periodicFixedDelayTask(1,
"Planned exception");
-        TimeUnit.MILLISECONDS.sleep(500);
 
         try {
+            TimeUnit.MILLISECONDS.sleep(500);
+            // please note that this thread will pause here until an exception is thrown.
+            // The scheduler uses a Runnable that will never return a result.
             scheduledFuture.get(200, TimeUnit.MILLISECONDS);
         } catch (ExecutionException e) {
             // the thrown RuntimeException will be wrapped around an ExecutionException
@@ -98,7 +123,7 @@ public class ManagedScheduledServiceTest {
 
         if (!scheduledFuture.isCancelled()) {
             scheduledFuture.cancel(true);
-            System.out.println("task stopped");
+            LOGGER.info("task stopped");
         }
     }
 

http://git-wip-us.apache.org/repos/asf/tomee/blob/ce01d8d9/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 30b6f6f..8ab7138 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
@@ -30,6 +30,7 @@ import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
+import java.util.logging.Logger;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
@@ -38,6 +39,8 @@ import static org.junit.Assert.fail;
 @RunWith(Arquillian.class)
 public class ManagedServiceTest {
 
+    private static final Logger LOGGER = Logger.getLogger(ManagedServiceTest.class.getName());
+
     @Inject
     private ManagedService managedService;
 
@@ -54,7 +57,7 @@ public class ManagedServiceTest {
     @Test
     public void managedInvocationTest() {
         final CompletableFuture<Integer> future = managedService.asyncTask(1);
-        System.out.println("You can do something else in the meantime and later get the future
value");
+        LOGGER.info("You can do something else in the meantime and later get the future value");
         try {
             // To prevent hanged tasks, you should obtain the value of a future with a timeout.
             assertEquals(3, future.get(200, TimeUnit.MILLISECONDS).intValue());


Mime
View raw message