@Override public void onJobStarted() { super.onJobStarted(); progressPoller = new JobProgressPoller(this, progressUpdateIntervalMs); progressPoller.start(); }
@Override public void onJobEnded() { super.onJobEnded(); if (this.progressPoller != null) { this.progressPoller.interrupt(); } } }
public static ExecutorFactory singleton() { if (instance == null) { synchronized (_executorLock) { if (instance == null) { instance = new ExecutorFactory(); } } } return instance; }
public void run() { try { onJobStarted(); completeWithSuccess(jobRun()); } catch (Throwable e) { LOGGER.error("Job failed", e); completeWithError(e); } finally { onJobEnded(); } }
private synchronized void completeWithError(Throwable error) { setException(error); errorMessage = getJobExceptionStack(error); }
@Override public void stop() { terminate = true; for (Job job : queue) { job.aborted = true; job.jobAbort(); } }
FIFOScheduler(String name) { super(name); executor = Executors.newSingleThreadExecutor( new SchedulerThreadFactory("FIFOScheduler-Worker-")); }
public void setStatus(Status status) { if (this.status == status) { return; } Status before = this.status; Status after = status; this.status = status; if (listener != null && before != null && before != after) { listener.onStatusChange(this, before, after); } }
private synchronized void completeWithSuccess(T result) { setResult(result); exception = null; errorMessage = null; }
public boolean isRunning() { return this.status.isRunning(); }
public Job(String jobId, String jobName, JobListener listener) { this.jobName = jobName; this.listener = listener; dateCreated = new Date(); id = jobId; setStatus(Status.READY); }
public JobProgressPoller(Job job, long intervalMs) { super("JobProgressPoller, jobId=" + job.getId()); this.job = job; if (intervalMs < 0) { throw new IllegalArgumentException("polling interval can't be " + intervalMs); } this.intervalMs = intervalMs == 0 ? DEFAULT_INTERVAL_MSEC : intervalMs; }
@Override public Job cancel(String jobId) { Job job = jobs.remove(jobId); job.abort(); return job; }
public void shutdownAll() { synchronized (executors) { for (String name : executors.keySet()) { shutdown(name); } } } }
@Override public void runJobInScheduler(final Job runningJob) { // submit this job to a FixedThreadPool so that at most maxConcurrencyJobs running executor.execute(() -> runJob(runningJob)); } }
@Override public void runJobInScheduler(final Job job) { // run job in the SingleThreadExecutor since this is FIFO. executor.execute(() -> runJob(job)); } }
public void abort() { aborted = jobAbort(); }
ParallelScheduler(String name, int maxConcurrency) { super(name); this.executor = Executors.newFixedThreadPool(maxConcurrency, new SchedulerThreadFactory("ParallelScheduler-Worker-")); }