| | |
| | | import java.util.concurrent.Executors; |
| | | |
| | | public class JobQueue { |
| | | private static final int MAX_DONE_JOBS_SIZE = 50; |
| | | private Logger log = LoggerFactory.getLogger(JobQueue.class); |
| | | private List<AbstractJob> queue = new ArrayList<>(); |
| | | private List<AbstractJob> doneJobs = new LinkedList<>(); |
| | |
| | | return queue.size(); |
| | | } |
| | | |
| | | public List<AbstractJob> getDoneJobs() { |
| | | return Collections.unmodifiableList(doneJobs); |
| | | } |
| | | |
| | | /** |
| | | * Appends the job if not alread in the queue. Starts the execution if no execution thread is already running. |
| | | * |
| | |
| | | it.remove(); |
| | | doneJobs.add(0, job); |
| | | } |
| | | while (doneJobs.size() > MAX_DONE_JOBS_SIZE) { |
| | | doneJobs.remove(doneJobs.size() - 1); |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | log.info("Starting job: " + job.getId()); |
| | | job.setStatus(AbstractJob.Status.RUNNING); |
| | | job.execute(); |
| | | job.setStatus(AbstractJob.Status.DONE); |
| | | if (!job.isFinished()) { |
| | | // Don't overwrite status failed set by job. |
| | | job.setStatus(AbstractJob.Status.DONE); |
| | | } |
| | | } catch (Exception ex) { |
| | | log.error("Error while executing job '" + job.getId() + "': " + ex.getMessage(), ex); |
| | | job.setStatus(AbstractJob.Status.FAILED); |