// Path: java-core/src/main/java/com/sayemahmed/example/executor/MonitoredThreadPoolExecutor.java
// static final String activeThreads = "activeThreads";
// 
// Path: java-core/src/main/java/com/sayemahmed/example/executor/MonitoredThreadPoolExecutor.java
// static final String failedTasks = "failed-tasks";
// 
// Path: java-core/src/main/java/com/sayemahmed/example/executor/MonitoredThreadPoolExecutor.java
// static final String maxPoolSize = "maxPoolSize";
// 
// Path: java-core/src/main/java/com/sayemahmed/example/executor/MonitoredThreadPoolExecutor.java
// static final String queueSize = "queueSize";
// 
// Path: java-core/src/main/java/com/sayemahmed/example/executor/MonitoredThreadPoolExecutor.java
// static final String successfulTasks = "successful-tasks";
// 
// Path: java-core/src/main/java/com/sayemahmed/example/executor/MonitoredThreadPoolExecutor.java
// static final String taskExecution = "task-execution";


import com.codahale.metrics.Counter;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import static com.sayemahmed.example.executor.MonitoredThreadPoolExecutor.MonitoringKey.activeThreads;
import static com.sayemahmed.example.executor.MonitoredThreadPoolExecutor.MonitoringKey.failedTasks;
import static com.sayemahmed.example.executor.MonitoredThreadPoolExecutor.MonitoringKey.maxPoolSize;
import static com.sayemahmed.example.executor.MonitoredThreadPoolExecutor.MonitoringKey.queueSize;
import static com.sayemahmed.example.executor.MonitoredThreadPoolExecutor.MonitoringKey.successfulTasks;
import static com.sayemahmed.example.executor.MonitoredThreadPoolExecutor.MonitoringKey.taskExecution;
/*
 * Copyright 2018 MD Sayem Ahmed
 *
 * 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 com.sayemahmed.example.executor;




/**
 * @author MD Sayem Ahmed
 */
@SuppressWarnings({"WeakerAccess", "unused"})
@Slf4j
public class MonitoredThreadPoolExecutor extends ThreadPoolExecutor {
  private final MetricRegistry metricRegistry;
  private final String metricsPrefix;
  private ThreadLocal<Timer.Context> taskExecutionTimer = new ThreadLocal<>();

  public MonitoredThreadPoolExecutor(
      int corePoolSize,
      int maximumPoolSize,
      long keepAliveTime,
      TimeUnit unit,
      BlockingQueue<Runnable> workQueue,
      MetricRegistry metricRegistry,
      String poolName
  ) {
    super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    this.metricRegistry = metricRegistry;
    this.metricsPrefix = MetricRegistry.name(getClass(), poolName);
    registerGauges();
  }

  public MonitoredThreadPoolExecutor(
      int corePoolSize,
// Path: java-core/src/main/java/com/sayemahmed/example/executor/MonitoredThreadPoolExecutor.java
// static final String activeThreads = "activeThreads";
// 
// Path: java-core/src/main/java/com/sayemahmed/example/executor/MonitoredThreadPoolExecutor.java
// static final String failedTasks = "failed-tasks";
// 
// Path: java-core/src/main/java/com/sayemahmed/example/executor/MonitoredThreadPoolExecutor.java
// static final String maxPoolSize = "maxPoolSize";
// 
// Path: java-core/src/main/java/com/sayemahmed/example/executor/MonitoredThreadPoolExecutor.java
// static final String queueSize = "queueSize";
// 
// Path: java-core/src/main/java/com/sayemahmed/example/executor/MonitoredThreadPoolExecutor.java
// static final String successfulTasks = "successful-tasks";
// 
// Path: java-core/src/main/java/com/sayemahmed/example/executor/MonitoredThreadPoolExecutor.java
// static final String taskExecution = "task-execution";


// Path: java-core/src/main/java/com/sayemahmed/example/executor/MonitoredThreadPoolExecutor.java
import com.codahale.metrics.Counter;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import static com.sayemahmed.example.executor.MonitoredThreadPoolExecutor.MonitoringKey.activeThreads;
import static com.sayemahmed.example.executor.MonitoredThreadPoolExecutor.MonitoringKey.failedTasks;
import static com.sayemahmed.example.executor.MonitoredThreadPoolExecutor.MonitoringKey.maxPoolSize;
import static com.sayemahmed.example.executor.MonitoredThreadPoolExecutor.MonitoringKey.queueSize;
import static com.sayemahmed.example.executor.MonitoredThreadPoolExecutor.MonitoringKey.successfulTasks;
import static com.sayemahmed.example.executor.MonitoredThreadPoolExecutor.MonitoringKey.taskExecution;

/*
 * Copyright 2018 MD Sayem Ahmed
 *
 * 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 com.sayemahmed.example.executor;




/**
 * @author MD Sayem Ahmed
 */
@SuppressWarnings({"WeakerAccess", "unused"})
@Slf4j
public class MonitoredThreadPoolExecutor extends ThreadPoolExecutor {
  private final MetricRegistry metricRegistry;
  private final String metricsPrefix;
  private ThreadLocal<Timer.Context> taskExecutionTimer = new ThreadLocal<>();

  public MonitoredThreadPoolExecutor(
      int corePoolSize,
      int maximumPoolSize,
      long keepAliveTime,
      TimeUnit unit,
      BlockingQueue<Runnable> workQueue,
      MetricRegistry metricRegistry,
      String poolName
  ) {
    super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    this.metricRegistry = metricRegistry;
    this.metricsPrefix = MetricRegistry.name(getClass(), poolName);
    registerGauges();
  }

  public MonitoredThreadPoolExecutor(
      int corePoolSize,
      int maximumPoolSize,