AsyncExecutor(String category, int poolSize, int queueLength) { super(poolSize, poolSize, DEFAULT_KEEP_ALIVE_TIME, TimeUnit.SECONDS, createWorkQueue(queueLength)); this.category = category; setThreadFactory(new ThreadFactoryBuilder().setNameFormat(category + "-%d").build()); setRejectedExecutionHandler(this); }
@Override public String toString() { return Strings.apply("%s - Active: %d, Queued: %d, Executed: %d, Blocked: %d, Rejected: %d", category, getActiveCount(), getQueue().size(), executed.getCount(), blocked.getCount(), dropped.getCount()); }
for (AsyncExecutor exec : tasks.getExecutors()) { output.apply("%-20s %8d %8d %8d %12.1f %8d %8d", exec.getCategory(), exec.getActiveCount(), exec.getQueue().size(), exec.getExecuted(), exec.getAverageDuration(), exec.getBlocked(), exec.getDropped());
private void blockUnitExecutorTerminates(String name, AsyncExecutor exec) { LOG.INFO("Waiting for async executor '%s' to terminate...", name); try { if (!exec.awaitTermination(EXECUTOR_SHUTDOWN_WAIT.getSeconds(), TimeUnit.SECONDS)) { LOG.SEVERE(Strings.apply("Executor '%s' did not terminate within 60s. Interrupting " + "tasks...", name)); exec.shutdownNow(); if (!exec.awaitTermination(EXECUTOR_TERMINATION_WAIT.getSeconds(), TimeUnit.SECONDS)) { LOG.SEVERE(Strings.apply("Executor '%s' did not terminate after another 30s!", name)); } } } catch (InterruptedException ex) { Exceptions.ignore(ex); Thread.currentThread().interrupt(); LOG.SEVERE(Strings.apply("Interrupted while waiting for '%s' to terminate!", name)); } }
private void executeNow(ExecutionBuilder.TaskWrapper wrapper) { wrapper.prepare(); AsyncExecutor exec = findExecutor(wrapper.category); wrapper.jobNumber = exec.executed.inc(); wrapper.durationAverage = exec.duration; if (wrapper.synchronizer != null) { scheduleTable.put(wrapper.synchronizer, System.currentTimeMillis()); } exec.execute(wrapper); }
private AsyncExecutor findExecutor(String category) { return executors.computeIfAbsent(category, categoryName -> { Extension config = Sirius.getSettings().getExtension("async.executor", categoryName); return new AsyncExecutor(categoryName, config.get("poolSize").getInteger(), config.get("queueLength").getInteger()); }); }