Ejemplo n.º 1
0
  public int concurrentJobs(Class jobClass) {
    int concurrent = 0;

    for (final SystemJob job : jobs.values()) {
      if (job.getClass().equals(jobClass)) {
        concurrent += 1;
      }
    }

    return concurrent;
  }
Ejemplo n.º 2
0
  public String submitWithDelay(final SystemJob job, final long delay, TimeUnit timeUnit)
      throws SystemJobConcurrencyException {
    // for immediate jobs, check allowed concurrency right now
    if (delay == 0) {
      checkAllowedConcurrency(job);
    }

    final String jobClass = job.getClass().getCanonicalName();

    job.setId(new UUID().toString());
    jobs.put(job.getId(), job);

    executor.schedule(
        new Runnable() {
          @Override
          public void run() {
            try {
              if (delay > 0) {
                checkAllowedConcurrency(job);
              }
              job.markStarted();

              final Stopwatch x = Stopwatch.createStarted();

              job.execute(); // ... blocks until it finishes.
              x.stop();

              final String msg =
                  "SystemJob <"
                      + job.getId()
                      + "> ["
                      + jobClass
                      + "] finished in "
                      + x.elapsed(TimeUnit.MILLISECONDS)
                      + "ms.";
              LOG.info(msg);
              activityWriter.write(new Activity(msg, SystemJobManager.class));
            } catch (SystemJobConcurrencyException ignored) {
            } finally {
              jobs.remove(job.getId());
            }
          }
        },
        delay,
        timeUnit);

    LOG.info("Submitted SystemJob <{}> [{}]", job.getId(), jobClass);
    return job.getId();
  }
Ejemplo n.º 3
0
  protected void checkAllowedConcurrency(SystemJob job) throws SystemJobConcurrencyException {
    final int concurrent = concurrentJobs(job.getClass());

    if (concurrent >= job.maxConcurrency()) {
      throw new SystemJobConcurrencyException(
          "The maximum of parallel ["
              + job.getClass().getCanonicalName()
              + "] is locked "
              + "to <"
              + job.maxConcurrency()
              + "> but <"
              + concurrent
              + "> are running.");
    }
  }