blob: f96a0650f4a1ee3a53bcf97bdb6eac7c00170d36 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2012 Laurent CARON
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Laurent CARON (laurent.caron at gmail dot com) - initial API and implementation
*******************************************************************************/
package org.mihalis.opal.systemMonitor;
import java.lang.management.ManagementFactory;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
/**
* Instances of this class represent a sample that contains the Thread Usage of
* the running application.
*/
public class ThreadsUsageSample implements Sample {
/** The Constant PEAK_THREAD_COUNT. */
private static final String PEAK_THREAD_COUNT = "PeakThreadCount";
/** The Constant THREAD_COUNT. */
private static final String THREAD_COUNT = "ThreadCount";
/** The Constant OBJECT_NAME_ATTRIBUTE. */
private static final String OBJECT_NAME_ATTRIBUTE = "java.lang:type=Threading";
/** The m bean server connection. */
private final MBeanServerConnection mBeanServerConnection = ManagementFactory.getPlatformMBeanServer();
/** The object name. */
private ObjectName objectName;
/**
* Constructor.
*/
ThreadsUsageSample() {
try {
this.objectName = new ObjectName(OBJECT_NAME_ATTRIBUTE);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
/**
* Gets the value.
*
* @return the value
* @see org.mihalis.opal.systemMonitor.Sample#getValue()
*/
@Override
public double getValue() {
try {
return ((Integer) this.mBeanServerConnection.getAttribute(this.objectName, THREAD_COUNT)).intValue();
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
/**
* Gets the max value.
*
* @return the max value
* @see org.mihalis.opal.systemMonitor.Sample#getMaxValue()
*/
@Override
public double getMaxValue() {
try {
return ((Integer) this.mBeanServerConnection.getAttribute(this.objectName, PEAK_THREAD_COUNT)).intValue();
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
}