/*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This source code is provided to illustrate the usage of a given feature
* or technique and has been deliberately simplified. Additional steps
* required for a production-quality application, such as security checks,
* input validation and proper error handling, might not be present in
* this sample code.
*/
/*
*
* Example of using the java.lang.management API to sort threads
* by CPU usage.
*
* JTop class can be run as a standalone application.
* It first establishs a connection to a target VM specified
* by the given hostname and port number where the JMX agent
* to be connected. It then polls for the thread information
* and the CPU consumption of each thread to display every 2
* seconds.
*
* It is also used by JTopPlugin which is a JConsolePlugin
* that can be used with JConsole (see README.txt). The JTop
* GUI will be added as a JConsole tab by the JTop plugin.
*
* @see com.sun.tools.jconsole.JConsolePlugin
*
* @author Mandy Chung
*/
import javax.management.*;
/**
* JTop is a JPanel to display thread's name, CPU time, and its state
* in a table.
*/
public JTop() {
tmodel = new MyTableModel();
// Set the renderer to format Double
// Add some space
// Create the scroll pane and add the table to it.
// Add the scroll pane to this panel.
}
// Set the MBeanServerConnection object for communicating
// with the target VM
try {
ThreadMXBean.class);
} catch (IOException e) {
e.printStackTrace();
}
if (!tmbean.isThreadCpuTimeSupported()) {
} else {
tmbean.setThreadCpuTimeEnabled(true);
}
}
"CPU(sec)",
"State"};
// List of all threads. The key of each entry is the CPU time
// and its value is the ThreadInfo object with no stack trace.
public MyTableModel() {
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return threadList.size();
}
return columnNames[col];
}
switch (col) {
case 0 :
// Column 0 shows the thread name
case 1 :
// Column 1 shows the CPU usage
case 2 :
// Column 2 shows the thread state
default:
return null;
}
}
}
threadList = list;
}
}
/**
* Get the thread list with CPU consumption and the ThreadInfo
* for each thread sorted by the CPU time.
*/
// Get all threads and their ThreadInfo objects
// with no stack trace
// build a map with key = CPU time and value = ThreadInfo
// filter out threads that have been terminated
}
}
// build the thread list and sort it with CPU time
// in decreasing order
return list;
}
/**
* Format Double with 4 fraction digits
*/
public DoubleRenderer() {
super();
}
}
}
}
// SwingWorker responsible for updating the GUI
//
// It first gets the thread and CPU usage information as a
// background task done by a worker thread so that
// it will not block the event dispatcher thread.
//
// When the worker thread finishes, the event dispatcher
// thread will invoke the done() method which will update
// the UI.
}
// Get the current thread info and CPU time
return getThreadList();
}
// fire table data changed to trigger GUI update
// when doInBackground() is finished
protected void done() {
try {
// Set table model with the new thread list
// refresh the table model
} catch (InterruptedException e) {
} catch (ExecutionException e) {
}
}
}
// Return a new SwingWorker for UI update
}
// Validate the input arguments
usage();
}
usage();
}
int port = -1;
try {
} catch (NumberFormatException x) {
usage();
}
if (port < 0) {
usage();
}
// Create the JTop Panel
// Set up the MBeanServerConnection to the target VM
// A timer task to update GUI per each interval
public void run() {
// Schedule the SwingWorker to update the GUI
}
};
// Create the standalone window with JTop panel
// by the event dispatcher thread
public void run() {
}
});
// refresh every 2 seconds
}
// Establish a connection with the remote application
//
// You can modify the urlPath to the address of the JMX agent
// of your application if it has a different URL.
//
// You can also modify the following code to take
// username and password for client authentication.
// Create an RMI connector client and connect it to
// the RMI connector server
try {
} catch (MalformedURLException e) {
// should not reach here
} catch (IOException e) {
}
return server;
}
private static void usage() {
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
// Create and set up the window.
// Create and set up the content pane.
// Display the window.
frame.setVisible(true);
}
}