Example #1
0
  /**
   * Add a job output listener for the current instance of the job
   *
   * @param listener the listener
   */
  public synchronized void addJobOutputListenerToCurrent(JobOutputListener listener) {

    // TODO This method has several possible race conditions, but they are all quite rare

    ExecutionThread t = current;
    if (t == null) {
      if (last != null) {
        listener.jobOutput(last.output.toString());
      }
      return;
    }

    while (t.newOutputListener != null) {
      Thread.yield();
      if (t != current) {
        if (last != null) {
          listener.jobOutput(last.output.toString());
        }
        return;
      }
    }

    String output = t.output.toString();
    listener.jobOutput(output);
    t.newOutputCharsSent = output.length();

    t.newOutputListener = listener;
  }
Example #2
0
  /**
   * This main loop iterates over the submitted jobs and creates the corresponding threads that will
   * carry the execution of each job segment.
   */
  @Override
  public void run() {
    try {
      int jobno = 0;

      while (true) {

        sleep(20);
        synchronized (jobs) {
          if (jobs.size() == 0) {

            continue;
          }
        }

        TnrsJob job = jobs.get(jobno % jobs.size());
        if (job.status().equalsIgnoreCase("idle")) {
          ExecutionThread thread = new ExecutionThread(job);
          threads.add(thread);
          thread.start();
        } else if (job.status().equalsIgnoreCase("stopped")) {
          jobs.remove(job);
          JobHelper.cleanJobData(baseFolder, job);
          jobno = 0;
          continue;
        } else if (job.status().equals("failed")) {
          sendFailedJobEmail(job);
          job.setStatus("error");
        }
        jobno++;

        if (jobs.size() == 200) {

          for (int i = 0; i < threads.size(); i++) {
            threads.elementAt(i).join();
          }
          jobno = 0;
          threads.clear();
        }

        sleep(10);
      }

    } catch (Exception e) {
      log.error(ExceptionUtils.getFullStackTrace(e));
      e.printStackTrace();
    }
  }
Example #3
0
  /** Start the job and return immediately */
  public synchronized void start() {

    if (current != null) {
      throw new IllegalStateException("The job is already running");
    }

    executionTime = new Date();

    current = new ExecutionThread();
    current.start();
  }
Example #4
0
 /**
  * Join the execution of the thread
  *
  * @return true if the job was joined, false otherwise
  * @throws InterruptedException if interrupted
  */
 public boolean join() throws InterruptedException {
   ExecutionThread t = current;
   if (t == null) return false;
   t.join();
   return true;
 }