181 samplerTask, oldInterval, 182 newInterval); 183 } 184 if (notifierTask != null) { 185 notifierTask.cancel(); 186 NotifierTask oldNotifierTask = notifierTask; 187 notifierTask = new NotifierTask(); 188 CountedTimerTaskUtils.reschedule(timer, oldNotifierTask, 189 notifierTask, oldInterval, 190 newInterval); 191 } 192 } 193 } 194 195 /** 196 * Fire MonitoredVmStructureChanged events. 197 * 198 * @param inserted List of Monitor objects inserted. 199 * @param removed List of Monitor objects removed. 200 */ 201 void fireMonitorStatusChangedEvents(List inserted, List removed) { 202 ArrayList registered = null; 203 MonitorStatusChangeEvent ev = null; 204 205 synchronized(listeners) { 206 registered = (ArrayList)listeners.clone(); 207 } 208 209 for (Iterator i = registered.iterator(); i.hasNext(); /* empty */) { 210 VmListener l = (VmListener)i.next(); 211 if (ev == null) { 212 ev = new MonitorStatusChangeEvent(this, inserted, removed); 213 } 214 l.monitorStatusChanged(ev); 215 } 216 } 217 218 /** 219 * Fire MonitoredVmStructureChanged events. 220 */ 221 void fireMonitorsUpdatedEvents() { 222 ArrayList registered = null; 223 VmEvent ev = null; 224 225 synchronized(listeners) { 226 registered = (ArrayList)listeners.clone(); 227 } 228 229 for (Iterator i = registered.iterator(); i.hasNext(); /* empty */) { 230 VmListener l = (VmListener)i.next(); 231 if (ev == null) { 232 ev = new VmEvent(this); 233 } 234 l.monitorsUpdated(ev); 235 } 236 } 237 238 /* 239 * Timer Tasks. There are two separate timer tasks here. The SamplerTask 240 * is active whenever we are attached to the remote JVM with a periodic 241 * sampling interval > 0. The NotifierTask is only active if a VmListener 242 * has registered with this RemoteMonitoredVm instance. Also, in the future 243 * we may want to run these tasks at different intervals. Currently, 244 * they run at the same interval and some significant work may 245 * need to be done to complete the separation of these two intervals. 246 */ 247 248 /** 249 * Class to periodically check the state of the defined monitors 250 * for the remote MonitoredVm instance and to notify listeners of 251 * any detected changes. 252 */ 253 private class NotifierTask extends CountedTimerTask { 254 public void run() { 255 super.run(); 256 try { 257 MonitorStatus status = getMonitorStatus(); 258 259 List inserted = status.getInserted(); 260 List removed = status.getRemoved(); 261 262 if (!inserted.isEmpty() || !removed.isEmpty()) { 263 fireMonitorStatusChangedEvents(inserted, removed); 264 } 265 } catch (MonitorException e) { 266 // XXX: use logging api? fire disconnect events? mark errored? 267 // fireDisconnectedEvents(); 268 System.err.println("Exception updating monitors for " 269 + getVmIdentifier()); 270 e.printStackTrace(); 271 // XXX: should we cancle the notifierTask here? 272 // this.cancel(); 273 } 274 } 275 } 276 277 /** 278 * Class to periodically sample the remote instrumentation byte buffer 279 * and refresh the local copy. Registered listeners are notified of 280 * the completion of a sampling event. | 181 samplerTask, oldInterval, 182 newInterval); 183 } 184 if (notifierTask != null) { 185 notifierTask.cancel(); 186 NotifierTask oldNotifierTask = notifierTask; 187 notifierTask = new NotifierTask(); 188 CountedTimerTaskUtils.reschedule(timer, oldNotifierTask, 189 notifierTask, oldInterval, 190 newInterval); 191 } 192 } 193 } 194 195 /** 196 * Fire MonitoredVmStructureChanged events. 197 * 198 * @param inserted List of Monitor objects inserted. 199 * @param removed List of Monitor objects removed. 200 */ 201 @SuppressWarnings("unchecked") // Cast of result of clone 202 void fireMonitorStatusChangedEvents(List<Monitor> inserted, List<Monitor> removed) { 203 ArrayList<VmListener> registered = null; 204 MonitorStatusChangeEvent ev = null; 205 206 synchronized(listeners) { 207 registered = (ArrayList)listeners.clone(); 208 } 209 210 for (Iterator<VmListener> i = registered.iterator(); i.hasNext(); /* empty */) { 211 VmListener l = i.next(); 212 if (ev == null) { 213 ev = new MonitorStatusChangeEvent(this, inserted, removed); 214 } 215 l.monitorStatusChanged(ev); 216 } 217 } 218 219 /** 220 * Fire MonitoredVmStructureChanged events. 221 */ 222 @SuppressWarnings("unchecked") // Cast of result of clone 223 void fireMonitorsUpdatedEvents() { 224 ArrayList<VmListener> registered = null; 225 VmEvent ev = null; 226 227 synchronized(listeners) { 228 registered = (ArrayList)listeners.clone(); 229 } 230 231 for (Iterator<VmListener> i = registered.iterator(); i.hasNext(); /* empty */) { 232 VmListener l = i.next(); 233 if (ev == null) { 234 ev = new VmEvent(this); 235 } 236 l.monitorsUpdated(ev); 237 } 238 } 239 240 /* 241 * Timer Tasks. There are two separate timer tasks here. The SamplerTask 242 * is active whenever we are attached to the remote JVM with a periodic 243 * sampling interval > 0. The NotifierTask is only active if a VmListener 244 * has registered with this RemoteMonitoredVm instance. Also, in the future 245 * we may want to run these tasks at different intervals. Currently, 246 * they run at the same interval and some significant work may 247 * need to be done to complete the separation of these two intervals. 248 */ 249 250 /** 251 * Class to periodically check the state of the defined monitors 252 * for the remote MonitoredVm instance and to notify listeners of 253 * any detected changes. 254 */ 255 private class NotifierTask extends CountedTimerTask { 256 public void run() { 257 super.run(); 258 try { 259 MonitorStatus status = getMonitorStatus(); 260 261 List<Monitor> inserted = status.getInserted(); 262 List<Monitor> removed = status.getRemoved(); 263 264 if (!inserted.isEmpty() || !removed.isEmpty()) { 265 fireMonitorStatusChangedEvents(inserted, removed); 266 } 267 } catch (MonitorException e) { 268 // XXX: use logging api? fire disconnect events? mark errored? 269 // fireDisconnectedEvents(); 270 System.err.println("Exception updating monitors for " 271 + getVmIdentifier()); 272 e.printStackTrace(); 273 // XXX: should we cancle the notifierTask here? 274 // this.cancel(); 275 } 276 } 277 } 278 279 /** 280 * Class to periodically sample the remote instrumentation byte buffer 281 * and refresh the local copy. Registered listeners are notified of 282 * the completion of a sampling event. |