FileDocCategorySizeDatePackage
ThreadMonitor.javaAPI DocGlassfish v2 API6906Fri May 04 22:24:10 BST 2007com.sun.enterprise.admin.mbeans.jvm

ThreadMonitor

public class ThreadMonitor extends Object

Fields Summary
private final MBeanServerConnection
mbsc
private static final Logger
logger
private final com.sun.enterprise.util.i18n.StringManager
sm
Constructors Summary
public ThreadMonitor(MBeanServerConnection mbsc)

        
        this.mbsc = mbsc;
    
Methods Summary
private java.lang.StringdumpThread(java.lang.management.ThreadInfo ti)

        final long ids = ti.getThreadId();
        final String ss  = ti.getThreadState().toString();
        //following should work because of autoboxing :)
        String msg = sm.getString("thread.title", quote(ti.getThreadName()), ids, ss);
        final StringBuilder sb = new StringBuilder(msg);
        if (ti.getLockName() != null) {
            msg = sm.getString("thread.waiting.on", ti.getLockName());
            sb.append(msg);
        }
        if (ti.isSuspended()) {
            msg = sm.getString("thread.suspended");
            sb.append(msg);
        }
        if (ti.isInNative()) {
            msg = sm.getString("thread.in.native");
            sb.append(msg);
        }
        sb.append(System.getProperty("line.separator"));
        if (ti.getLockOwnerName() != null) {
            msg = sm.getString("thread.owner", ti.getLockOwnerName(), ti.getLockOwnerId());
            sb.append(msg);
        }
        for (final StackTraceElement ste : ti.getStackTrace()) {
           msg = sm.getString("thread.stack.element", ste.toString());
           sb.append(msg);
           sb.append(System.getProperty("line.separator"));
        }
        sb.append(System.getProperty("line.separator"));
        return ( sb.toString() );
    
private java.lang.StringgetDeadlockInfo(java.lang.management.ThreadMXBean tmx)

        final StringBuilderNewLineAppender sb = new StringBuilderNewLineAppender(new StringBuilder());
        final long[] dts = tmx.findMonitorDeadlockedThreads();
        if (dts == null) {
            sb.append(sm.getString("no.deadlock"));
        }
        else {
            sb.append(sm.getString("deadlocks.found"));
            for (final long dt : dts) {
                final ThreadInfo ti = tmx.getThreadInfo(dt);
                sb.append(this.dumpThread(ti));
            }
        }
        return ( sb.toString() );
    
public final java.lang.StringgetThreadDump()

        final long start = System.currentTimeMillis();
        final StringBuilder sb = new StringBuilder();
        final StringBuilderNewLineAppender td = new StringBuilderNewLineAppender(sb);
        try {
            final ThreadMXBean tmx = ManagementFactory.newPlatformMXBeanProxy(mbsc, ManagementFactory.THREAD_MXBEAN_NAME, ThreadMXBean.class);
            final String title = getTitle();
            td.append(title); 
            final long[] tids = tmx.getAllThreadIds();
            td.append(sm.getString("thread.no", tmx.getThreadCount()));
            td.append(sm.getString("daemon.threads.no", tmx.getDaemonThreadCount()));
            final ThreadInfo[] tinfos = tmx.getThreadInfo(tids, Integer.MAX_VALUE);
            /*
            Arrays.sort(tinfos, new Comparator<ThreadInfo> () {
                public int compare(ThreadInfo a, ThreadInfo b) {
                    return ( a.getThreadName().compareTo(b.getThreadName()) );
                }
            });
             */
            for (final ThreadInfo ti : tinfos) {
                td.append(dumpThread(ti));
            }
            sb.append(getDeadlockInfo(tmx));
            return ( td.toString() );
        } catch(final Exception e) {
            throw new RuntimeException(e);
        }
        finally {
            final long end = System.currentTimeMillis();
            final double time = (end/1000.0) - (start/1000.0);
            logger.info("Time in seconds to get the jvm thread dump: " + time);
        }
    
private java.lang.StringgetTitle()

        final RuntimeMXBean rt  = ManagementFactory.newPlatformMXBeanProxy(mbsc, ManagementFactory.RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);
        final String vmname     = rt.getVmName();
        final String vmversion  = rt.getVmVersion();
        final String vmvendor   = rt.getVmVendor();
        final String title      = sm.getString("td.title", vmname, vmversion, vmvendor);
        
        return ( title );
    
private java.lang.Stringquote(java.lang.String uq)

        final StringBuilder sb = new StringBuilder("\"");
        sb.append(uq).append("\"");
        return ( sb.toString() );