// WARN Watch this code, see if we can do better, maybe leverage @UnitOfWorkRetry @Override public void run() { System.out.println("Running Schedule"); Usecase usecase = UsecaseBuilder.newUsecase("ScheduleRunner"); UnitOfWork uow = module.newUnitOfWork(usecase); try { Schedule schedule = uow.get(Schedule.class, this.schedule.scheduleIdentity); Task task = schedule.task().get(); schedule = uow.get(Schedule.class, this.schedule.scheduleIdentity); try { schedule.taskStarting(); task.run(); schedule.taskCompletedSuccessfully(); } catch (RuntimeException ex) { schedule.taskCompletedWithException(ex); } schedulerMixin.dispatchForExecution(schedule); uow.complete(); } catch (UnitOfWorkCompletionException ex) { } finally { // What should we do if we can't manage the Running flag?? if (uow.isOpen()) { uow.discard(); } } }
private void dispatchForExecution(Schedule schedule) { long now = System.currentTimeMillis(); synchronized (timingQueue) { if (timingQueue.size() == 0) { long nextRun = schedule.nextRun(now); if (nextRun < 0) { return; } System.out.println("Next run at: " + new DateTime(nextRun)); timingQueue.add(new ScheduleTime(schedule.identity().get(), nextRun)); if (scheduleHandler == null) { dispatchHandler(); } } else { ScheduleTime first = timingQueue.first(); long nextRun = schedule.nextRun(now); if (nextRun < 0) { return; } System.out.println("Next run at: " + new DateTime(nextRun)); timingQueue.add(new ScheduleTime(schedule.identity().get(), nextRun)); ScheduleTime newFirst = timingQueue.first(); if (!first.equals(newFirst)) { // We need to restart the managementThread, which is currently waiting for a 'later' event // to // occur than the one that was just scheduled. scheduleHandler.future.cancel(true); dispatchHandler(); } } } }