View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  package com.addthis.codec.utils;
15  
16  import java.util.concurrent.BlockingQueue;
17  import java.util.concurrent.ExecutorService;
18  import java.util.concurrent.LinkedBlockingQueue;
19  import java.util.concurrent.ThreadFactory;
20  import java.util.concurrent.ThreadPoolExecutor;
21  import java.util.concurrent.TimeUnit;
22  
23  import com.addthis.codec.annotations.Time;
24  
25  import com.google.common.annotations.Beta;
26  import com.google.common.util.concurrent.MoreExecutors;
27  
28  import com.fasterxml.jackson.annotation.JsonCreator;
29  import com.fasterxml.jackson.annotation.JsonProperty;
30  import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
31  import com.yammer.metrics.Metrics;
32  
33  @Beta
34  @JsonDeserialize(builder = ExecutorServiceBuilder.class)
35  public class ExecutorServiceBuilder {
36  
37      private final ThreadFactory threadFactory;
38      private final int coreThreads;
39      private final int maxThreads;
40      private final int keepAlive;
41      private final BlockingQueue<Runnable> queue;
42      private final boolean shutdownHook;
43  
44      @JsonCreator
45      public ExecutorServiceBuilder(@JsonProperty("thread-factory") ThreadFactory threadFactory,
46                                    @JsonProperty("core-threads") int coreThreads,
47                                    @JsonProperty("max-threads") int maxThreads,
48                                    @JsonProperty("keep-alive") @Time(TimeUnit.MILLISECONDS) int keepAlive,
49                                    @JsonProperty("queue-size") int queueSize,
50                                    @JsonProperty("queue-gauge-class") Class<?> gaugeClass,
51                                    @JsonProperty("queue-gauge-name") String gaugeName,
52                                    @JsonProperty("shutdown-hook") boolean shutdownHook) {
53          this.threadFactory = threadFactory;
54          this.coreThreads = coreThreads;
55          this.maxThreads = maxThreads;
56          this.keepAlive = keepAlive;
57          this.queue = new LinkedBlockingQueue<>(queueSize);
58          if ((gaugeClass != null) && (gaugeName != null)) {
59              Metrics.newGauge(gaugeClass, gaugeName, new SizeGauge(queue));
60          }
61          this.shutdownHook = shutdownHook;
62      }
63  
64      public ExecutorService build() {
65          ThreadPoolExecutor service = new ThreadPoolExecutor(coreThreads, maxThreads,
66                                                              keepAlive, TimeUnit.MILLISECONDS,
67                                                              queue, threadFactory);
68          if (shutdownHook) {
69              return MoreExecutors.getExitingExecutorService(service);
70          } else {
71              return service;
72          }
73      }
74  }