use of java.util.concurrent.ScheduledFuture in project distributedlog by twitter.
the class TestNonBlockingReads method testNonBlockingReadRecovery.
@Test(timeout = 100000)
public void testNonBlockingReadRecovery() throws Exception {
String name = "distrlog-non-blocking-reader-recovery";
final DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
confLocal.loadConf(conf);
confLocal.setReadAheadBatchSize(10);
confLocal.setReadAheadMaxRecords(10);
final DistributedLogManager dlm = createNewDLM(confLocal, name);
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
ScheduledFuture writerClosedFuture = null;
try {
final Thread currentThread = Thread.currentThread();
writerClosedFuture = executor.schedule(new Runnable() {
@Override
public void run() {
try {
writeRecordsForNonBlockingReads(confLocal, dlm, true);
} catch (Exception exc) {
currentThread.interrupt();
}
}
}, 100, TimeUnit.MILLISECONDS);
readNonBlocking(dlm, false);
assertFalse(currentThread.isInterrupted());
} finally {
if (writerClosedFuture != null) {
// ensure writer.closeAndComplete is done before we close dlm
writerClosedFuture.get();
}
executor.shutdown();
dlm.close();
}
}
use of java.util.concurrent.ScheduledFuture in project graylog2-server by Graylog2.
the class PeriodicalsService method shutDown.
@Override
protected void shutDown() throws Exception {
for (Periodical periodical : periodicals.getAllStoppedOnGracefulShutdown()) {
LOG.info("Shutting down periodical [{}].", periodical.getClass().getCanonicalName());
Stopwatch s = Stopwatch.createStarted();
// Cancel future executions.
Map<Periodical, ScheduledFuture> futures = periodicals.getFutures();
if (futures.containsKey(periodical)) {
futures.get(periodical).cancel(false);
s.stop();
LOG.info("Shutdown of periodical [{}] complete, took <{}ms>.", periodical.getClass().getCanonicalName(), s.elapsed(TimeUnit.MILLISECONDS));
} else {
LOG.error("Could not find periodical [{}] in futures list. Not stopping execution.", periodical.getClass().getCanonicalName());
}
}
}
use of java.util.concurrent.ScheduledFuture in project camel by apache.
the class AmazonSQSClientMock method scheduleCancelInflight.
/*
* Cancel (put back onto queue) in flight messages if the visibility time has expired
* and has not been manually deleted (ack'd)
*/
private void scheduleCancelInflight(final String queueUrl, final Message message) {
if (scheduler != null) {
int visibility = getVisibilityForQueue(queueUrl);
if (visibility > 0) {
ScheduledFuture task = scheduler.schedule(new Runnable() {
@Override
public void run() {
synchronized (messages) {
// put it back!
messages.add(message);
}
}
}, visibility, TimeUnit.SECONDS);
inFlight.put(message.getReceiptHandle(), task);
}
}
}
use of java.util.concurrent.ScheduledFuture in project geode by apache.
the class ScheduledThreadPoolExecutorWithKeepAliveJUnitTest method testRepeatedExecution.
@Test
public void testRepeatedExecution() throws InterruptedException {
ex = new ScheduledThreadPoolExecutorWithKeepAlive(50, 1, TimeUnit.SECONDS, Executors.defaultThreadFactory());
final AtomicInteger counter = new AtomicInteger();
Runnable run = new Runnable() {
public void run() {
counter.incrementAndGet();
}
};
ScheduledFuture f = ex.scheduleAtFixedRate(run, 0, 1, TimeUnit.SECONDS);
Awaitility.await().atMost(30, TimeUnit.SECONDS).until(() -> assertEquals("Task was not executed repeatedly", true, counter.get() > 1));
Awaitility.await().atMost(30, TimeUnit.SECONDS).until(() -> assertEquals("The task could not be cancelled", true, f.cancel(true)));
Awaitility.await().atMost(30, TimeUnit.SECONDS).until(() -> assertEquals("Task was not cancelled within 30 sec", true, f.isCancelled()));
int oldValue = counter.get();
Thread.sleep(5000);
assertEquals("Task was not cancelled", oldValue, counter.get());
}
use of java.util.concurrent.ScheduledFuture in project geode by apache.
the class ScheduledThreadPoolExecutorWithKeepAlive method scheduleWithFixedDelay.
public ScheduledFuture scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
DelegatingScheduledFuture future = new DelegatingScheduledFuture(command, null, true);
ScheduledFuture timerFuture = timer.scheduleWithFixedDelay(new HandOffTask(future), initialDelay, delay, unit);
future.setDelegate(timerFuture);
return future;
}
Aggregations