View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.thrift;
20  
21  import java.util.concurrent.BlockingQueue;
22  import java.util.concurrent.ThreadFactory;
23  import java.util.concurrent.ThreadPoolExecutor;
24  import java.util.concurrent.TimeUnit;
25  
26  /**
27   * A ThreadPoolExecutor customized for working with HBase thrift to update metrics before and
28   * after the execution of a task.
29   */
30  
31  public class THBaseThreadPoolExecutor extends ThreadPoolExecutor {
32  
33    private ThriftMetrics metrics;
34  
35    public THBaseThreadPoolExecutor(int corePoolSize, int maxPoolSize, long keepAliveTime,
36        TimeUnit unit, BlockingQueue<Runnable> workQueue, ThriftMetrics metrics) {
37      this(corePoolSize, maxPoolSize, keepAliveTime, unit, workQueue, null, metrics);
38    }
39  
40    public THBaseThreadPoolExecutor(int corePoolSize, int maxPoolSize, long keepAliveTime,
41        TimeUnit unit, BlockingQueue<Runnable> workQueue,
42        ThreadFactory threadFactory,ThriftMetrics metrics) {
43      super(corePoolSize, maxPoolSize, keepAliveTime, unit, workQueue);
44      if (threadFactory != null) {
45        setThreadFactory(threadFactory);
46      }
47      this.metrics = metrics;
48    }
49  
50    @Override
51    protected void beforeExecute(Thread t, Runnable r) {
52      super.beforeExecute(t, r);
53      metrics.incActiveWorkerCount();
54    }
55  
56    @Override
57    protected void afterExecute(Runnable r, Throwable t) {
58      metrics.decActiveWorkerCount();
59      super.afterExecute(r, t);
60    }
61  }