@SuppressWarnings("unchecked") public void restoreFromMemento(OpenmrsMemento memento) { if (memento != null && memento instanceof TimerSchedulerMemento) { TimerSchedulerMemento timerMemento = (TimerSchedulerMemento) memento; Set<Integer> taskIds = (HashSet<Integer>) timerMemento.getState(); // try to start all of the tasks that were stopped right before this restore for (Integer taskId : taskIds) { TaskDefinition task = getTask(taskId); try { scheduleTask(task); } catch (Exception e) { // essentially swallow exceptions log.debug( "EXPECTED ERROR IF STOPPING THIS TASK'S MODULE: Unable to start task " + taskId, e); // save this errored task and try again next time we restore timerMemento.addErrorTask(taskId); } } timerMemento = null; // so the old cl can be gc'd } }
/** * Saves and stops all active tasks * * @return OpenmrsMemento */ public OpenmrsMemento saveToMemento() { Set<Integer> tasks = new HashSet<Integer>(); for (TaskDefinition task : getScheduledTasks()) { tasks.add(task.getId()); try { shutdownTask(task); } catch (SchedulerException e) { // just swallow exceptions log.debug("Failed to stop task while saving memento " + task.getName(), e); } } TimerSchedulerMemento memento = new TimerSchedulerMemento(tasks); memento.saveErrorTasks(); return memento; }