001 package org.shiftone.jrat.core.jmx; 002 003 004 import org.shiftone.jrat.util.log.Logger; 005 006 import java.util.ArrayList; 007 import java.util.List; 008 009 010 /** 011 * @author jeff@shiftone.org (Jeff Drost) 012 */ 013 public class WaitingJmxRegistry implements JmxRegistry, Runnable { 014 015 private static final Logger LOG = Logger.getLogger(WaitingJmxRegistry.class); 016 private List waitList = new ArrayList(); 017 private final JmxRegistry registry; 018 private final Thread waitThread; 019 020 public WaitingJmxRegistry(JmxRegistry registry) { 021 022 this.registry = registry; 023 this.waitThread = new Thread(this); 024 025 this.waitThread.setDaemon(true); 026 this.waitThread.setName("JRat-JMX-Poller"); 027 this.waitThread.setPriority(Thread.MIN_PRIORITY); 028 this.waitThread.start(); 029 } 030 031 032 public boolean isReady() { 033 return true; 034 } 035 036 037 public void registerMBean(Object object, String name) { 038 039 synchronized (this) { 040 waitList.add(new WaitingRegisterRequest(object, name)); 041 } 042 } 043 044 045 private void registerNow() { 046 047 List currentList = null; 048 049 synchronized (this) { 050 if (!waitList.isEmpty()) { 051 052 // if there are waiting mbeans, then "copy" the list 053 currentList = waitList; 054 this.waitList = new ArrayList(); 055 } 056 } 057 058 if (currentList != null) { 059 LOG.info("registering " + currentList.size() + " mbean(s)"); 060 061 for (int i = 0; i < currentList.size(); i++) { 062 WaitingRegisterRequest request = (WaitingRegisterRequest) currentList.get(i); 063 064 registry.registerMBean(request.object, request.name); 065 } 066 } 067 } 068 069 070 public void run() { 071 072 int sleep = 10000; 073 074 while (true) { 075 try { 076 LOG.debug("polling MBeanServer..."); 077 078 if (registry.isReady()) { 079 registerNow(); 080 081 return; 082 } 083 084 Thread.sleep(sleep); 085 } 086 catch (Throwable e) { 087 LOG.warn("JMX poller thread encountered an error", e); 088 } 089 } 090 } 091 092 093 private class WaitingRegisterRequest { 094 095 private Object object; 096 private String name; 097 098 public WaitingRegisterRequest(Object object, String name) { 099 this.object = object; 100 this.name = name; 101 } 102 } 103 }