public void disposeWorker(WorkerEntry workerEntry, List<IResponseTO> responses) {
    ServiceID serviceID = workerEntry.getServiceID();

    responses.add(new LoggerResponseTO("Worker dispose: " + serviceID, LoggerResponseTO.DEBUG));

    workerEntry.dispose();

    WorkerDAO workerDAO = BrokerDAOFactory.getInstance().getWorkerDAO();
    String wAddress = StringUtil.deploymentIDToAddress(workerEntry.getWorkerID());

    DisposeWorkerResponseTO disposeTO = new DisposeWorkerResponseTO();
    disposeTO.setPeerAddress(StringUtil.deploymentIDToAddress(workerEntry.getPeerID()));
    disposeTO.setWorkerAddress(wAddress);
    disposeTO.setWorkerPublicKey(workerDAO.getWorkerPublicKey(wAddress));
    responses.add(disposeTO);

    responses.add(new LoggerResponseTO("Stub to be released: " + wAddress, LoggerResponseTO.INFO));

    ReleaseResponseTO releaseTO = new ReleaseResponseTO();
    releaseTO.setStubAddress(wAddress);
    responses.add(releaseTO);

    workerDAO.removeWorker(wAddress);
    WorkerInfo.getInstance().removeWorker(workerEntry.getServiceID().getContainerID().toString());
  }
  /**
   * @param job
   * @param workerEntry
   */
  private void unwantWorker(Job job, WorkerEntry workerEntry, List<IResponseTO> responses) {

    responses.add(
        new LoggerResponseTO(
            "Worker unwanted: " + workerEntry.getWorkerID(), LoggerResponseTO.DEBUG));

    job.unwantWorker(workerEntry);
    workerEntry.unwant();

    RequestSpecification spec = workerEntry.getRequestSpecification();

    String peerAddress = StringUtil.deploymentIDToAddress(workerEntry.getPeerID());
    String workerAddress = StringUtil.deploymentIDToAddress(workerEntry.getWorkerID());
    Integer jobId = Integer.parseInt("" + spec.getJobId());

    JobSpecification jobSpec = BrokerDAOFactory.getInstance().getJobDAO().getJobSpec(jobId);

    UnwantWorkerResponseTO unwantWorkerTO = new UnwantWorkerResponseTO();
    unwantWorkerTO.setJobID(jobId);
    unwantWorkerTO.setJobSpec(jobSpec);
    unwantWorkerTO.setMaxFails(spec.getMaxFails());
    unwantWorkerTO.setMaxReplicas(spec.getMaxReplicas());
    unwantWorkerTO.setPeerAddress(peerAddress);
    unwantWorkerTO.setRequestID(spec.getRequestId());
    unwantWorkerTO.setRequiredWorkers(spec.getRequiredWorkers());

    unwantWorkerTO.setWorkerAddress(workerAddress);

    WorkerDAO workerDAO = BrokerDAOFactory.getInstance().getWorkerDAO();

    String workerPublicKey = workerDAO.getWorkerPublicKey(workerAddress);
    unwantWorkerTO.setWorkerPublicKey(workerPublicKey);

    responses.add(unwantWorkerTO);

    ReleaseResponseTO releaseTO = new ReleaseResponseTO();
    releaseTO.setStubAddress(workerAddress);

    responses.add(releaseTO);

    workerDAO.removeWorker(workerAddress);

    WorkerInfo.getInstance().removeWorker(workerEntry.getServiceID().getContainerID().toString());
  }
  public void executionFailed(GridProcess execution, List<IResponseTO> responses) {

    reportReplicaAccounting(execution, responses);

    Job job = execution.getJob();
    GridProcessExecutionResult executionResult = execution.getResult();

    try {
      job.newReplicaResult(
          executionResult,
          GridProcessState.FAILED,
          verifyFailure(execution.getTask(), GridProcessState.FAILED),
          canReplicate(execution.getTask()));
    } catch (IllegalResultException e) {

      responses.add(
          new LoggerResponseTO(
              "Illegal result on replica " + execution.getState() + " : " + e.getMessage(),
              LoggerResponseTO.ERROR));
    }

    GridProcessHandle handle = executionResult.getReplicaHandle();

    WorkerEntry workerEntry = execution.getWorkerEntry();
    workerEntry.deallocate();

    GridProcessErrorTypes type = null;

    if (executionResult != null && executionResult.getExecutionError() != null) {
      type = executionResult.getExecutionError().getType();
    }

    boolean enteredTaskBlacklist = executionFailedOnWorker(workerEntry, type, execution, responses);

    if (enteredTaskBlacklist) {
      if (!isWorkerNeeded(workerEntry, execution)) {
        unwantWorker(job, workerEntry, responses);
      }
    } else {
      disposeWorker(workerEntry, responses);
    }

    boolean hasJobEnded = hasJobEnded(job);

    String executorMsg = "";

    if (executionResult != null
        && executionResult.getExecutionError() != null
        && executionResult.getExecutionError().getErrorCause() != null) {
      executorMsg = executionResult.getExecutionError().getErrorCause().toString();
    }

    responses.add(
        new LoggerResponseTO(
            "Grid process "
                + execution.getState()
                + " "
                + handle
                + ". Job ended: "
                + hasJobEnded
                + " "
                + executorMsg
                + ".",
            LoggerResponseTO.DEBUG));

    if (hasJobEnded) {
      finishJob(execution.getJob(), responses);
    }

    if (!isJobSatisfied(job) && !hasJobEnded) {

      Request request = execution.getJob().getRequest(workerEntry.getRequestID());
      if (request != null) {
        request.setPaused(false);
      }

      ResumeRequestResponseTO to = new ResumeRequestResponseTO();

      to.setPeerAddress(StringUtil.deploymentIDToAddress(workerEntry.getPeerID()));
      to.setRequestID(workerEntry.getRequestID());

      responses.add(to);
    }

    updateScheduler(responses);
  }