use of io.datarouter.util.concurrent.NamedThreadFactory in project datarouter by hotpads.
the class BaseDatarouterServletContextListener method processListeners.
private void processListeners(OnAction onAction, boolean executeAllListenersSynchronously) {
ThreadFactory factory = new NamedThreadFactory("datarouterListenerExecutor", false);
ExecutorService executor = Executors.newFixedThreadPool(allListeners.size(), factory);
var timer = new PhaseTimer();
long shutdownStartMillis = System.currentTimeMillis();
for (Pair<ExecutionMode, List<DatarouterAppListener>> listenersByShutdownMode : listenersByExecutionMods) {
List<DatarouterAppListener> listeners = listenersByShutdownMode.getRight();
ExecutionMode executionMode = executeAllListenersSynchronously ? ExecutionMode.SYNCHRONOUS : listenersByShutdownMode.getLeft();
logger.warn("{} {}: [{}", onAction.display, executionMode.display, listeners.stream().map(listener -> listener.getClass().getSimpleName()).collect(Collectors.joining(", ")) + "]");
if (executionMode == ExecutionMode.SYNCHRONOUS) {
Scanner.of(listeners).map(executeOnAction(onAction)).forEach(timer::add);
} else if (executionMode == ExecutionMode.PARALLEL) {
long shutdownParallelStartMillis = System.currentTimeMillis();
Scanner.of(listeners).parallel(new ParallelScannerContext(executor, listeners.size(), true)).map(executeOnAction(onAction)).forEach(timer::add);
logger.info("Parallel {} total={}", onAction.display, System.currentTimeMillis() - shutdownParallelStartMillis);
}
}
logger.warn(String.format("%s [total=%d][%s]", onAction, System.currentTimeMillis() - shutdownStartMillis, timer.getPhaseNamesAndTimes().stream().map(pair -> pair.getLeft() + "=" + pair.getRight()).collect(Collectors.joining("]["))));
ExecutorServiceTool.shutdown(executor, Duration.ofSeconds(2));
}
use of io.datarouter.util.concurrent.NamedThreadFactory in project datarouter by hotpads.
the class BaseConveyors method start.
protected void start(Conveyor conveyor, int numThreads, long delaySeconds) {
String name = conveyor.getName();
Require.notContains(execsAndConveyorsByName.keySet(), name, name + " already exists");
String threadGroupName = name;
ThreadFactory threadFactory = new NamedThreadFactory(threadGroupName, true);
ScheduledExecutorService exec = Executors.newScheduledThreadPool(numThreads, threadFactory);
for (int i = 0; i < numThreads; ++i) {
exec.scheduleWithFixedDelay(conveyor, delaySeconds, delaySeconds, TimeUnit.SECONDS);
}
instanceRegistry.register(exec);
execsAndConveyorsByName.put(name, new Pair<>(exec, conveyor));
}
Aggregations