iter = heartbeatRequestSub.getRemovedCollection().iterator();
Iterator iter = heartbeatRequestSub.getRemovedCollection().iterator();
  protected void execute() {    // prepare HeartbeatHealthReports    Iterator iter = prepareHealthReportsSub.getCollection().iterator();    boolean didOnce = false;    while (iter.hasNext()) {      PrepareHealthReports obj = (PrepareHealthReports)iter.next();      if (log.isDebugEnabled())         log.debug("execute: received added PrepareHealthReports = " + obj);      if (!didOnce) {        prepareHealthReports();        didOnce = true;      }      if (log.isDebugEnabled())        log.debug("execute: publishRemove PrepareHealthReports =" + obj);      bb.publishRemove(obj);    }    // process REMOVED HeartbeatRequests    iter = heartbeatRequestSub.getRemovedCollection().iterator();    while (iter.hasNext()) {      HeartbeatRequest req = (HeartbeatRequest)iter.next();      if (log.isDebugEnabled())         log.debug("execute: received removed HeartbeatRequest = " + req);      reqTable.remove(req);      UID hbReqUID = req.getHbReqUID();      Iterator iter2 = hbReqSub.getCollection().iterator();      while (iter2.hasNext()) {        HbReq hbReq = (HbReq)iter2.next();        if (hbReq.getUID().equals(hbReqUID)) {          if (log.isDebugEnabled())            log.debug("execute: publishRemove HbReq = " + hbReq);          bb.publishRemove(hbReq);        }      }     }    // check for responses from HeartbeatServerPlugin    // the first response will cause the status in the HeartbeatRequest to be updated    // all responses except REFUSED are considered heartbeats and are entered in hbTable    // Eventually, heartbeats won't show up this way, as they will be filtered in MTS,    // and fed into the Metrics Service and we will get them there.    iter = hbReqSub.getChangedCollection().iterator();    while (iter.hasNext()) {      HbReq hbReq = (HbReq)iter.next();      if (log.isDebugEnabled())         log.debug("execute: received changed HbReq = " + hbReq);      MessageAddress myAddr = getAgentIdentifier();      if (hbReq.getSource().equals(myAddr)) {        HbReqResponse response = (HbReqResponse)hbReq.getResponse();        int status = response.getStatus();        Date now = new Date();        switch (status) {          case HeartbeatRequest.ACCEPTED:            hbTable.put(response.getResponder(), now);            HbReqContent content = (HbReqContent)hbReq.getContent();            UID reqUID = content.getHeartbeatRequestUID();            reportTable.put(reqUID, now);            // figure out when the alarm should be set            HeartbeatRequest req = (HeartbeatRequest)reqTable.findUniqueObject(reqUID);            long freq = req.getHbFrequency();            float percentOutOfSpec = req.getPercentOutOfSpec();            long fromNow = freq + (long)(freq * (percentOutOfSpec / 100));            if (nextAlarm == null){    // if no alarm set yet, this is the first                                       // ACCEPTED request, so set one to                                        // (freq + (freq * percentOutOfSpec)) from now              nextAlarm = new SendHealthReportsAlarm(fromNow);              alarmService.addRealTimeAlarm(nextAlarm);            } else { // an alarm is already set - figure out if its soon enough              long fromNowTime = now.getTime() + fromNow;              if (nextAlarm.getExpirationTime() > fromNowTime) {                nextAlarm.cancel();                 nextAlarm = new SendHealthReportsAlarm(fromNow);                alarmService.addRealTimeAlarm(nextAlarm);              }            } // falls through          case HeartbeatRequest.REFUSED:            updateHeartbeatRequest(hbReq);            break;          case HbReqResponse.HEARTBEAT:            hbTable.put(response.getResponder(), now);            break;          default:            throw new RuntimeException("illegal status = " + status);        }      }    }    // process NEW HeartbeatRequests    iter = heartbeatRequestSub.getAddedCollection().iterator();    while (iter.hasNext()) {      HeartbeatRequest req = (HeartbeatRequest)iter.next();      int status = req.getStatus();      if (status == HeartbeatRequest.NEW) {        if (log.isDebugEnabled())           log.debug("execute: received added HeartbeatRequest = " + req);        MessageAddress source = getAgentIdentifier();        Set targets = req.getTargets();        if (!targets.contains(source)){  //source cannot be a target          UID reqUID = req.getUID();          reqTable.add(req);          req.setStatus(HeartbeatRequest.SENT);          req.setTimeSent(new Date());          HbReqContent content = new HbReqContent(reqUID,                                                   req.getReqTimeout(),                                                   req.getHbFrequency(),                                                   req.getHbTimeout());          UID hbReqUID = getUIDService().nextUID();          req.setHbReqUID(hbReqUID);          HbReq hbReq = new HbReq(hbReqUID,                                   source,                                   targets,                                   content,                                   null);          if (log.isDebugEnabled())            log.debug("execute: publishAdd HbReq = " + hbReq);          bb.publishAdd(hbReq);          if (log.isDebugEnabled())            log.debug("execute: publishChange HeartbeatRequest = " + req);          bb.publishChange(req);          alarmService.addRealTimeAlarm(new HeartbeatRequestTimeout(req.getReqTimeout(),reqUID));        } else { //exclude source in targets          if (log.isErrorEnabled()) {            log.error("execute: Illegal HeartbeatRequest received. Source cannot also be a target. HeartbeatRequest = " + req);          }          req.setStatus(HeartbeatRequest.FAILED);          if (log.isDebugEnabled())              log.debug("execute: publishChange HeartbeatRequest = " + req);          bb.publishChange(req);        }       }    }  }