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 mBeanName Used as the <code>name</code> property in the bean's ObjectName.
38 * @param mBeanType Used as the <code>type</code> property in the bean's ObjectName.
39 * @param mBeanCategory Used as the <code>Category</code> property in the bean's ObjectName.
41 public ThreadExecutorStatsMXBeanImpl(final ThreadPoolExecutor executor, final String mBeanName,
42 final String mBeanType, @Nullable final String mBeanCategory) {
43 super(mBeanName, mBeanType, mBeanCategory);
44 this.executor = Preconditions.checkNotNull(executor);
47 private static ThreadExecutorStatsMXBeanImpl createInternal(final Executor executor,
48 final String mBeanName, final String mBeanType, final String mBeanCategory) {
49 if (executor instanceof ThreadPoolExecutor) {
50 final ThreadExecutorStatsMXBeanImpl ret = new ThreadExecutorStatsMXBeanImpl(
51 (ThreadPoolExecutor) executor, mBeanName, mBeanType, mBeanCategory);
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 mBeanName Used as the <code>name</code> property in the bean's ObjectName.
64 * @param mBeanType Used as the <code>type</code> property in the bean's ObjectName.
65 * @param mBeanCategory 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 mBeanName,
70 final String mBeanType, @Nullable final String mBeanCategory) {
71 ThreadExecutorStatsMXBeanImpl ret = createInternal(executor, mBeanName, mBeanType, mBeanCategory);
80 * Creates a new bean if the backing executor is a ThreadPoolExecutor.
82 * @param executor the backing {@link Executor}
83 * @return a ThreadExecutorStatsMXBeanImpl instance if the backing executor
84 * is a ThreadPoolExecutor, otherwise null.
86 public static ThreadExecutorStatsMXBeanImpl create(final Executor executor) {
87 return createInternal(executor, "", "", null);
91 public long getCurrentThreadPoolSize() {
92 return executor.getPoolSize();
96 public long getLargestThreadPoolSize() {
97 return executor.getLargestPoolSize();
101 public long getMaxThreadPoolSize() {
102 return executor.getMaximumPoolSize();
106 public long getCurrentQueueSize() {
107 return executor.getQueue().size();
111 public Long getLargestQueueSize() {
112 BlockingQueue<Runnable> queue = executor.getQueue();
113 if(queue instanceof TrackingLinkedBlockingQueue) {
114 return Long.valueOf(((TrackingLinkedBlockingQueue<?>)queue).getLargestQueueSize());
121 public long getMaxQueueSize() {
122 long queueSize = executor.getQueue().size();
123 return executor.getQueue().remainingCapacity() + queueSize;
127 public long getActiveThreadCount() {
128 return executor.getActiveCount();
132 public long getCompletedTaskCount() {
133 return executor.getCompletedTaskCount();
137 public long getTotalTaskCount() {
138 return executor.getTaskCount();
142 public Long getRejectedTaskCount() {
143 RejectedExecutionHandler rejectedHandler = executor.getRejectedExecutionHandler();
144 if(rejectedHandler instanceof CountingRejectedExecutionHandler) {
145 return Long.valueOf(((CountingRejectedExecutionHandler)rejectedHandler)
146 .getRejectedTaskCount());
153 * Returns a {@link ThreadExecutorStats} instance containing a snapshot of the statistic
156 public ThreadExecutorStats toThreadExecutorStats() {
157 return new ThreadExecutorStats(getActiveThreadCount(), getCurrentThreadPoolSize(),
158 getLargestThreadPoolSize(), getMaxThreadPoolSize(), getCurrentQueueSize(),
159 getLargestQueueSize(), getMaxQueueSize(), getCompletedTaskCount(),
160 getTotalTaskCount(), getRejectedTaskCount());