use of java.util.concurrent.ThreadPoolExecutor in project camel by apache.
the class ThreadsRejectedExecutionTest method testThreadsRejectedExecution.
public void testThreadsRejectedExecution() throws Exception {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
// use a custom pool which rejects any new tasks while currently in progress
// this should force the ThreadsProcessor to run the tasks itself
ExecutorService pool = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
from("seda:start").to("log:before").threads().executorService(pool).delay(1000).to("log:after").to("mock:result");
}
});
context.start();
getMockEndpoint("mock:result").expectedMessageCount(3);
template.sendBody("seda:start", "Hello World");
template.sendBody("seda:start", "Hi World");
template.sendBody("seda:start", "Bye World");
assertMockEndpointsSatisfied();
}
use of java.util.concurrent.ThreadPoolExecutor in project camel by apache.
the class ThreadsRejectedExecutionTest method testThreadsRejectedExecutionCallerNotRuns.
public void testThreadsRejectedExecutionCallerNotRuns() throws Exception {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
// use a custom pool which rejects any new tasks while currently in progress
// this should force the ThreadsProcessor to run the tasks itself
ExecutorService pool = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
from("seda:start").to("log:before").threads().executorService(pool).callerRunsWhenRejected(false).delay(1000).to("log:after").to("mock:result");
}
});
context.start();
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(3);
// wait at most 5 seconds
mock.setResultWaitTime(5000);
template.sendBody("seda:start", "Hello World");
template.sendBody("seda:start", "Hi World");
template.sendBody("seda:start", "Bye World");
// should not be possible to route all 3
mock.assertIsNotSatisfied();
// only 1 should arrive
assertEquals(1, mock.getReceivedCounter());
}
use of java.util.concurrent.ThreadPoolExecutor in project hadoop by apache.
the class AsyncDiskService method awaitTermination.
/**
* Wait for the termination of the thread pools.
*
* @param milliseconds The number of milliseconds to wait
* @return true if all thread pools are terminated without time limit
* @throws InterruptedException
*/
public synchronized boolean awaitTermination(long milliseconds) throws InterruptedException {
long end = Time.now() + milliseconds;
for (Map.Entry<String, ThreadPoolExecutor> e : executors.entrySet()) {
ThreadPoolExecutor executor = e.getValue();
if (!executor.awaitTermination(Math.max(end - Time.now(), 0), TimeUnit.MILLISECONDS)) {
LOG.warn("AsyncDiskService awaitTermination timeout.");
return false;
}
}
LOG.info("All AsyncDiskService threads are terminated.");
return true;
}
use of java.util.concurrent.ThreadPoolExecutor in project hadoop by apache.
the class ErasureCodingWorker method initializeStripedReadThreadPool.
private void initializeStripedReadThreadPool(int num) {
LOG.debug("Using striped reads; pool threads={}", num);
stripedReadPool = new ThreadPoolExecutor(1, num, 60, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new Daemon.DaemonFactory() {
private final AtomicInteger threadIndex = new AtomicInteger(0);
@Override
public Thread newThread(Runnable r) {
Thread t = super.newThread(r);
t.setName("stripedRead-" + threadIndex.getAndIncrement());
return t;
}
}, new ThreadPoolExecutor.CallerRunsPolicy() {
@Override
public void rejectedExecution(Runnable runnable, ThreadPoolExecutor e) {
LOG.info("Execution for striped reading rejected, " + "Executing in current thread");
// will run in the current thread
super.rejectedExecution(runnable, e);
}
});
stripedReadPool.allowCoreThreadTimeOut(true);
}
use of java.util.concurrent.ThreadPoolExecutor in project hadoop by apache.
the class FsDatasetAsyncDiskService method addVolume.
/**
* Starts AsyncDiskService for a new volume
* @param volume the root of the new data volume.
*/
synchronized void addVolume(FsVolumeImpl volume) {
if (executors == null) {
throw new RuntimeException("AsyncDiskService is already shutdown");
}
if (volume == null) {
throw new RuntimeException("Attempt to add a null volume");
}
ThreadPoolExecutor executor = executors.get(volume.getStorageID());
if (executor != null) {
throw new RuntimeException("Volume " + volume + " is already existed.");
}
addExecutorForVolume(volume);
}
Aggregations