src/share/classes/sun/jvmstat/perfdata/monitor/protocol/rmi/MonitoredHostProvider.java

Print this page




 229 
 230     /**
 231      * {@inheritDoc}
 232      */
 233     public Set<Integer> activeVms() throws MonitorException {
 234         return vmManager.activeVms();
 235     }
 236 
 237     /**
 238      * Fire VmStatusChangeEvent events to HostListener objects
 239      *
 240      * @param active Set of Integer objects containing the local
 241      *               Vm Identifiers of the active JVMs
 242      * @param started Set of Integer objects containing the local
 243      *                Vm Identifiers of new JVMs started since last
 244      *                interval.
 245      * @param terminated Set of Integer objects containing the local
 246      *                   Vm Identifiers of terminated JVMs since last
 247      *                   interval.
 248      */
 249     private void fireVmStatusChangedEvents(Set active, Set started,
 250                                            Set terminated) {
 251         ArrayList registered = null;

 252         VmStatusChangeEvent ev = null;
 253 
 254         synchronized(listeners) {
 255             registered = (ArrayList)listeners.clone();
 256         }
 257 
 258         for (Iterator i = registered.iterator(); i.hasNext(); /* empty */) {
 259             HostListener l = (HostListener)i.next();
 260             if (ev == null) {
 261                 ev = new VmStatusChangeEvent(this, active, started, terminated);
 262             }
 263             l.vmStatusChanged(ev);
 264         }
 265     }
 266 
 267     /**
 268      * Fire hostDisconnectEvent events.
 269      */

 270     void fireDisconnectedEvents() {
 271         ArrayList registered = null;
 272         HostEvent ev = null;
 273 
 274         synchronized(listeners) {
 275             registered = (ArrayList)listeners.clone();
 276         }
 277 
 278         for (Iterator i = registered.iterator(); i.hasNext(); /* empty */) {
 279             HostListener l = (HostListener)i.next();
 280             if (ev == null) {
 281                 ev = new HostEvent(this);
 282             }
 283             l.disconnected(ev);
 284         }
 285     }
 286 
 287     /**
 288      * class to poll the remote machine and generate local event notifications.
 289      */
 290     private class NotifierTask extends CountedTimerTask {
 291         public void run() {
 292             super.run();
 293 
 294             // save the last set of active JVMs
 295             Set lastActiveVms = activeVms;
 296 
 297             try {
 298                 // get the current set of active JVMs
 299                 activeVms = (HashSet<Integer>)vmManager.activeVms();
 300 
 301             } catch (MonitorException e) {
 302                 // XXX: use logging api
 303                 System.err.println("MonitoredHostProvider: polling task "
 304                                    + "caught MonitorException:");
 305                 e.printStackTrace();
 306 
 307                 // mark the HostManager as errored and notify listeners
 308                 setLastException(e);
 309                 fireDisconnectedEvents();
 310             }
 311 
 312             if (activeVms.isEmpty()) {
 313                 return;
 314             }
 315 
 316             Set<Integer> startedVms = new HashSet<Integer>();
 317             Set<Object> terminatedVms = new HashSet<Object>();
 318 
 319             for (Iterator i = activeVms.iterator(); i.hasNext(); /* empty */ ) {
 320                 Integer vmid = (Integer)i.next();
 321                 if (!lastActiveVms.contains(vmid)) {
 322                     // a new file has been detected, add to set
 323                     startedVms.add(vmid);
 324                 }
 325             }
 326 
 327             for (Iterator i = lastActiveVms.iterator(); i.hasNext();
 328                     /* empty */ ) {
 329                 Object o = i.next();
 330                 if (!activeVms.contains(o)) {
 331                     // JVM has terminated, remove it from the active list
 332                     terminatedVms.add(o);
 333                 }
 334             }
 335 
 336             if (!startedVms.isEmpty() || !terminatedVms.isEmpty()) {
 337                 fireVmStatusChangedEvents(activeVms, startedVms, terminatedVms);
 338             }
 339         }
 340     }
 341 }


 229 
 230     /**
 231      * {@inheritDoc}
 232      */
 233     public Set<Integer> activeVms() throws MonitorException {
 234         return vmManager.activeVms();
 235     }
 236 
 237     /**
 238      * Fire VmStatusChangeEvent events to HostListener objects
 239      *
 240      * @param active Set of Integer objects containing the local
 241      *               Vm Identifiers of the active JVMs
 242      * @param started Set of Integer objects containing the local
 243      *                Vm Identifiers of new JVMs started since last
 244      *                interval.
 245      * @param terminated Set of Integer objects containing the local
 246      *                   Vm Identifiers of terminated JVMs since last
 247      *                   interval.
 248      */
 249     @SuppressWarnings("unchecked") // Cast of result of clone
 250     private void fireVmStatusChangedEvents(Set<Integer> active, Set<Integer> started,
 251                                            Set<Integer> terminated) {
 252         ArrayList<HostListener> registered = null;
 253         VmStatusChangeEvent ev = null;
 254 
 255         synchronized(listeners) {
 256             registered = (ArrayList)listeners.clone();
 257         }
 258 
 259         for (Iterator<HostListener> i = registered.iterator(); i.hasNext(); /* empty */) {
 260             HostListener l = i.next();
 261             if (ev == null) {
 262                 ev = new VmStatusChangeEvent(this, active, started, terminated);
 263             }
 264             l.vmStatusChanged(ev);
 265         }
 266     }
 267 
 268     /**
 269      * Fire hostDisconnectEvent events.
 270      */
 271     @SuppressWarnings("unchecked") // Cast of result of clone
 272     void fireDisconnectedEvents() {
 273         ArrayList<HostListener> registered = null;
 274         HostEvent ev = null;
 275 
 276         synchronized(listeners) {
 277             registered = (ArrayList)listeners.clone();
 278         }
 279 
 280         for (Iterator<HostListener> i = registered.iterator(); i.hasNext(); /* empty */) {
 281             HostListener l = i.next();
 282             if (ev == null) {
 283                 ev = new HostEvent(this);
 284             }
 285             l.disconnected(ev);
 286         }
 287     }
 288 
 289     /**
 290      * class to poll the remote machine and generate local event notifications.
 291      */
 292     private class NotifierTask extends CountedTimerTask {
 293         public void run() {
 294             super.run();
 295 
 296             // save the last set of active JVMs
 297             Set<Integer> lastActiveVms = activeVms;
 298 
 299             try {
 300                 // get the current set of active JVMs
 301                 activeVms = (HashSet<Integer>)vmManager.activeVms();
 302 
 303             } catch (MonitorException e) {
 304                 // XXX: use logging api
 305                 System.err.println("MonitoredHostProvider: polling task "
 306                                    + "caught MonitorException:");
 307                 e.printStackTrace();
 308 
 309                 // mark the HostManager as errored and notify listeners
 310                 setLastException(e);
 311                 fireDisconnectedEvents();
 312             }
 313 
 314             if (activeVms.isEmpty()) {
 315                 return;
 316             }
 317 
 318             Set<Integer> startedVms = new HashSet<>();
 319             Set<Integer> terminatedVms = new HashSet<>();
 320 
 321             for (Iterator<Integer> i = activeVms.iterator(); i.hasNext(); /* empty */ ) {
 322                 Integer vmid = i.next();
 323                 if (!lastActiveVms.contains(vmid)) {
 324                     // a new file has been detected, add to set
 325                     startedVms.add(vmid);
 326                 }
 327             }
 328 
 329             for (Iterator<Integer> i = lastActiveVms.iterator(); i.hasNext();
 330                     /* empty */ ) {
 331                 Integer o = i.next();
 332                 if (!activeVms.contains(o)) {
 333                     // JVM has terminated, remove it from the active list
 334                     terminatedVms.add(o);
 335                 }
 336             }
 337 
 338             if (!startedVms.isEmpty() || !terminatedVms.isEmpty()) {
 339                 fireVmStatusChangedEvents(activeVms, startedVms, terminatedVms);
 340             }
 341         }
 342     }
 343 }