protected void stopScheduler() { try { SchedulerFactory f = DefaultSchedulerFactory.getInstance(); Scheduler s = f.getScheduler(); s.stop(null, null); } catch (IOException ex) { // Log info: { String message = "Failure to stop scheduler!"; log.log(Level.SEVERE, message, ex); } throw new RuntimeException(ex); } catch (Throwable ex) { // Log info: { String message = "Failure to stop scheduler!"; log.log(Level.SEVERE, message, ex); } throw new RuntimeException(ex); } }
public void testExecute(int id, final Map<Thermostat, SortedMap<Period, ZoneStatus>> schedule) { NDC.push("testExecute:" + id); try { ScheduleUpdater u = new ScheduleUpdater() { @Override public Map<Thermostat, SortedMap<Period, ZoneStatus>> update() throws IOException { return schedule; } }; Scheduler s = new Scheduler(u); s.setScheduleGranularity(50); // This instance will run until the JVM is gone or Scheduler#ScheduledExecutorService is // otherwise stopped s.start(0); Thread.sleep(50); s.stop(); } catch (InterruptedException ex) { throw new IllegalStateException(ex); } finally { NDC.pop(); } }
public void testStartStop() { NDC.push("testStartStop"); try { final Semaphore syncLock = new Semaphore(1); ScheduleUpdater u = new ScheduleUpdater() { @Override public Map<Thermostat, SortedMap<Period, ZoneStatus>> update() throws IOException { NDC.push("update"); try { logger.info("started"); syncLock.acquire(); logger.info("got the lock"); // This timeout should be longer than the run timeout so we can test the stop() // properly Thread.sleep(200); logger.info("done"); return new TreeMap<Thermostat, SortedMap<Period, ZoneStatus>>(); } catch (InterruptedException ex) { logger.info("Interrupted", ex); return null; } finally { NDC.pop(); } } }; Scheduler s = new Scheduler(u); s.setScheduleGranularity(50); // Acquire the lock so update() will wait until it is released syncLock.acquire(); s.start(0); // Wait for a bit so update() has a chance to run Thread.sleep(100); logger.info("releasing the lock"); syncLock.release(); // Wait for a bit so update() has a chance to acquire the lock and start waiting Thread.sleep(50); s.stop(); } catch (InterruptedException ex) { throw new IllegalStateException(ex); } finally { NDC.pop(); } }