[UI] Support UI partial control
[validation.git] / ui / src / main / java / org / akraino / validation / ui / conf / ExecutorServiceInitializer.java
1 /*
2  * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may
5  * not use this file except in compliance with the License. You may obtain
6  * a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13  * implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 package org.akraino.validation.ui.conf;
17
18 import java.lang.reflect.Field;
19 import java.util.Comparator;
20 import java.util.concurrent.ExecutorService;
21 import java.util.concurrent.PriorityBlockingQueue;
22 import java.util.concurrent.ThreadPoolExecutor;
23 import java.util.concurrent.TimeUnit;
24
25 import org.springframework.context.annotation.Bean;
26 import org.springframework.context.annotation.Configuration;
27
28 @Configuration
29 public class ExecutorServiceInitializer {
30
31     private static final int QUEUE_CAPACITY = 500;
32     private static final int EXECUTOR_SIZE = 30; // the number of threads to keep in the pool, even if
33     // they are idle, unless allowCoreThreadTimeOut is
34     // set
35     private static final int EXECUTOR_MAX_SIZE = 30; // the maximum number of threads to allow in the pool
36     private static final int KEEPALIVE_TIME = 30; // when the number of threads is greater than the
37     // core, this is the maximum time that excess idle
38     // threads will wait for new tasks before
39     // terminating.
40     private static final PriorityBlockingQueue<Runnable> BLOCKING_QUEUE = new PriorityBlockingQueue<Runnable>(
41             QUEUE_CAPACITY, new CFRunnableComparator());
42     private static ExecutorService executorService = new ThreadPoolExecutor(EXECUTOR_SIZE, EXECUTOR_MAX_SIZE,
43             KEEPALIVE_TIME, TimeUnit.SECONDS, BLOCKING_QUEUE);
44
45     @Bean(name = "executorService")
46     public ExecutorService getExecutorService() {
47         return this.executorService;
48     }
49
50     private static class CFRunnableComparator implements Comparator<Runnable> {
51         @Override
52         @SuppressWarnings("unchecked")
53         public int compare(Runnable runnable1, Runnable runnable2) {
54             return ((Comparable) unwrap(runnable1)).compareTo(unwrap(runnable2));
55         }
56
57         private Object unwrap(Runnable runnable) {
58             try {
59                 Field field = runnable.getClass().getDeclaredField("fn");
60                 field.setAccessible(true);
61                 return field.get(runnable);
62             } catch (IllegalAccessException | NoSuchFieldException e) {
63                 throw new IllegalArgumentException("Couldn't unwrap " + runnable, e);
64             }
65         }
66     }
67
68 }