X-Git-Url: https://gerrit.akraino.org/r/gitweb?a=blobdiff_plain;f=ui%2Fsrc%2Fmain%2Fjava%2Forg%2Fakraino%2Fvalidation%2Fui%2Fconf%2FExecutorServiceInitializer.java;fp=ui%2Fsrc%2Fmain%2Fjava%2Forg%2Fakraino%2Fvalidation%2Fui%2Fconf%2FExecutorServiceInitializer.java;h=bfe4fc95339c88af33861c11261e5ab4211fea45;hb=2eba847ebb6acb2686be08eb1cdafc1b12071e7d;hp=0000000000000000000000000000000000000000;hpb=f86b9715d156238532fcb0bf464bd72e9cf7ce96;p=validation.git diff --git a/ui/src/main/java/org/akraino/validation/ui/conf/ExecutorServiceInitializer.java b/ui/src/main/java/org/akraino/validation/ui/conf/ExecutorServiceInitializer.java new file mode 100644 index 0000000..bfe4fc9 --- /dev/null +++ b/ui/src/main/java/org/akraino/validation/ui/conf/ExecutorServiceInitializer.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved. + * + * Licensed 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.akraino.validation.ui.conf; + +import java.lang.reflect.Field; +import java.util.Comparator; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.PriorityBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ExecutorServiceInitializer { + + private static final int QUEUE_CAPACITY = 500; + private static final int EXECUTOR_SIZE = 30; // the number of threads to keep in the pool, even if + // they are idle, unless allowCoreThreadTimeOut is + // set + private static final int EXECUTOR_MAX_SIZE = 30; // the maximum number of threads to allow in the pool + private static final int KEEPALIVE_TIME = 30; // when the number of threads is greater than the + // core, this is the maximum time that excess idle + // threads will wait for new tasks before + // terminating. + private static final PriorityBlockingQueue BLOCKING_QUEUE = new PriorityBlockingQueue( + QUEUE_CAPACITY, new CFRunnableComparator()); + private static ExecutorService executorService = new ThreadPoolExecutor(EXECUTOR_SIZE, EXECUTOR_MAX_SIZE, + KEEPALIVE_TIME, TimeUnit.SECONDS, BLOCKING_QUEUE); + + @Bean(name = "executorService") + public ExecutorService getExecutorService() { + return this.executorService; + } + + private static class CFRunnableComparator implements Comparator { + @Override + @SuppressWarnings("unchecked") + public int compare(Runnable runnable1, Runnable runnable2) { + return ((Comparable) unwrap(runnable1)).compareTo(unwrap(runnable2)); + } + + private Object unwrap(Runnable runnable) { + try { + Field field = runnable.getClass().getDeclaredField("fn"); + field.setAccessible(true); + return field.get(runnable); + } catch (IllegalAccessException | NoSuchFieldException e) { + throw new IllegalArgumentException("Couldn't unwrap " + runnable, e); + } + } + } + +}