2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.md.sal.common.util.jmx;
11 import com.google.common.base.Preconditions;
12 import java.util.concurrent.BlockingQueue;
13 import java.util.concurrent.Executor;
14 import java.util.concurrent.RejectedExecutionHandler;
15 import java.util.concurrent.ThreadPoolExecutor;
16 import javax.annotation.Nullable;
17 import org.opendaylight.yangtools.util.concurrent.CountingRejectedExecutionHandler;
18 import org.opendaylight.yangtools.util.concurrent.TrackingLinkedBlockingQueue;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * MXBean implementation of the ThreadExecutorStatsMXBean interface that retrieves statistics
24 * from a backing {@link java.util.concurrent.ExecutorService}.
26 * @author Thomas Pantelis
28 public class ThreadExecutorStatsMXBeanImpl extends AbstractMXBean
29 implements ThreadExecutorStatsMXBean {
30 private static final Logger LOG = LoggerFactory.getLogger(ThreadExecutorStatsMXBeanImpl.class);
31 private final ThreadPoolExecutor executor;
34 * Constructs an instance for the given {@link Executor}.
36 * @param executor the backing {@link Executor}
37 * @param beanName Used as the <code>name</code> property in the bean's ObjectName.
38 * @param beanType Used as the <code>type</code> property in the bean's ObjectName.
39 * @param beanCategory Used as the <code>Category</code> property in the bean's ObjectName.
41 public ThreadExecutorStatsMXBeanImpl(final ThreadPoolExecutor executor, final String beanName,
42 final String beanType, @Nullable final String beanCategory) {
43 super(beanName, beanType, beanCategory);
44 this.executor = Preconditions.checkNotNull(executor);
47 private static ThreadExecutorStatsMXBeanImpl createInternal(final Executor executor,
48 final String beanName, final String beanType, final String beanCategory) {
49 if (executor instanceof ThreadPoolExecutor) {
50 final ThreadExecutorStatsMXBeanImpl ret = new ThreadExecutorStatsMXBeanImpl(
51 (ThreadPoolExecutor) executor, beanName, beanType, beanCategory);
55 LOG.info("Executor {} is not supported", executor);
60 * Creates a new bean if the backing executor is a ThreadPoolExecutor and registers it.
62 * @param executor the backing {@link Executor}
63 * @param beanName Used as the <code>name</code> property in the bean's ObjectName.
64 * @param beanType Used as the <code>type</code> property in the bean's ObjectName.
65 * @param beanCategory Used as the <code>Category</code> property in the bean's ObjectName.
66 * @return a registered ThreadExecutorStatsMXBeanImpl instance if the backing executor
67 * is a ThreadPoolExecutor, otherwise null.
69 public static ThreadExecutorStatsMXBeanImpl create(final Executor executor, final String beanName,
70 final String beanType, @Nullable final String beanCategory) {
71 ThreadExecutorStatsMXBeanImpl ret = createInternal(executor, beanName, beanType, beanCategory);
80 * Creates a new bean if the backing executor is a ThreadPoolExecutor and registers it.
82 * @param executor the backing {@link Executor}
83 * @param beanName Used as the <code>name</code> property in the bean's ObjectName.
84 * @param beanType Used as the <code>type</code> property in the bean's ObjectName.
85 * @return a registered ThreadExecutorStatsMXBeanImpl instance if the backing executor
86 * is a ThreadPoolExecutor, otherwise null.
88 public static ThreadExecutorStatsMXBeanImpl create(final Executor executor, final String beanName,
89 final String beanType) {
90 return create(executor, beanName, beanType, null);
94 * Creates a new bean if the backing executor is a ThreadPoolExecutor.
96 * @param executor the backing {@link Executor}
97 * @return a ThreadExecutorStatsMXBeanImpl instance if the backing executor
98 * is a ThreadPoolExecutor, otherwise null.
100 public static ThreadExecutorStatsMXBeanImpl create(final Executor executor) {
101 return createInternal(executor, "", "", null);
105 public long getCurrentThreadPoolSize() {
106 return executor.getPoolSize();
110 public long getLargestThreadPoolSize() {
111 return executor.getLargestPoolSize();
115 public long getMaxThreadPoolSize() {
116 return executor.getMaximumPoolSize();
120 public long getCurrentQueueSize() {
121 return executor.getQueue().size();
125 public Long getLargestQueueSize() {
126 BlockingQueue<Runnable> queue = executor.getQueue();
127 if (queue instanceof TrackingLinkedBlockingQueue) {
128 return Long.valueOf(((TrackingLinkedBlockingQueue<?>)queue).getLargestQueueSize());
135 public long getMaxQueueSize() {
136 long queueSize = executor.getQueue().size();
137 return executor.getQueue().remainingCapacity() + queueSize;
141 public long getActiveThreadCount() {
142 return executor.getActiveCount();
146 public long getCompletedTaskCount() {
147 return executor.getCompletedTaskCount();
151 public long getTotalTaskCount() {
152 return executor.getTaskCount();
156 public Long getRejectedTaskCount() {
157 RejectedExecutionHandler rejectedHandler = executor.getRejectedExecutionHandler();
158 if (rejectedHandler instanceof CountingRejectedExecutionHandler) {
159 return Long.valueOf(((CountingRejectedExecutionHandler)rejectedHandler)
160 .getRejectedTaskCount());
167 * Returns a {@link ThreadExecutorStats} instance containing a snapshot of the statistic
170 public ThreadExecutorStats toThreadExecutorStats() {
171 return new ThreadExecutorStats(getActiveThreadCount(), getCurrentThreadPoolSize(),
172 getLargestThreadPoolSize(), getMaxThreadPoolSize(), getCurrentQueueSize(),
173 getLargestQueueSize(), getMaxQueueSize(), getCompletedTaskCount(),
174 getTotalTaskCount(), getRejectedTaskCount());