use of java.util.concurrent.ScheduledExecutorService in project camel by apache.
the class ManagedThrottlerTest method createRouteBuilder.
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
final ScheduledExecutorService badService = new ScheduledThreadPoolExecutor(1) {
@Override
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
throw new RejectedExecutionException();
}
};
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").to("log:foo").throttle(10).id("mythrottler").to("mock:result");
from("seda:throttleCount").throttle(1).timePeriodMillis(250).id("mythrottler2").to("mock:end");
from("seda:throttleCountAsync").throttle(1).asyncDelayed().timePeriodMillis(250).id("mythrottler3").to("mock:endAsync");
from("seda:throttleCountAsyncException").throttle(1).asyncDelayed().timePeriodMillis(250).id("mythrottler4").to("mock:endAsyncException").process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
throw new RuntimeException("Fail me");
}
});
from("seda:throttleCountRejectExecutionCallerRuns").onException(RejectedExecutionException.class).to("mock:rejectedExceptionEndpoint1").end().throttle(1).timePeriodMillis(250).asyncDelayed().executorService(badService).callerRunsWhenRejected(true).id("mythrottler5").to("mock:endAsyncRejectCallerRuns");
from("seda:throttleCountRejectExecution").onException(RejectedExecutionException.class).to("mock:rejectedExceptionEndpoint1").end().throttle(1).timePeriodMillis(250).asyncDelayed().executorService(badService).callerRunsWhenRejected(false).id("mythrottler6").to("mock:endAsyncReject");
}
};
}
use of java.util.concurrent.ScheduledExecutorService in project redisson by redisson.
the class RedissonBoundedBlockingQueueTest method testOfferTimeout.
@Test
public void testOfferTimeout() throws InterruptedException {
RBoundedBlockingQueue<Integer> queue = redisson.getBoundedBlockingQueue("bounded-queue");
queue.trySetCapacity(5);
queue.add(1);
queue.add(2);
queue.add(3);
queue.add(4);
queue.add(5);
long start = System.currentTimeMillis();
assertThat(queue.offer(6, 2, TimeUnit.SECONDS)).isFalse();
assertThat(System.currentTimeMillis() - start).isGreaterThan(1900);
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
final AtomicBoolean executed = new AtomicBoolean();
executor.schedule(new Runnable() {
@Override
public void run() {
RBoundedBlockingQueue<Integer> queue1 = redisson.getBoundedBlockingQueue("bounded-queue");
assertThat(queue1.remove()).isEqualTo(1);
executed.set(true);
}
}, 1, TimeUnit.SECONDS);
start = System.currentTimeMillis();
assertThat(queue.offer(6, 3, TimeUnit.SECONDS)).isTrue();
assertThat(System.currentTimeMillis() - start).isBetween(1000L, 2000L);
await().atMost(2, TimeUnit.SECONDS).until(() -> assertThat(executed.get()).isTrue());
assertThat(queue).containsExactly(2, 3, 4, 5, 6);
executor.shutdown();
assertThat(executor.awaitTermination(1, TimeUnit.MINUTES)).isTrue();
}
use of java.util.concurrent.ScheduledExecutorService in project hive by apache.
the class ReplChangeManager method scheduleCMClearer.
// Schedule CMClearer thread. Will be invoked by metastore
public static void scheduleCMClearer(HiveConf hiveConf) {
if (HiveConf.getBoolVar(hiveConf, HiveConf.ConfVars.REPLCMENABLED)) {
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new BasicThreadFactory.Builder().namingPattern("cmclearer-%d").daemon(true).build());
executor.scheduleAtFixedRate(new CMClearer(hiveConf.get(HiveConf.ConfVars.REPLCMDIR.varname), hiveConf.getTimeVar(ConfVars.REPLCMRETIAN, TimeUnit.SECONDS), hiveConf), 0, hiveConf.getTimeVar(ConfVars.REPLCMINTERVAL, TimeUnit.SECONDS), TimeUnit.SECONDS);
}
}
use of java.util.concurrent.ScheduledExecutorService in project storm by apache.
the class FileBasedEventLogger method setUpFlushTask.
private void setUpFlushTask() {
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
Runnable task = new Runnable() {
@Override
public void run() {
try {
if (dirty) {
eventLogWriter.flush();
dirty = false;
}
} catch (IOException ex) {
LOG.error("Error flushing " + eventLogPath, ex);
throw new RuntimeException(ex);
}
}
};
scheduler.scheduleAtFixedRate(task, FLUSH_INTERVAL_MILLIS, FLUSH_INTERVAL_MILLIS, TimeUnit.MILLISECONDS);
}
use of java.util.concurrent.ScheduledExecutorService in project elasticsearch by elastic.
the class CacheTests method testDependentKeyDeadlock.
public void testDependentKeyDeadlock() throws BrokenBarrierException, InterruptedException {
class Key {
private final int key;
Key(int key) {
this.key = key;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Key key1 = (Key) o;
return key == key1.key;
}
@Override
public int hashCode() {
return key % 2;
}
}
int numberOfThreads = randomIntBetween(2, 32);
final Cache<Key, Integer> cache = CacheBuilder.<Key, Integer>builder().build();
CopyOnWriteArrayList<ExecutionException> failures = new CopyOnWriteArrayList<>();
CyclicBarrier barrier = new CyclicBarrier(1 + numberOfThreads);
CountDownLatch deadlockLatch = new CountDownLatch(numberOfThreads);
List<Thread> threads = new ArrayList<>();
for (int i = 0; i < numberOfThreads; i++) {
Thread thread = new Thread(() -> {
try {
try {
barrier.await();
} catch (BrokenBarrierException | InterruptedException e) {
throw new AssertionError(e);
}
Random random = new Random(random().nextLong());
for (int j = 0; j < numberOfEntries; j++) {
Key key = new Key(random.nextInt(numberOfEntries));
try {
cache.computeIfAbsent(key, k -> {
if (k.key == 0) {
return 0;
} else {
Integer value = cache.get(new Key(k.key / 2));
return value != null ? value : 0;
}
});
} catch (ExecutionException e) {
failures.add(e);
break;
}
}
} finally {
// successfully avoided deadlock, release the main thread
deadlockLatch.countDown();
}
});
threads.add(thread);
thread.start();
}
AtomicBoolean deadlock = new AtomicBoolean();
assert !deadlock.get();
// start a watchdog service
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(() -> {
Set<Long> ids = threads.stream().map(t -> t.getId()).collect(Collectors.toSet());
ThreadMXBean mxBean = ManagementFactory.getThreadMXBean();
long[] deadlockedThreads = mxBean.findDeadlockedThreads();
if (!deadlock.get() && deadlockedThreads != null) {
for (long deadlockedThread : deadlockedThreads) {
// ensure that we detected deadlock on our threads
if (ids.contains(deadlockedThread)) {
deadlock.set(true);
// release the main test thread to fail the test
for (int i = 0; i < numberOfThreads; i++) {
deadlockLatch.countDown();
}
break;
}
}
}
}, 1, 1, TimeUnit.SECONDS);
// everything is setup, release the hounds
barrier.await();
// wait for either deadlock to be detected or the threads to terminate
deadlockLatch.await();
// shutdown the watchdog service
scheduler.shutdown();
assertThat(failures, is(empty()));
assertFalse("deadlock", deadlock.get());
}
Aggregations