Bug 1875 - Used variables for nexusproxy host, externalized versions
[controller.git] / opendaylight / md-sal / sal-common-util / src / main / java / org / opendaylight / controller / md / sal / common / util / jmx / ThreadExecutorStatsMXBeanImpl.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.md.sal.common.util.jmx;
10
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
20 /**
21  * MXBean implementation of the ThreadExecutorStatsMXBean interface that retrieves statistics
22  * from a backing {@link java.util.concurrent.ExecutorService}.
23  *
24  * @author Thomas Pantelis
25  */
26 public class ThreadExecutorStatsMXBeanImpl extends AbstractMXBean
27                                            implements ThreadExecutorStatsMXBean {
28
29     private final ThreadPoolExecutor executor;
30
31     /**
32      * Constructs an instance for the given {@link Executor}.
33      *
34      * @param executor the backing {@link Executor}
35      * @param mBeanName Used as the <code>name</code> property in the bean's ObjectName.
36      * @param mBeanType Used as the <code>type</code> property in the bean's ObjectName.
37      * @param mBeanCategory Used as the <code>Category</code> property in the bean's ObjectName.
38      */
39     public ThreadExecutorStatsMXBeanImpl(Executor executor, String mBeanName,
40             String mBeanType, @Nullable String mBeanCategory) {
41         super(mBeanName, mBeanType, mBeanCategory);
42
43         Preconditions.checkArgument(executor instanceof ThreadPoolExecutor,
44                 "The ExecutorService of type {} is not an instanceof ThreadPoolExecutor",
45                 executor.getClass());
46         this.executor = (ThreadPoolExecutor)executor;
47     }
48
49     @Override
50     public long getCurrentThreadPoolSize() {
51         return executor.getPoolSize();
52     }
53
54     @Override
55     public long getLargestThreadPoolSize() {
56         return  executor.getLargestPoolSize();
57     }
58
59     @Override
60     public long getMaxThreadPoolSize() {
61         return executor.getMaximumPoolSize();
62     }
63
64     @Override
65     public long getCurrentQueueSize() {
66         return executor.getQueue().size();
67     }
68
69     @Override
70     public Long getLargestQueueSize() {
71         BlockingQueue<Runnable> queue = executor.getQueue();
72         if(queue instanceof TrackingLinkedBlockingQueue) {
73             return Long.valueOf(((TrackingLinkedBlockingQueue<?>)queue).getLargestQueueSize());
74         }
75
76         return null;
77     }
78
79     @Override
80     public long getMaxQueueSize() {
81         long queueSize = executor.getQueue().size();
82         return executor.getQueue().remainingCapacity() + queueSize;
83     }
84
85     @Override
86     public long getActiveThreadCount() {
87         return executor.getActiveCount();
88     }
89
90     @Override
91     public long getCompletedTaskCount() {
92         return  executor.getCompletedTaskCount();
93     }
94
95     @Override
96     public long getTotalTaskCount() {
97         return executor.getTaskCount();
98     }
99
100     @Override
101     public Long getRejectedTaskCount() {
102         RejectedExecutionHandler rejectedHandler = executor.getRejectedExecutionHandler();
103         if(rejectedHandler instanceof CountingRejectedExecutionHandler) {
104             return Long.valueOf(((CountingRejectedExecutionHandler)rejectedHandler)
105                                                                      .getRejectedTaskCount());
106         }
107
108         return null;
109     }
110
111     /**
112      * Returns a {@link ThreadExecutorStats} instance containing a snapshot of the statistic
113      * metrics.
114      */
115     public ThreadExecutorStats toThreadExecutorStats() {
116         return new ThreadExecutorStats(getActiveThreadCount(), getCurrentThreadPoolSize(),
117                 getLargestThreadPoolSize(), getMaxThreadPoolSize(), getCurrentQueueSize(),
118                 getLargestQueueSize(), getMaxQueueSize(), getCompletedTaskCount(),
119                 getTotalTaskCount(), getRejectedTaskCount());
120     }
121 }