public void smartShutdown() { long timeout = SMART_SHUTDOWN_TIMEOUT; int lastTaskCount = Integer.MAX_VALUE; while (getExecutorService().getActiveCount() != 0) { if (getExecutorService().getActiveCount() >= lastTaskCount) { timeout -= SMART_SHUTDOWN_STATUS_PRINT_RATE; } else { logger.info("Waiting for " + getExecutorService().getActiveCount() + " tasks to continue the shutdown."); } if (timeout <= 0) { logger.warn("Smart shutdown timeout reached!"); if(JPService.testMode() || JPService.verboseMode()) { StackTracePrinter.printAllStackTraces("pool", logger, LogLevel.INFO, true); } break; } lastTaskCount = getExecutorService().getActiveCount(); try { Thread.sleep(SMART_SHUTDOWN_STATUS_PRINT_RATE); } catch (InterruptedException ex) { logger.warn("Smart shutdown skipped!"); break; } } shutdown(DEFAULT_SHUTDOWN_TIME, TimeUnit.MILLISECONDS); }
private Runnable initReportService() { final Runnable reportService = () -> { final boolean overload; if (executorService.getActiveCount() == executorService.getMaximumPoolSize()) { overload = true; logger.warn("Further tasks will be rejected because executor service overload is detected!"); if (JPService.verboseMode()) { StackTracePrinter.printAllStackTrackes("pool", logger, LogLevel.INFO); } } else if (executorService.getActiveCount() >= ((double) executorService.getMaximumPoolSize() * DEFAULT_WARNING_RATIO)) { overload = true; logger.warn("High Executor service load detected! This can cause system instability issues!"); if (JPService.verboseMode()) { StackTracePrinter.printAllStackTrackes("pool", logger, LogLevel.INFO); } } else { overload = false; } if (JPService.debugMode() || overload) { logger.info("Executor load " + getExecutorLoad() + "% [" + executorService.getActiveCount() + " of " + executorService.getMaximumPoolSize() + " threads processing " + (executorService.getTaskCount() - executorService.getCompletedTaskCount()) + " tasks] in total " + executorService.getCompletedTaskCount() + " are completed."); } }; final ScheduledExecutorService scheduledExecutorService; scheduledExecutorService = executorService instanceof ScheduledExecutorService ? ((ScheduledExecutorService) executorService) : GlobalScheduledExecutorService.getInstance().getExecutorService(); scheduledExecutorService.scheduleAtFixedRate(reportService, DEFAULT_REPORT_RATE, DEFAULT_REPORT_RATE, TimeUnit.MILLISECONDS); return reportService; }
StackTracePrinter.printStackTrace("Dublicated reinit call by:", trace, logger, LogLevel.WARN);