[jboss-user] [Messaging, JMS & JBossMQ] - Re: excess UIL2 threads

oglueck do-not-reply at jboss.com
Fri Jun 29 11:13:12 EDT 2007


If you want to monitor thread death, deploy this mbean and watch the log:

public class ThreadCoroner extends ServiceMBeanSupport implements ThreadCoronerMBean {
  |     private ThreadMXBean mbean;
  |     private Map<Long, ThreadInfo> last;
  |     private Timer timer;
  |     private int interval = 60;
  |     
  |     
  |     public int getInterval() {
  |         return interval;
  |     }
  |     public void setInterval(int interval) {
  |         this.interval = interval;
  |     }
  | 
  |     @Override
  |     protected void startService() throws Exception {
  |         mbean = ManagementFactory.getThreadMXBean();
  |         
  |         timer = new Timer("ThreadCoroner-Timer", true);
  |         TimerTask task = new TimerTask() {
  |             @Override
  |             public void run() {
  |                 logDeadThreads();
  |             }
  |         };
  |         timer.schedule(task, new Date(), interval * 1000L);        
  |     }
  | 
  |     @Override
  |     protected void stopService() throws Exception {
  |         timer.cancel();
  |         mbean = null;
  |         last = null;
  |     }
  |     
  |     private void logDeadThreads() {
  |         Map<Long, ThreadInfo> current = getAllThreads();
  |         if (last != null) {
  |             Set<ThreadInfo> dead = new HashSet<ThreadInfo>();
  |             for (Map.Entry<Long, ThreadInfo> entry : last.entrySet()) {
  |                 if (!current.containsKey(entry.getKey())) {
  |                     dead.add(entry.getValue());
  |                 } 
  |             }
  |             
  |             for (ThreadInfo ti : dead) {
  |                 log.debug(ti.getThreadName());
  |             }
  |         }
  |         last = current;
  |     }
  |     
  |     private Map<Long, ThreadInfo> getAllThreads() {
  |         long myId = Thread.currentThread().getId();
  |         long[] ids = mbean.getAllThreadIds();
  |         ThreadInfo[] infos = mbean.getThreadInfo(ids);
  |         Map<Long, ThreadInfo> map = new HashMap<Long, ThreadInfo>(infos.length);
  |         for (ThreadInfo info : infos) {
  |             if (info.getThreadId() == myId) continue;
  |             map.put(new Long(info.getThreadId()), info);
  |         }
  |         return map;
  |     }
  | }

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4059224#4059224

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4059224



More information about the jboss-user mailing list