[SYNCOPE-1318] Now using more flexible ThreadPoolTaskExecutor
authorFrancesco Chicchiriccò <ilgrosso@apache.org>
Tue, 29 May 2018 10:17:18 +0000 (12:17 +0200)
committerFrancesco Chicchiriccò <ilgrosso@apache.org>
Tue, 29 May 2018 10:18:56 +0000 (12:18 +0200)
client/console/src/main/java/org/apache/syncope/client/console/SyncopeConsoleSession.java
client/console/src/main/java/org/apache/syncope/client/console/commons/Constants.java
client/console/src/main/java/org/apache/syncope/client/console/pages/BasePage.java
client/console/src/main/java/org/apache/syncope/client/console/wizards/AjaxWizard.java

index faf75a4..065e035 100644 (file)
@@ -27,9 +27,7 @@ import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.Callable;
-import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
-import java.util.concurrent.ScheduledExecutorService;
 import javax.ws.rs.core.EntityTag;
 import javax.ws.rs.core.MediaType;
 import org.apache.commons.collections4.list.SetUniqueList;
@@ -37,6 +35,7 @@ import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.time.FastDateFormat;
 import org.apache.commons.lang3.tuple.Pair;
 import org.apache.cxf.jaxrs.client.WebClient;
+import org.apache.syncope.client.console.commons.Constants;
 import org.apache.syncope.client.lib.AnonymousAuthenticationHandler;
 import org.apache.syncope.client.lib.SyncopeClient;
 import org.apache.syncope.client.lib.SyncopeClientFactoryBean;
@@ -51,7 +50,7 @@ import org.apache.wicket.authroles.authorization.strategies.role.Roles;
 import org.apache.wicket.request.Request;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.scheduling.concurrent.ThreadPoolExecutorFactoryBean;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 
 public class SyncopeConsoleSession extends AuthenticatedWebSession {
 
@@ -59,18 +58,6 @@ public class SyncopeConsoleSession extends AuthenticatedWebSession {
 
     private static final Logger LOG = LoggerFactory.getLogger(SyncopeConsoleSession.class);
 
-    private static final ThreadPoolExecutorFactoryBean THREAD_POOL_FACTORY;
-
-    public static final String AUTHENTICATED = "AUTHENTICATED";
-
-    public static final String MENU_COLLAPSE = "MENU_COLLAPSE";
-
-    static {
-        THREAD_POOL_FACTORY = new ThreadPoolExecutorFactoryBean();
-        THREAD_POOL_FACTORY.setThreadNamePrefix(SyncopeConsoleSession.class.getSimpleName());
-        THREAD_POOL_FACTORY.setDaemon(true);
-    }
-
     private final SyncopeClientFactoryBean clientFactory;
 
     private final SyncopeClient anonymousClient;
@@ -79,11 +66,11 @@ public class SyncopeConsoleSession extends AuthenticatedWebSession {
 
     private final SystemInfo systemInfo;
 
-    private String domain;
-
     private final Map<Class<?>, Object> services = Collections.synchronizedMap(new HashMap<>());
 
-    private final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(5, THREAD_POOL_FACTORY);
+    private final ThreadPoolTaskExecutor executor;
+
+    private String domain;
 
     private SyncopeClient client;
 
@@ -108,6 +95,12 @@ public class SyncopeConsoleSession extends AuthenticatedWebSession {
 
         platformInfo = anonymousClient.getService(SyncopeService.class).platform();
         systemInfo = anonymousClient.getService(SyncopeService.class).system();
+
+        executor = new ThreadPoolTaskExecutor();
+        executor.setWaitForTasksToCompleteOnShutdown(false);
+        executor.setCorePoolSize(5);
+        executor.setMaxPoolSize(10);
+        executor.setQueueCapacity(50);
     }
 
     public MediaType getMediaType() {
@@ -119,11 +112,11 @@ public class SyncopeConsoleSession extends AuthenticatedWebSession {
     }
 
     public void execute(final Runnable command) {
-        executorService.execute(command);
+        executor.execute(command);
     }
 
     public <T> Future<T> execute(final Callable<T> command) {
-        return executorService.submit(command);
+        return executor.submit(command);
     }
 
     public PlatformInfo getPlatformInfo() {
@@ -199,7 +192,7 @@ public class SyncopeConsoleSession extends AuthenticatedWebSession {
             }
             cleanup();
         }
-        executorService.shutdown();
+        executor.shutdown();
         super.invalidate();
     }
 
@@ -211,7 +204,7 @@ public class SyncopeConsoleSession extends AuthenticatedWebSession {
             }
             cleanup();
         }
-        executorService.shutdownNow();
+        executor.shutdown();
         super.invalidateNow();
     }
 
@@ -252,7 +245,7 @@ public class SyncopeConsoleSession extends AuthenticatedWebSession {
     public Roles getRoles() {
         if (isSignedIn() && roles == null && auth != null) {
             roles = new Roles(auth.keySet().toArray(new String[] {}));
-            roles.add(AUTHENTICATED);
+            roles.add(Constants.ROLE_AUTHENTICATED);
         }
 
         return roles;
index 31cbda1..01ea08a 100644 (file)
@@ -29,6 +29,10 @@ public final class Constants {
 
     public static final String SYNCOPE = "syncope";
 
+    public static final String ROLE_AUTHENTICATED = "AUTHENTICATED";
+
+    public static final String MENU_COLLAPSE = "MENU_COLLAPSE";
+
     public static final String VEIL_INDICATOR_MARKUP_ID = "veil";
 
     public static final String FLOWABLE_MODELER_CONTEXT = "flowable-modeler";
index 17d6717..d79cec7 100644 (file)
@@ -93,7 +93,7 @@ public class BasePage extends WebPage implements IAjaxIndicatorAware {
         super(parameters);
 
         body = new WebMarkupContainer("body");
-        Serializable leftMenuCollapse = SyncopeConsoleSession.get().getAttribute(SyncopeConsoleSession.MENU_COLLAPSE);
+        Serializable leftMenuCollapse = SyncopeConsoleSession.get().getAttribute(Constants.MENU_COLLAPSE);
         if ((leftMenuCollapse instanceof Boolean) && ((Boolean) leftMenuCollapse)) {
             body.add(new AttributeAppender("class", " sidebar-collapse"));
         }
@@ -252,10 +252,10 @@ public class BasePage extends WebPage implements IAjaxIndicatorAware {
 
             @Override
             public void onClick(final AjaxRequestTarget target) {
-                SyncopeConsoleSession.get().setAttribute(SyncopeConsoleSession.MENU_COLLAPSE,
-                        SyncopeConsoleSession.get().getAttribute(SyncopeConsoleSession.MENU_COLLAPSE) == null
+                SyncopeConsoleSession.get().setAttribute(Constants.MENU_COLLAPSE,
+                        SyncopeConsoleSession.get().getAttribute(Constants.MENU_COLLAPSE) == null
                         ? true
-                        : !(Boolean) SyncopeConsoleSession.get().getAttribute(SyncopeConsoleSession.MENU_COLLAPSE));
+                        : !(Boolean) SyncopeConsoleSession.get().getAttribute(Constants.MENU_COLLAPSE));
             }
         });
         body.add(new Label("domain", SyncopeConsoleSession.get().getDomain()));
index a26f6b7..1f14d8f 100644 (file)
@@ -379,11 +379,11 @@ public abstract class AjaxWizard<T extends Serializable> extends Wizard
 
     private Serializable onApply(final AjaxRequestTarget target) throws TimeoutException {
         try {
-            final Future<Pair<Serializable, Serializable>> executor
-                    SyncopeConsoleSession.get().execute(new ApplyFuture(target));
+            Future<Pair<Serializable, Serializable>> executor =
+                    SyncopeConsoleSession.get().execute(new ApplyFuture(target));
 
-            final Pair<Serializable, Serializable> res
-                    executor.get(SyncopeConsoleApplication.get().getMaxWaitTimeInSeconds(), TimeUnit.SECONDS);
+            Pair<Serializable, Serializable> res =
+                    executor.get(SyncopeConsoleApplication.get().getMaxWaitTimeInSeconds(), TimeUnit.SECONDS);
 
             if (res.getLeft() != null) {
                 send(pageRef.getPage(), Broadcast.BUBBLE, res.getLeft());