@Override public NetworkHealthCheck setPeriod(long period) { super.setPeriod(period); return this; }
@Override public NetworkHealthCheck setTimeUnit(TimeUnit timeUnit) { super.setTimeUnit(timeUnit); return this; }
@Test public void testUsingOwnExecutors() throws Exception { final CountDownLatch latch = new CountDownLatch(1); final ActiveMQScheduledComponent local = new ActiveMQScheduledComponent(10, TimeUnit.MILLISECONDS, false) { @Override public void run() { latch.countDown(); } }; local.start(); local.start(); // should be ok to call start again try { Assert.assertTrue(latch.await(10, TimeUnit.SECONDS)); // re-scheduling the executor at a big interval.. // just to make sure it won't hung local.setTimeUnit(TimeUnit.HOURS); local.setPeriod(1); } finally { local.stop(); local.stop(); // should be ok to call stop again } }
@Test public void testUsingCustomInitialDelay() throws InterruptedException { final CountDownLatch latch = new CountDownLatch(1); final long initialDelayMillis = 100; final long checkPeriodMillis = 100 * initialDelayMillis; final ActiveMQScheduledComponent local = new ActiveMQScheduledComponent(scheduledExecutorService, executorService, initialDelayMillis, checkPeriodMillis, TimeUnit.MILLISECONDS, false) { @Override public void run() { latch.countDown(); } }; final long start = System.nanoTime(); local.start(); try { final boolean triggeredBeforePeriod = latch.await(local.getPeriod(), local.getTimeUnit()); final long timeToFirstTrigger = TimeUnit.NANOSECONDS.convert(System.nanoTime() - start, local.getTimeUnit()); Assert.assertTrue("Takes too long to start", triggeredBeforePeriod); Assert.assertTrue("Started too early", timeToFirstTrigger >= local.getInitialDelay()); } finally { local.stop(); } } }
@Test public void testVerifyInitialDelayChanged() { final long initialDelay = 10; final long period = 100; final ActiveMQScheduledComponent local = new ActiveMQScheduledComponent(scheduledExecutorService, executorService, initialDelay, period, TimeUnit.MILLISECONDS, false) { @Override public void run() { } }; local.start(); final long newInitialDelay = 1000; //the parameters are valid? assert initialDelay != newInitialDelay && newInitialDelay != period; local.setInitialDelay(newInitialDelay); local.stop(); Assert.assertEquals("the initial dalay can't change", newInitialDelay, local.getInitialDelay()); }
/** * Useful to change a running schedule and avoid multiple restarts. */ public synchronized ActiveMQScheduledComponent setInitialDelayAndPeriod(long initialDelay, long period, TimeUnit timeUnit) { this.period = period; this.initialDelay = initialDelay; this.timeUnit = timeUnit; restartIfNeeded(); return this; }
@Override public void stop() { scheduledComponent.stop(); }
@Override public void start() { scheduledComponent.start(); }
@Test public void testAccumulationOwnPool() throws Exception { final AtomicInteger count = new AtomicInteger(0); final ActiveMQScheduledComponent local = new ActiveMQScheduledComponent(100, TimeUnit.MILLISECONDS, false) { @Override public void run() { if (count.get() == 0) { try { Thread.sleep(500); } catch (Exception e) { } } count.incrementAndGet(); } }; local.start(); Thread.sleep(1000); local.stop(); Assert.assertTrue("just because one took a lot of time, it doesn't mean we can accumulate many, we got " + count + " executions", count.get() <= 5 && count.get() > 0); }
@Override public boolean isStarted() { return scheduledComponent.isStarted(); } }
protected ActiveMQThreadFactory getThreadFactory() { return new ActiveMQThreadFactory(this.getClass().getSimpleName() + "-scheduled-threads", false, getThisClassLoader()); }
@Override public void run() { if (onDemand && delayed.get() > 0) { delayed.decrementAndGet(); } if (!onDemand && lastTime > 0) { if (System.currentTimeMillis() - lastTime < millisecondsPeriod) { logger.trace("Execution ignored due to too many simultaneous executions, probably a previous delayed execution"); return; } } lastTime = System.currentTimeMillis(); ActiveMQScheduledComponent.this.run(); } };
@Override public synchronized void start() { if (future != null) { // already started return; } if (scheduledExecutorService == null) { scheduledExecutorService = new ScheduledThreadPoolExecutor(1, getThreadFactory()); startedOwnScheduler = true; } if (onDemand) { return; } this.millisecondsPeriod = timeUnit.convert(period, TimeUnit.MILLISECONDS); if (period >= 0) { future = scheduledExecutorService.scheduleWithFixedDelay(runForScheduler, initialDelay >= 0 ? initialDelay : period, period, timeUnit); } else { logger.tracef("did not start scheduled executor on %s because period was configured as %d", this, period); } }
@Test public void testUsingOwnExecutorsOnDemand() throws Throwable { final ReusableLatch latch = new ReusableLatch(1); final ActiveMQScheduledComponent local = new ActiveMQScheduledComponent(10, TimeUnit.MILLISECONDS, true) { @Override public void run() { latch.countDown(); } }; local.start(); local.start(); // should be ok to call start again try { Assert.assertFalse(latch.await(20, TimeUnit.MILLISECONDS)); local.delay(); Assert.assertTrue(latch.await(20, TimeUnit.MILLISECONDS)); latch.setCount(1); Assert.assertFalse(latch.await(20, TimeUnit.MILLISECONDS)); // re-scheduling the executor at a big interval.. // just to make sure it won't hung local.setTimeUnit(TimeUnit.HOURS); local.setPeriod(1); } finally { local.stop(); local.stop(); // calling stop again should not be an issue. } }
public synchronized ActiveMQScheduledComponent setPeriod(long period, TimeUnit unit) { this.period = period; this.timeUnit = unit; restartIfNeeded(); return this; }
@Override public void stop() { scheduledComponent.stop(); }
@Override public void start() { scheduledComponent.start(); }
@Test public void testAccumulation() throws Exception { final AtomicInteger count = new AtomicInteger(0); final ActiveMQScheduledComponent local = new ActiveMQScheduledComponent(scheduledExecutorService, executorService, 100, TimeUnit.MILLISECONDS, false) { @Override public void run() { if (count.get() == 0) { try { Thread.sleep(800); } catch (Exception e) { } } count.incrementAndGet(); } }; local.start(); Thread.sleep(1000); local.stop(); Assert.assertTrue("just because one took a lot of time, it doesn't mean we can accumulate many, we got " + count + " executions", count.get() < 5); }
@Override public boolean isStarted() { return scheduledComponent.isStarted(); } }