001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019package org.apache.hadoop.metrics2.lib; 020 021import java.util.concurrent.ScheduledExecutorService; 022import java.util.concurrent.ScheduledThreadPoolExecutor; 023import java.util.concurrent.ThreadFactory; 024import java.util.concurrent.atomic.AtomicInteger; 025 026import org.apache.hadoop.metrics2.MetricsExecutor; 027import org.apache.yetus.audience.InterfaceAudience; 028 029/** 030 * Class to handle the ScheduledExecutorService{@link ScheduledExecutorService} used by 031 * MetricsRegionAggregateSourceImpl, and 032 * JmxCacheBuster 033 */ 034@InterfaceAudience.Private 035public class MetricsExecutorImpl implements MetricsExecutor { 036 037 @Override 038 public ScheduledExecutorService getExecutor() { 039 return ExecutorSingleton.INSTANCE.scheduler; 040 } 041 042 @Override 043 public void stop() { 044 if (!getExecutor().isShutdown()) { 045 getExecutor().shutdown(); 046 } 047 } 048 049 private enum ExecutorSingleton { 050 INSTANCE; 051 private final transient ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(1, 052 new ThreadPoolExecutorThreadFactory("HBase-Metrics2-")); 053 } 054 055 private final static class ThreadPoolExecutorThreadFactory implements ThreadFactory { 056 private final String name; 057 private final AtomicInteger threadNumber = new AtomicInteger(1); 058 059 private ThreadPoolExecutorThreadFactory(String name) { 060 this.name = name; 061 } 062 063 @Override 064 public Thread newThread(Runnable runnable) { 065 Thread t = new Thread(runnable, name + threadNumber.getAndIncrement()); 066 t.setDaemon(true); 067 return t; 068 } 069 } 070}