You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2016/06/16 09:44:44 UTC

[03/13] isis git commit: ISIS-1437: uses a ThreadPool to initialize Wicket viewer components in parallel.

ISIS-1437: uses a ThreadPool to initialize Wicket viewer components in parallel.


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

Branch: refs/heads/ISIS-1427
Commit: 9eb35d3eac51173a9a04e3835bb67f02356f580b
Parents: 512d2d2
Author: Dan Haywood <da...@haywood-associates.co.uk>
Authored: Wed Jun 15 12:10:28 2016 +0100
Committer: Dan Haywood <da...@haywood-associates.co.uk>
Committed: Wed Jun 15 12:10:28 2016 +0100

----------------------------------------------------------------------
 .../runtime/threadpool/ThreadPoolSupport.java   | 102 +++++++++++++++++++
 .../wicket/viewer/IsisWicketApplication.java    |  39 ++++++-
 .../src/main/webapp/WEB-INF/logging.properties  |   4 +
 3 files changed, 142 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/isis/blob/9eb35d3e/core/metamodel/src/main/java/org/apache/isis/core/runtime/threadpool/ThreadPoolSupport.java
----------------------------------------------------------------------
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
new file mode 100644
index 0000000..0aee0fd
--- /dev/null
+++ b/core/metamodel/src/main/java/org/apache/isis/core/runtime/threadpool/ThreadPoolSupport.java
@@ -0,0 +1,102 @@
+/*
+ *  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
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  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.
+ */
+
+package org.apache.isis.core.runtime.threadpool;
+
+import java.util.List;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+import com.google.common.collect.Lists;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public final class ThreadPoolSupport {
+
+    private static final Logger LOG = LoggerFactory.getLogger(ThreadPoolSupport.class);
+
+    public static ThreadGroup group;
+
+    public static ThreadPoolExecutor executor   ;
+
+    static {
+        group = new ThreadGroup(ThreadPoolSupport.class.getName());
+
+        final int corePoolSize = Runtime.getRuntime().availableProcessors();
+        final int maximumPoolSize = Runtime.getRuntime().availableProcessors();
+        final int keepAliveTimeSecs = 5;
+
+        final int queueCapacity = 25;
+        final BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(queueCapacity);
+
+        executor = new ThreadPoolExecutor(
+                corePoolSize,
+                maximumPoolSize,
+                keepAliveTimeSecs, TimeUnit.SECONDS,
+                workQueue,
+                new ThreadFactory() {
+                    @Override
+                    public Thread newThread(final Runnable r) {
+                        return new Thread(group, r);
+                    }
+                });
+    }
+
+    public static List<Object> join(final List<Future<Object>> futures) {
+        if (futures == null) {
+            return null;
+        }
+
+        final long t0 = System.currentTimeMillis();
+        try{
+            final List<Object> returnValues = Lists.newArrayList();
+            for (Future<Object> future : futures) {
+                returnValues.add(join(future));
+            }
+            return returnValues;
+        } finally {
+            final long t1 = System.currentTimeMillis();
+            LOG.info("join'ing {} tasks: waited {} milliseconds ", futures.size(), (t1-t0));
+        }
+    }
+
+    public static Object join(final Future<Object> future) {
+        try {
+            return future.get();
+        } catch (InterruptedException | ExecutionException e) {
+            // ignore
+        }
+        return null;
+    }
+
+    public static List<Future<Object>> invokeAll(final List<Callable<Object>> callables) {
+        try {
+            return executor.invokeAll(callables);
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/isis/blob/9eb35d3e/core/viewer-wicket-impl/src/main/java/org/apache/isis/viewer/wicket/viewer/IsisWicketApplication.java
----------------------------------------------------------------------
diff --git a/core/viewer-wicket-impl/src/main/java/org/apache/isis/viewer/wicket/viewer/IsisWicketApplication.java b/core/viewer-wicket-impl/src/main/java/org/apache/isis/viewer/wicket/viewer/IsisWicketApplication.java
index 46ed84d..7efc13a 100644
--- a/core/viewer-wicket-impl/src/main/java/org/apache/isis/viewer/wicket/viewer/IsisWicketApplication.java
+++ b/core/viewer-wicket-impl/src/main/java/org/apache/isis/viewer/wicket/viewer/IsisWicketApplication.java
@@ -24,6 +24,8 @@ import java.util.List;
 import java.util.ServiceLoader;
 import java.util.Set;
 import java.util.UUID;
+import java.util.concurrent.Callable;
+import java.util.concurrent.Future;
 
 import com.google.common.base.Function;
 import com.google.common.collect.Lists;
@@ -71,8 +73,12 @@ import org.apache.isis.core.runtime.runner.IsisInjectModule;
 import org.apache.isis.core.runtime.system.DeploymentType;
 import org.apache.isis.core.runtime.system.context.IsisContext;
 import org.apache.isis.core.runtime.system.session.IsisSessionFactory;
+import org.apache.isis.core.runtime.threadpool.ThreadPoolSupport;
 import org.apache.isis.core.webapp.IsisWebAppBootstrapper;
 import org.apache.isis.core.webapp.WebAppConstants;
+import org.apache.isis.schema.utils.ChangesDtoUtils;
+import org.apache.isis.schema.utils.CommandDtoUtils;
+import org.apache.isis.schema.utils.InteractionDtoUtils;
 import org.apache.isis.viewer.wicket.model.isis.WicketViewerSettings;
 import org.apache.isis.viewer.wicket.model.mementos.ObjectAdapterMemento;
 import org.apache.isis.viewer.wicket.model.models.ImageResourceCache;
@@ -237,12 +243,11 @@ public class IsisWicketApplication
      */
     @Override
     protected void init() {
+        List<Future<Object>> futures = null;
         try {
             super.init();
 
-            configureWebJars();
-            configureWicketBootstrap();
-            configureWicketSelect2();
+            futures = startBackgroundInitializationThreads();
 
             String isisConfigDir = getServletContext().getInitParameter("isis.config.dir");
 
@@ -315,9 +320,37 @@ public class IsisWicketApplication
             // because Wicket's handling in its WicketFilter (that calls this method) does not log the exception.
             LOG.error("Failed to initialize", ex);
             throw ex;
+        } finally {
+            ThreadPoolSupport.join(futures);
         }
     }
 
+    protected List<Future<Object>> startBackgroundInitializationThreads() {
+        return ThreadPoolSupport.invokeAll(Lists.newArrayList(
+                new Callable<Object>() {
+                    @Override
+                    public Object call() throws Exception {
+                        configureWebJars();
+                        return null;
+                    }
+                },
+                new Callable<Object>() {
+                    @Override
+                    public Object call() throws Exception {
+                        configureWicketBootstrap();
+                        return null;
+                    }
+                },
+                new Callable<Object>() {
+                    @Override
+                    public Object call() throws Exception {
+                        configureWicketSelect2();
+                        return null;
+                    }
+                }
+        ));
+    }
+
     /**
      * protected visibility to allow ad-hoc overriding of some other authentication strategy.
      */

http://git-wip-us.apache.org/repos/asf/isis/blob/9eb35d3e/example/application/simpleapp/webapp/src/main/webapp/WEB-INF/logging.properties
----------------------------------------------------------------------
diff --git a/example/application/simpleapp/webapp/src/main/webapp/WEB-INF/logging.properties b/example/application/simpleapp/webapp/src/main/webapp/WEB-INF/logging.properties
index ca7a641..7cbfcc6 100644
--- a/example/application/simpleapp/webapp/src/main/webapp/WEB-INF/logging.properties
+++ b/example/application/simpleapp/webapp/src/main/webapp/WEB-INF/logging.properties
@@ -207,6 +207,10 @@ log4j.additivity.org.apache.isis.core.commons.config.IsisConfigurationDefault=fa
 log4j.logger.org.apache.isis.core.runtime.installers.InstallerLookupDefault=WARN,Console
 log4j.additivity.org.apache.isis.core.runtime.installers.InstallerLookupDefault=false
 
+log4j.logger.org.apache.isis.core.runtime.threadpool.ThreadPoolSupport=INFO,Console
+log4j.additivity.org.apache.isis.core.runtime.threadpool.ThreadPoolSupport=false
+
+
 
 # quieten Shiro
 log4j.logger.org.apache.shiro.realm.AuthorizingRealm=WARN,Console