KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > mbeans > jvm > ThreadMonitor


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * ThreadMonitor.java
26  *
27  * Created on July 21, 2005, 11:50 AM
28  */

29
30 package com.sun.enterprise.admin.mbeans.jvm;
31
32 import com.sun.enterprise.admin.common.constant.AdminConstants;
33 import com.sun.enterprise.util.i18n.StringManager;
34 import java.lang.management.ManagementFactory JavaDoc;
35 import java.lang.management.RuntimeMXBean JavaDoc;
36 import java.lang.management.ThreadInfo JavaDoc;
37 import java.lang.management.ThreadMXBean JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39 import javax.management.MBeanServerConnection JavaDoc;
40
41 /**
42  */

43 class ThreadMonitor {
44     
45     private final MBeanServerConnection JavaDoc mbsc;
46     private static final Logger JavaDoc logger = Logger.getLogger(AdminConstants.kLoggerName);
47     private final StringManager sm = StringManager.getManager(ThreadMonitor.class);
48     public ThreadMonitor(final MBeanServerConnection JavaDoc mbsc) {
49         this.mbsc = mbsc;
50     }
51     public final String JavaDoc getThreadDump() {
52         final long start = System.currentTimeMillis();
53         final StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
54         final StringBuilderNewLineAppender td = new StringBuilderNewLineAppender(sb);
55         try {
56             final ThreadMXBean JavaDoc tmx = ManagementFactory.newPlatformMXBeanProxy(mbsc, ManagementFactory.THREAD_MXBEAN_NAME, ThreadMXBean JavaDoc.class);
57             final String JavaDoc title = getTitle();
58             td.append(title);
59             final long[] tids = tmx.getAllThreadIds();
60             td.append(sm.getString("thread.no", tmx.getThreadCount()));
61             td.append(sm.getString("daemon.threads.no", tmx.getDaemonThreadCount()));
62             final ThreadInfo JavaDoc[] tinfos = tmx.getThreadInfo(tids, Integer.MAX_VALUE);
63             /*
64             Arrays.sort(tinfos, new Comparator<ThreadInfo> () {
65                 public int compare(ThreadInfo a, ThreadInfo b) {
66                     return ( a.getThreadName().compareTo(b.getThreadName()) );
67                 }
68             });
69              */

70             for (final ThreadInfo JavaDoc ti : tinfos) {
71                 td.append(dumpThread(ti));
72             }
73             sb.append(getDeadlockInfo(tmx));
74             return ( td.toString() );
75         } catch(final Exception JavaDoc e) {
76             throw new RuntimeException JavaDoc(e);
77         }
78         finally {
79             final long end = System.currentTimeMillis();
80             final double time = (end/1000.0) - (start/1000.0);
81             logger.info("Time in seconds to get the jvm thread dump: " + time);
82         }
83     }
84     private String JavaDoc dumpThread(final ThreadInfo JavaDoc ti) {
85         final long ids = ti.getThreadId();
86         final String JavaDoc ss = ti.getThreadState().toString();
87         //following should work because of autoboxing :)
88
String JavaDoc msg = sm.getString("thread.title", quote(ti.getThreadName()), ids, ss);
89         final StringBuilder JavaDoc sb = new StringBuilder JavaDoc(msg);
90         if (ti.getLockName() != null) {
91             msg = sm.getString("thread.waiting.on", ti.getLockName());
92             sb.append(msg);
93         }
94         if (ti.isSuspended()) {
95             msg = sm.getString("thread.suspended");
96             sb.append(msg);
97         }
98         if (ti.isInNative()) {
99             msg = sm.getString("thread.in.native");
100             sb.append(msg);
101         }
102         sb.append(System.getProperty("line.separator"));
103         if (ti.getLockOwnerName() != null) {
104             msg = sm.getString("thread.owner", ti.getLockOwnerName(), ti.getLockOwnerId());
105             sb.append(msg);
106         }
107         for (final StackTraceElement JavaDoc ste : ti.getStackTrace()) {
108            msg = sm.getString("thread.stack.element", ste.toString());
109            sb.append(msg);
110            sb.append(System.getProperty("line.separator"));
111         }
112         sb.append(System.getProperty("line.separator"));
113         return ( sb.toString() );
114     }
115     
116     private String JavaDoc getTitle() throws Exception JavaDoc {
117         final RuntimeMXBean JavaDoc rt = ManagementFactory.newPlatformMXBeanProxy(mbsc, ManagementFactory.RUNTIME_MXBEAN_NAME, RuntimeMXBean JavaDoc.class);
118         final String JavaDoc vmname = rt.getVmName();
119         final String JavaDoc vmversion = rt.getVmVersion();
120         final String JavaDoc vmvendor = rt.getVmVendor();
121         final String JavaDoc title = sm.getString("td.title", vmname, vmversion, vmvendor);
122         
123         return ( title );
124     }
125     
126     private String JavaDoc quote(final String JavaDoc uq) {
127         final StringBuilder JavaDoc sb = new StringBuilder JavaDoc("\"");
128         sb.append(uq).append("\"");
129         return ( sb.toString() );
130     }
131     
132     private String JavaDoc getDeadlockInfo(final ThreadMXBean JavaDoc tmx) {
133         final StringBuilderNewLineAppender sb = new StringBuilderNewLineAppender(new StringBuilder JavaDoc());
134         final long[] dts = tmx.findMonitorDeadlockedThreads();
135         if (dts == null) {
136             sb.append(sm.getString("no.deadlock"));
137         }
138         else {
139             sb.append(sm.getString("deadlocks.found"));
140             for (final long dt : dts) {
141                 final ThreadInfo JavaDoc ti = tmx.getThreadInfo(dt);
142                 sb.append(this.dumpThread(ti));
143             }
144         }
145         return ( sb.toString() );
146     }
147 }
148
Popular Tags