@Override public void stopReadOnly() { readOnlyRunning = false; if (readOnlyTask!=null) { LOG.debug("Stopping read-only rebinding ("+this+"), mgmt "+managementContext.getManagementNodeId()); readOnlyTask.cancel(true); readOnlyTask.blockUntilEnded(); boolean reallyEnded = Tasks.blockUntilInternalTasksEnded(readOnlyTask, Duration.TEN_SECONDS); if (!reallyEnded) { LOG.warn("Rebind (read-only) tasks took too long to die after interrupt (ignoring): "+readOnlyTask); } readOnlyTask = null; LOG.debug("Stopped read-only rebinding ("+this+"), mgmt "+managementContext.getManagementNodeId()); } }
@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 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); }
log.info("done one ({}) {} {}", new Object[] {interimResult, t, t.getStatusDetail(false)}); assertTrue(i.get() > 0); t.blockUntilEnded(); Integer finalResult = (Integer) t.get(); log.info("ended ({}) {} {}", new Object[] {finalResult, t, t.getStatusDetail(false)});
log.info("done one ({}) {} {}", new Object[] {interimResult, t, t.getStatusDetail(false)}); assertTrue(i.get() > 0); t.blockUntilEnded();
Stopwatch timer = Stopwatch.createUnstarted(); t.cancel(true); t.blockUntilEnded();
Stopwatch timer = Stopwatch.createUnstarted(); t.cancel(true); t.blockUntilEnded();
void stop(Duration timeout, Duration graceTimeoutForSubsequentOperations) { synchronized (startStopMutex) { state = ListenerState.STOPPING; try { if (scheduledTask != null) { CountdownTimer expiry = timeout.countdownTimer(); try { scheduledTask.cancel(false); waitForPendingComplete(expiry.getDurationRemaining().lowerBound(Duration.ZERO).add(graceTimeoutForSubsequentOperations), true); } catch (Exception e) { throw Exceptions.propagate(e); } scheduledTask.blockUntilEnded(expiry.getDurationRemaining().lowerBound(Duration.ZERO).add(graceTimeoutForSubsequentOperations)); scheduledTask.cancel(true); boolean reallyEnded = Tasks.blockUntilInternalTasksEnded(scheduledTask, expiry.getDurationRemaining().lowerBound(Duration.ZERO).add(graceTimeoutForSubsequentOperations)); if (!reallyEnded) { LOG.warn("Persistence tasks took too long to terminate, when stopping persistence, although pending changes were persisted (ignoring): "+scheduledTask); } scheduledTask = null; } // Discard all state that was waiting to be persisted synchronized (this) { deltaCollector = new DeltaCollector(); } } finally { state = ListenerState.STOPPED; } } }