@SuppressWarnings("unchecked") protected void schedulePublish(long delay) { if (isRunning() && executorQueued.compareAndSet(false, true)) { long now = System.currentTimeMillis(); delay = Math.max(0, Math.max(delay, (executorTime + MIN_PERIOD_BETWEEN_EXECS_MILLIS) - now)); if (LOG.isTraceEnabled()) LOG.trace("{} scheduling publish in {}ms", this, delay); Runnable job = new PublishJob(); ScheduledTask task = new ScheduledTask(MutableMap.of("delay", Duration.of(delay, TimeUnit.MILLISECONDS)), new BasicTask<Void>(job)); ((EntityInternal)entity).getExecutionContext().submit(task); } }
@Test public void testScheduledTaskExecutedAfterDelay() throws Exception { int delay = 100; final CountDownLatch latch = new CountDownLatch(1); Callable<Task<?>> taskFactory = new Callable<Task<?>>() { @Override public Task<?> call() { return new BasicTask<Void>(new Runnable() { @Override public void run() { latch.countDown(); }}); }}; ScheduledTask t = new ScheduledTask(taskFactory).delay(delay); Stopwatch stopwatch = Stopwatch.createStarted(); em.submit(t); assertTrue(latch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)); long actualDelay = stopwatch.elapsed(TimeUnit.MILLISECONDS); assertTrue(actualDelay > (delay-EARLY_RETURN_GRACE), "actualDelay="+actualDelay+"; delay="+delay); assertTrue(actualDelay < (delay+MAX_OVERHEAD_MS), "actualDelay="+actualDelay+"; delay="+delay); }
ScheduledTask task = new ScheduledTask(MutableMap.of("delay", Duration.of(delay, TimeUnit.MILLISECONDS)), new BasicTask(job)); ((EntityInternal)entity).getExecutionContext().submit(task);
@Test public void testCanCancelScheduledTask() throws Exception { final int period = 1; final long checkPeriod = 250; final List<Long> timestamps = Collections.synchronizedList(Lists.<Long>newArrayList()); Callable<Task<?>> taskFactory = new Callable<Task<?>>() { @Override public Task<?> call() { return new BasicTask<Void>(new Runnable() { @Override public void run() { timestamps.add(System.currentTimeMillis()); }}); }}; ScheduledTask t = new ScheduledTask(taskFactory).period(period); em.submit(t); t.cancel(); long cancelTime = System.currentTimeMillis(); int countImmediatelyAfterCancel = timestamps.size(); Thread.sleep(checkPeriod); int countWellAfterCancel = timestamps.size(); // should have at most 1 more execution after cancel log.info("testCanCancelScheduledTask saw "+countImmediatelyAfterCancel+" then cancel then "+countWellAfterCancel+" total"); assertTrue(countWellAfterCancel - countImmediatelyAfterCancel <= 2, "timestamps="+timestamps+"; cancelTime="+cancelTime); }
@Test public void testScheduledTaskResubmittedIfExceptionThrownAndCancelOnExceptionFalse() { BasicExecutionManager m = new BasicExecutionManager("mycontextid"); final AtomicInteger calls = new AtomicInteger(0); ScheduledTask t = new ScheduledTask(MutableMap.of("period", Duration.ONE_MILLISECOND, "maxIterations", 5, "cancelOnException", false), new Callable<Task<?>>() { @Override public Task<?> call() throws Exception { return new BasicTask<>(new Callable<Integer>() { @Override public Integer call() { calls.incrementAndGet(); throw new RuntimeException("boo"); }}); }}); m.submit(t); t.blockUntilEnded(); assertEquals(calls.get(), 5, "Expected task to be resubmitted despite throwing an exception"); }
@Test public void testScheduledTaskExecutedAtRegularPeriod() throws Exception { final int period = 100; final int numTimestamps = 4; final CountDownLatch latch = new CountDownLatch(1); final List<Long> timestamps = Collections.synchronizedList(Lists.<Long>newArrayList()); final Stopwatch stopwatch = Stopwatch.createStarted(); Callable<Task<?>> taskFactory = new Callable<Task<?>>() { @Override public Task<?> call() { return new BasicTask<Void>(new Runnable() { @Override public void run() { timestamps.add(stopwatch.elapsed(TimeUnit.MILLISECONDS)); if (timestamps.size() >= numTimestamps) latch.countDown(); }}); }}; ScheduledTask t = new ScheduledTask(taskFactory).delay(1).period(period); em.submit(t); assertTrue(latch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)); synchronized (timestamps) { long prev = timestamps.get(0); for (long timestamp : timestamps.subList(1, timestamps.size())) { assertTrue(timestamp > prev+period-EARLY_RETURN_GRACE, "timestamps="+timestamps); assertTrue(timestamp < prev+period+MAX_OVERHEAD_MS, "timestamps="+timestamps); prev = timestamp; } } }
public void start() { synchronized (startStopMutex) { if (state==ListenerState.RUNNING || (scheduledTask!=null && !scheduledTask.isDone())) { LOG.warn("Request to start "+this+" when already running - "+scheduledTask+"; ignoring"); return; } state = ListenerState.RUNNING; Callable<Task<?>> taskFactory = new Callable<Task<?>>() { @Override public Task<Void> call() { return Tasks.<Void>builder().dynamic(false).displayName("periodic-persister").body(new Callable<Void>() { @Override public Void call() { persistNowSafely(); return null; }}).build(); } }; scheduledTask = (ScheduledTask) executionContext.submit(new ScheduledTask(MutableMap.of("displayName", "scheduled[periodic-persister]", "tags", MutableSet.of(BrooklynTaskTags.TRANSIENT_TASK_TAG)), taskFactory).period(period).delay(period)); } }
@SuppressWarnings("unchecked") protected void doStartPolling() { if (scheduledTask == null || scheduledTask.isDone()) { ScheduledTask task = new ScheduledTask(MutableMap.of("period", getPollPeriod(), "displayName", getTaskName()), pollingTaskFactory); scheduledTask = ((EntityInternal)entity).getExecutionContext().submit(task); } }
new ScheduledTask(MutableMap.of("displayName", "Periodic read-only rebind"), taskFactory).period(periodicPersistPeriod));
ScheduledTask task = new ScheduledTask(taskFlags, pollingTaskFactory) .period(pollJob.pollPeriod) .cancelOnException(false);
if (pollingTask!=null) pollingTask.cancel(true); ScheduledTask task = new ScheduledTask(MutableMap.of("period", pollPeriod, "displayName", "scheduled:[HA poller task]"), taskFactory); pollingTask = managementContext.getExecutionManager().submit(task);
final List<Long> execTimes = new CopyOnWriteArrayList<Long>(); ScheduledTask t = new ScheduledTask(MutableMap.of("delay", PERIOD, "period", PERIOD), new Callable<Task<?>>() { @Override public Task<?> call() throws Exception {
@Test public void testScheduledTask() throws Exception { int PERIOD = 20; BasicExecutionManager m = new BasicExecutionManager("mycontextid"); final AtomicInteger i = new AtomicInteger(0); ScheduledTask t = new ScheduledTask(MutableMap.of("delay", 2*PERIOD, "period", PERIOD, "maxIterations", 5), new Callable<Task<?>>() { @Override public Task<?> call() throws Exception { return new BasicTask<Integer>(new Callable<Integer>() { @Override public Integer call() { log.debug("task running: "+Tasks.current()+" "+Tasks.current().getStatusDetail(false)); return i.incrementAndGet(); }}); }}); log.info("submitting {} {}", t, t.getStatusDetail(false)); m.submit(t); log.info("submitted {} {}", t, t.getStatusDetail(false)); Integer interimResult = (Integer) t.get(); log.info("done one ({}) {} {}", new Object[] {interimResult, t, t.getStatusDetail(false)}); assertTrue(i.get() > 0, "i="+i); t.blockUntilEnded(); Integer finalResult = (Integer) t.get(); log.info("ended ({}) {} {}", new Object[] {finalResult, t, t.getStatusDetail(false)}); assertEquals(finalResult, (Integer)5); assertEquals(i.get(), 5); }
ScheduledTask scheduledTask = new ScheduledTask(flags, new BasicTask<Void>(job)); getExecutionContext().submit(scheduledTask);
BasicExecutionManager m = new BasicExecutionManager("mycontextid"); final AtomicInteger i = new AtomicInteger(0); ScheduledTask t = new ScheduledTask(MutableMap.of("delay", 2*PERIOD, "period", PERIOD), new Callable<Task<?>>() { @Override public Task<?> call() throws Exception {
cleanupTask = getManagementContext().getExecutionManager().submit(new ScheduledTask( MutableMap.of("displayName", "scheduled[ssh-location cache cleaner]"), cleanupTaskFactory) .period(expiryDuration)
BasicExecutionManager m = new BasicExecutionManager("mycontextid"); final AtomicInteger i = new AtomicInteger(); ScheduledTask t = new ScheduledTask(MutableMap.of("delay", PERIOD.times(2), "period", PERIOD), new Callable<Task<?>>() { @Override public Task<?> call() throws Exception {
@Test public void testScheduledTaskCancelledIfExceptionThrown() throws Exception { BasicExecutionManager m = new BasicExecutionManager("mycontextid"); final AtomicInteger calls = new AtomicInteger(0); ScheduledTask t = new ScheduledTask(MutableMap.of("period", Duration.ONE_MILLISECOND, "maxIterations", 5), new Callable<Task<?>>() { @Override public Task<?> call() throws Exception { return new BasicTask<>(new Callable<Integer>() { @Override public Integer call() { calls.incrementAndGet(); throw new RuntimeException("boo"); }}); }}); m.submit(t); Runnable callsIsOne = new Runnable() { @Override public void run() { if (calls.get() != 1) { throw new RuntimeException("not yet"); } } }; Asserts.succeedsEventually(callsIsOne); Asserts.succeedsContinually(callsIsOne); }
final Semaphore interruptedSemaphore = new Semaphore(0); final AtomicInteger i = new AtomicInteger(); ScheduledTask t = new ScheduledTask(MutableMap.of("delay", PERIOD.times(2), "period", PERIOD), new Callable<Task<?>>() { @Override public Task<?> call() throws Exception {
ScheduledTask t = new ScheduledTask(MutableMap.of("delay", PERIOD.times(2), "period", PERIOD), new Callable<Task<?>>() { @Override public Task<?> call() throws Exception {