TOMEE-2301 - Add Thread factory example and fix some typos

Signed-off-by: brunobat <[email protected]>


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

Branch: refs/heads/master
Commit: e96315bbf5b2ccb40866e0155132129b229306ca
Parents: ce01d8d
Author: brunobat <[email protected]>
Authored: Wed Dec 5 15:25:54 2018 +0000
Committer: brunobat <[email protected]>
Committed: Wed Dec 5 15:27:04 2018 +0000

----------------------------------------------------------------------
 .../executor/ManagedScheduledService.java       |  2 +-
 .../org/superbiz/executor/ManagedService.java   |  4 +-
 .../superbiz/executor/ThreadFactoryService.java | 71 ++++++++++++++++++++
 3 files changed, 74 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/e96315bb/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 e5c1e75..c0f2bdb 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
@@ -71,7 +71,7 @@ public class ManagedScheduledService {
      * Will simulate a long running operation
      *
      * @param value          The value to compute
-     * @param taskDurationMs the time lenght of the operation
+     * @param taskDurationMs the time length of the operation
      * @param errorMessage   If not null an exception with be thrown with this 
message
      * @return a {@link Runnable}
      */

http://git-wip-us.apache.org/repos/asf/tomee/blob/e96315bb/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 a8aa2cb..dabcf68 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
@@ -37,7 +37,7 @@ public class ManagedService {
     private ManagedExecutorService executor;
 
     /**
-     * Executes an opperation asynchronously, in a different thread provided 
by the {@link ManagedExecutorService}.
+     * Executes an operation asynchronously, in a different thread provided by 
the {@link ManagedExecutorService}.
      * The computation will carry on after the return of the method.
      *
      * @param value The demo data.
@@ -51,7 +51,7 @@ public class ManagedService {
     }
 
     /**
-     * Executes an opperation asynchronously, in a different thread provided 
by the {@link ManagedExecutorService}.
+     * Executes an operation asynchronously, in a different thread provided by 
the {@link ManagedExecutorService}.
      * The computation will carry on after the return of the method.
      *
      * @param value The demo data.

http://git-wip-us.apache.org/repos/asf/tomee/blob/e96315bb/examples/concurrency-utils/src/main/java/org/superbiz/executor/ThreadFactoryService.java
----------------------------------------------------------------------
diff --git 
a/examples/concurrency-utils/src/main/java/org/superbiz/executor/ThreadFactoryService.java
 
b/examples/concurrency-utils/src/main/java/org/superbiz/executor/ThreadFactoryService.java
new file mode 100644
index 0000000..17086e5
--- /dev/null
+++ 
b/examples/concurrency-utils/src/main/java/org/superbiz/executor/ThreadFactoryService.java
@@ -0,0 +1,71 @@
+package org.superbiz.executor;
+
+import javax.annotation.Resource;
+import javax.enterprise.concurrent.ManagedThreadFactory;
+import javax.enterprise.context.RequestScoped;
+import java.util.concurrent.TimeUnit;
+import java.util.logging.Logger;
+
+import static java.util.Objects.nonNull;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+@RequestScoped
+public class ThreadFactoryService {
+
+    private static final Logger LOGGER = 
Logger.getLogger(ThreadFactoryService.class.getSimpleName());
+
+    @Resource
+    private ManagedThreadFactory factory;
+
+    public void asyncTask(final int value) {
+        LOGGER.info("Create asyncTask");
+        final Thread thread = factory.newThread(longRunnableTask(value, 100, 
null));
+        thread.setName("pretty asyncTask");
+        thread.start();
+    }
+
+    /**
+     * Will simulate a long running operation
+     *
+     * @param value          The value to compute
+     * @param taskDurationMs the time length 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)) {
+                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;
+            LOGGER.info("longRunnableTask complete. Value is " + result);
+            // Cannot return result with a Runnable.
+        };
+    }
+
+}

Reply via email to