use of java.util.concurrent.RejectedExecutionHandler in project sling by apache.
the class ThreadPoolExecutorCleaningThreadLocalsTest method setUp.
@Before
public void setUp() {
final BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(20);
final RejectedExecutionHandler rejectionHandler = new ThreadPoolExecutor.AbortPolicy();
pool = new ThreadPoolExecutorCleaningThreadLocals(1, 1, 100, TimeUnit.MILLISECONDS, queue, Executors.defaultThreadFactory(), rejectionHandler, listener);
}
use of java.util.concurrent.RejectedExecutionHandler in project voldemort by voldemort.
the class ConsistencyFix method execute.
public String execute(int parallelism, String badKeyFileIn, boolean orphanFormat, String badKeyFileOut) {
ExecutorService badKeyReaderService;
ExecutorService badKeyWriterService;
ExecutorService consistencyFixWorkers;
// Create BadKeyWriter thread
BlockingQueue<BadKeyStatus> badKeyQOut = new ArrayBlockingQueue<BadKeyStatus>(parallelism * 10);
badKeyWriterService = Executors.newSingleThreadExecutor();
badKeyWriterService.submit(new BadKeyWriter(badKeyFileOut, badKeyQOut));
logger.info("Created badKeyWriter.");
// Create ConsistencyFixWorker thread pool
BlockingQueue<Runnable> blockingQ = new ArrayBlockingQueue<Runnable>(parallelism);
RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy();
consistencyFixWorkers = new ThreadPoolExecutor(parallelism, parallelism, 0L, TimeUnit.MILLISECONDS, blockingQ, rejectedExecutionHandler);
logger.info("Created ConsistencyFixWorker pool.");
// Create BadKeyReader thread
CountDownLatch allBadKeysReadLatch = new CountDownLatch(1);
badKeyReaderService = Executors.newSingleThreadExecutor();
BadKeyReader badKeyReader = null;
if (!orphanFormat) {
badKeyReader = new BadKeyReader(allBadKeysReadLatch, badKeyFileIn, this, consistencyFixWorkers, badKeyQOut);
} else {
badKeyReader = new BadKeyOrphanReader(allBadKeysReadLatch, badKeyFileIn, this, consistencyFixWorkers, badKeyQOut);
}
badKeyReaderService.submit(badKeyReader);
logger.info("Created badKeyReader.");
try {
allBadKeysReadLatch.await();
badKeyReaderService.shutdown();
badKeyReaderService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
logger.info("Bad key reader service has shutdown.");
consistencyFixWorkers.shutdown();
consistencyFixWorkers.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
logger.info("All workers have shutdown.");
// Poison the bad key writer to have it exit.
badKeyQOut.put(new BadKeyStatus());
badKeyWriterService.shutdown();
badKeyWriterService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
logger.info("Bad key writer service has shutdown.");
} catch (InterruptedException e) {
logger.error("InterruptedException caught.");
if (logger.isDebugEnabled()) {
e.printStackTrace();
}
} finally {
adminClient.close();
}
// Cobble together a status string for overall execution.
StringBuilder sb = new StringBuilder();
sb.append("\n\n");
sb.append("Exit statuses of various threads:\n");
sb.append("\tBadKeyReader: ");
if (badKeyReader.hasException()) {
sb.append("Had exception!\n");
} else {
sb.append("OK.\n");
}
sb.append("\tBadKeyWriter: ");
if (badKeyReader.hasException()) {
sb.append("Had exception!\n");
} else {
sb.append("OK.\n");
}
sb.append("\n\n");
sb.append(stats.summary());
return sb.toString();
}
use of java.util.concurrent.RejectedExecutionHandler in project tomee by apache.
the class Pool method createExecutor.
private Executor createExecutor() {
final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 10, 60L, SECONDS, new LinkedBlockingQueue<Runnable>(2), new DaemonThreadFactory("org.apache.openejb.util.Pool", hashCode()));
threadPoolExecutor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
@Override
public void rejectedExecution(final Runnable r, final ThreadPoolExecutor tpe) {
if (null == r || null == tpe || tpe.isShutdown() || tpe.isTerminated() || tpe.isTerminating()) {
return;
}
try {
if (!tpe.getQueue().offer(r, 20, SECONDS)) {
org.apache.openejb.util.Logger.getInstance(LogCategory.OPENEJB, "org.apache.openejb.util.resources").warning("Default pool executor failed to run asynchronous process: " + r);
}
} catch (final InterruptedException e) {
//Ignore
}
}
});
return threadPoolExecutor;
}
use of java.util.concurrent.RejectedExecutionHandler in project ddf by codice.
the class IngestCommand method executeWithSubject.
@Override
protected Object executeWithSubject() throws Exception {
if (batchSize * multithreaded > MAX_QUEUE_SIZE) {
throw new IngestException(String.format("batchsize * multithreaded cannot be larger than %d.", MAX_QUEUE_SIZE));
}
final File inputFile = getInputFile();
if (inputFile == null) {
return null;
}
int totalFiles = totalFileCount(inputFile);
fileCount.set(totalFiles);
final ArrayBlockingQueue<Metacard> metacardQueue = new ArrayBlockingQueue<>(batchSize * multithreaded);
ExecutorService queueExecutor = Executors.newSingleThreadExecutor();
final long start = System.currentTimeMillis();
printProgressAndFlush(start, fileCount.get(), 0);
// Registering for the main thread and on behalf of the buildQueue thread;
// the buildQueue thread will unregister itself when the files have all
// been added to the blocking queue and the final registration will
// be held for the await.
phaser.register();
phaser.register();
queueExecutor.submit(() -> buildQueue(inputFile, metacardQueue, start));
final ScheduledExecutorService batchScheduler = Executors.newSingleThreadScheduledExecutor();
BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(multithreaded);
RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy();
ExecutorService executorService = new ThreadPoolExecutor(multithreaded, multithreaded, 0L, TimeUnit.MILLISECONDS, blockingQueue, rejectedExecutionHandler);
final CatalogFacade catalog = getCatalog();
submitToCatalog(batchScheduler, executorService, metacardQueue, catalog, start);
// await on catalog processing threads to complete emptying queue
phaser.awaitAdvance(phaser.arrive());
try {
queueExecutor.shutdown();
executorService.shutdown();
batchScheduler.shutdown();
} catch (SecurityException e) {
LOGGER.info("Executor service shutdown was not permitted: {}", e);
}
printProgressAndFlush(start, fileCount.get(), ingestCount.get() + ignoreCount.get());
long end = System.currentTimeMillis();
console.println();
String elapsedTime = timeFormatter.print(new Period(start, end).withMillis(0));
console.println();
console.printf(" %d file(s) ingested in %s %n", ingestCount.get(), elapsedTime);
LOGGER.debug("{} file(s) ingested in {} [{} records/sec]", ingestCount.get(), elapsedTime, calculateRecordsPerSecond(ingestCount.get(), start, end));
INGEST_LOGGER.info("{} file(s) ingested in {} [{} records/sec]", ingestCount.get(), elapsedTime, calculateRecordsPerSecond(ingestCount.get(), start, end));
if (fileCount.get() != ingestCount.get()) {
console.println();
if ((fileCount.get() - ingestCount.get() - ignoreCount.get()) >= 1) {
String failedAmount = Integer.toString(fileCount.get() - ingestCount.get() - ignoreCount.get());
printErrorMessage(failedAmount + " file(s) failed to be ingested. See the ingest log for more details.");
INGEST_LOGGER.warn("{} files(s) failed to be ingested.", failedAmount);
}
if (ignoreList != null) {
String ignoredAmount = Integer.toString(ignoreCount.get());
printColor(Ansi.Color.YELLOW, ignoredAmount + " file(s) ignored. See the ingest log for more details.");
INGEST_LOGGER.warn("{} files(s) were ignored.", ignoredAmount);
}
}
console.println();
SecurityLogger.audit("Ingested {} files from {}", ingestCount.get(), filePath);
return null;
}
use of java.util.concurrent.RejectedExecutionHandler in project hbase by apache.
the class MobUtils method createMobCompactorThreadPool.
/**
* Creates a thread pool.
* @param conf the Configuration
* @return A thread pool.
*/
public static ExecutorService createMobCompactorThreadPool(Configuration conf) {
int maxThreads = conf.getInt(MobConstants.MOB_COMPACTION_THREADS_MAX, MobConstants.DEFAULT_MOB_COMPACTION_THREADS_MAX);
if (maxThreads == 0) {
maxThreads = 1;
}
final SynchronousQueue<Runnable> queue = new SynchronousQueue<>();
ThreadPoolExecutor pool = new ThreadPoolExecutor(1, maxThreads, 60, TimeUnit.SECONDS, queue, Threads.newDaemonThreadFactory("MobCompactor"), new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
try {
// waiting for a thread to pick up instead of throwing exceptions.
queue.put(r);
} catch (InterruptedException e) {
throw new RejectedExecutionException(e);
}
}
});
((ThreadPoolExecutor) pool).allowCoreThreadTimeOut(true);
return pool;
}
Aggregations