You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2018/10/29 05:23:52 UTC

[isis] branch v2 updated: ISIS-1811: ThreadPoolSupport: adds access to the underlying executor

This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch v2
in repository https://gitbox.apache.org/repos/asf/isis.git


The following commit(s) were added to refs/heads/v2 by this push:
     new 292ca19  ISIS-1811: ThreadPoolSupport: adds access to the underlying executor
292ca19 is described below

commit 292ca197468fabaf928354acfd8de706264ab99d
Author: Andi Huber <ah...@apache.org>
AuthorDate: Mon Oct 29 05:07:31 2018 +0100

    ISIS-1811: ThreadPoolSupport: adds access to the underlying executor
    
    also allows CompletableFuture creation w/ or w/o IsisSession
---
 .../core/runtime/threadpool/ThreadPoolSupport.java | 47 +++++++++++++++++-----
 .../core/runtime/system/context/IsisContext.java   | 41 +++++++++++++++----
 2 files changed, 72 insertions(+), 16 deletions(-)

diff --git a/core/metamodel/src/main/java/org/apache/isis/core/runtime/threadpool/ThreadPoolSupport.java b/core/metamodel/src/main/java/org/apache/isis/core/runtime/threadpool/ThreadPoolSupport.java
index e6d30c7..35be70b 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/runtime/threadpool/ThreadPoolSupport.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/runtime/threadpool/ThreadPoolSupport.java
@@ -19,18 +19,15 @@
 
 package org.apache.isis.core.runtime.threadpool;
 
-import static java.util.Collections.emptyList;
-import static java.util.Collections.singletonList;
-import static java.util.stream.Collectors.toList;
-import static org.apache.isis.commons.internal.base._Casts.uncheckedCast;
-
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.Callable;
+import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Executor;
 import java.util.concurrent.Future;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.ThreadFactory;
@@ -49,6 +46,12 @@ import org.apache.isis.commons.internal.base._NullSafe;
 import org.apache.isis.commons.internal.collections._Lists;
 import org.apache.isis.commons.internal.context._Context;
 
+import static java.util.Collections.emptyList;
+import static java.util.Collections.singletonList;
+import static java.util.stream.Collectors.toList;
+import static org.apache.isis.commons.internal.base._Casts.uncheckedCast;
+import static org.apache.isis.commons.internal.base._With.requires;
+
 /**
  * ThreadPoolSupport is application-scoped, meaning ThreadPoolSupport is closed on
  * application's end of life-cycle.
@@ -102,6 +105,27 @@ public final class ThreadPoolSupport implements AutoCloseable {
     public void close() throws Exception {
         concurrentExecutor.shutdown();
     }
+    
+    /**
+     * @return this thread-pool's underlying concurrent executor
+     */
+    public Executor getExecutor() {
+        return concurrentExecutor;
+    }
+    
+    /**
+     * Non-blocking call. 
+     * <p>
+     * If the computation requires an IsisSession use {@code IsisContext.newCompletableFuture(Supplier)} instead.
+     * 
+     * @param computation - async task 
+     * @return new CompletableFuture utilizing this thread-pool's underlying concurrent executor
+     */
+    public <T> CompletableFuture<T> newCompletableFuture(Supplier<T> computation) {
+        requires(computation, "computation");
+        return CompletableFuture.supplyAsync(computation, getExecutor());
+    }
+    
 
     /**
      * Executes specified {@code callables} on the default executor.  
@@ -155,7 +179,7 @@ public final class ThreadPoolSupport implements AutoCloseable {
 
 
     /**
-     * Waits if necessary for the computation to complete. (Suppresses checked exceptions.)
+     * Waits if necessary for the computation to complete. Suppresses checked exceptions.
      * @param futures
      * @return list of computation results.
      */
@@ -177,12 +201,17 @@ public final class ThreadPoolSupport implements AutoCloseable {
         }
     }
 
+    /**
+     * Waits if necessary for the computation to complete. Re-throws any checked exception as RuntimeException.
+     * @param futures
+     * @return list of computation results.
+     */
     public List<Object> joinGatherFailures(final List<Future<Object>> futures) {
         if (futures == null) {
             return null;
         }
 
-        final long t0 = System.currentTimeMillis();
+        final long t0 = System.nanoTime();
         try{
             final List<Object> returnValues = _Lists.newArrayList();
             for (Future<Object> future : futures) {
@@ -196,8 +225,8 @@ public final class ThreadPoolSupport implements AutoCloseable {
             }
             return returnValues;
         } finally {
-            final long t1 = System.currentTimeMillis();
-            LOG.info("join'ing {} tasks: waited {} milliseconds ", futures.size(), (t1-t0));
+            final long t1 = System.nanoTime();
+            LOG.info("join'ing {} tasks: waited {} milliseconds ", futures.size(), 0.000_001 * (t1-t0));
         }
     }
 
diff --git a/core/runtime/src/main/java/org/apache/isis/core/runtime/system/context/IsisContext.java b/core/runtime/src/main/java/org/apache/isis/core/runtime/system/context/IsisContext.java
index fb45ee3..ed26bf8 100644
--- a/core/runtime/src/main/java/org/apache/isis/core/runtime/system/context/IsisContext.java
+++ b/core/runtime/src/main/java/org/apache/isis/core/runtime/system/context/IsisContext.java
@@ -22,6 +22,8 @@ package org.apache.isis.core.runtime.system.context;
 import java.util.Map;
 import java.util.Optional;
 import java.util.TreeMap;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Supplier;
 
 import org.apache.isis.commons.internal.base._Strings;
 import org.apache.isis.commons.internal.context._Context;
@@ -33,6 +35,7 @@ import org.apache.isis.core.runtime.system.DeploymentType;
 import org.apache.isis.core.runtime.system.persistence.PersistenceSession;
 import org.apache.isis.core.runtime.system.session.IsisSession;
 import org.apache.isis.core.runtime.system.session.IsisSessionFactory;
+import org.apache.isis.core.runtime.threadpool.ThreadPoolSupport;
 
 /**
  * Provides static access to current context's singletons
@@ -68,6 +71,25 @@ public interface IsisContext {
     public static ClassLoader getClassLoader() {
         return _Context.getDefaultClassLoader();
     }
+    
+    /**
+     * Non-blocking call.
+     * <p>
+     * Utilizes the framework's thread pool by submitting the specified {@code computation} to run
+     * in background. The {@code computation} is running in the context of a new {@link IsisSession}.
+     * <p>
+     * If the computation requires no IsisSession use 
+     * {@link ThreadPoolSupport#newCompletableFuture(Supplier)} instead.
+     * 
+     * @param computation
+     * @return
+     */
+    public static <T> CompletableFuture<T> newCompletableFuture(Supplier<T> computation){
+        final Supplier<T> computationWithSession = ()->
+            IsisContext.getSessionFactory().doInSession(computation::get);
+        
+        return ThreadPoolSupport.getInstance().newCompletableFuture(computationWithSession);
+    }
 
     /**
      * @return pre-bootstrapping configuration
@@ -80,6 +102,7 @@ public interface IsisContext {
      * For integration testing allows to prime the environment via provided configuration. Will not override
      * any IsisSystemEnvironment instance, that is already registered with the current context, because the 
      * IsisSystemEnvironment is expected to be an immutable singleton within an application's life-cycle.
+     * @deprecated currently under investigation on user mailing list
      */
     public static void primeEnvironment(IsisConfiguration conf) {
         _Context.computeIfAbsent(IsisSystemEnvironment.class, __->IsisSystemEnvironment.of(conf));
@@ -134,14 +157,16 @@ public interface IsisContext {
         return "2.0.0-M2";
     }
     
-    public static void dumpConfig() {
+    public static StringBuilder dumpConfig() {
+        
+        final StringBuilder sb = new StringBuilder();
 
         final IsisConfiguration configuration;
         try {
             configuration = getConfiguration();
         } catch (Exception e) {
             // ignore
-            return;
+            return sb;
         }
 
         final Map<String, String> map = new TreeMap<>(configuration.asMap());
@@ -152,13 +177,15 @@ public interface IsisContext {
         final int fillRight = fillCount-fillLeft;
         head = _Strings.padStart("", fillLeft, ' ') + head + _Strings.padEnd("", fillRight, ' ');
         
-        System.out.println("================================================");
-        System.out.println("="+head+"=");
-        System.out.println("================================================");
+        sb.append("================================================\n");
+        sb.append("="+head+"=\n");
+        sb.append("================================================\n");
         map.forEach((k,v)->{
-            System.out.println(k+" -> "+v);
+            sb.append(k+" -> "+v).append("\n");
         });
-        System.out.println("================================================");
+        sb.append("================================================\n");
+        
+        return sb;
     }