Search in sources :

Example 1 with NamedThreadFactory

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));
}
Also used : DatarouterInjector(io.datarouter.inject.DatarouterInjector) Scanner(io.datarouter.scanner.Scanner) Logger(org.slf4j.Logger) ParallelScannerContext(io.datarouter.scanner.ParallelScannerContext) LoggerFactory(org.slf4j.LoggerFactory) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) ArrayList(java.util.ArrayList) NamedThreadFactory(io.datarouter.util.concurrent.NamedThreadFactory) List(java.util.List) ServletContextEvent(javax.servlet.ServletContextEvent) ExecutorServiceTool(io.datarouter.util.concurrent.ExecutorServiceTool) InjectorRetriever(io.datarouter.web.inject.InjectorRetriever) Duration(java.time.Duration) ThreadFactory(java.util.concurrent.ThreadFactory) Pair(io.datarouter.util.tuple.Pair) Collections(java.util.Collections) ServletContextListener(javax.servlet.ServletContextListener) ExecutorService(java.util.concurrent.ExecutorService) PhaseTimer(io.datarouter.util.timer.PhaseTimer) NamedThreadFactory(io.datarouter.util.concurrent.NamedThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) PhaseTimer(io.datarouter.util.timer.PhaseTimer) NamedThreadFactory(io.datarouter.util.concurrent.NamedThreadFactory) ParallelScannerContext(io.datarouter.scanner.ParallelScannerContext) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with NamedThreadFactory

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));
}
Also used : NamedThreadFactory(io.datarouter.util.concurrent.NamedThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) NamedThreadFactory(io.datarouter.util.concurrent.NamedThreadFactory)

Aggregations

NamedThreadFactory (io.datarouter.util.concurrent.NamedThreadFactory)2 ThreadFactory (java.util.concurrent.ThreadFactory)2 DatarouterInjector (io.datarouter.inject.DatarouterInjector)1 ParallelScannerContext (io.datarouter.scanner.ParallelScannerContext)1 Scanner (io.datarouter.scanner.Scanner)1 ExecutorServiceTool (io.datarouter.util.concurrent.ExecutorServiceTool)1 PhaseTimer (io.datarouter.util.timer.PhaseTimer)1 Pair (io.datarouter.util.tuple.Pair)1 InjectorRetriever (io.datarouter.web.inject.InjectorRetriever)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 ServletContextEvent (javax.servlet.ServletContextEvent)1 ServletContextListener (javax.servlet.ServletContextListener)1