2 * Copyright (c) 2017 Red Hat, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.genius.infra;
10 import java.util.concurrent.ExecutorService;
11 import java.util.concurrent.RejectedExecutionHandler;
12 import java.util.concurrent.ThreadPoolExecutor;
13 import java.util.concurrent.ThreadPoolExecutor.AbortPolicy;
14 import java.util.concurrent.ThreadPoolExecutor.DiscardPolicy;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
19 * A {@link RejectedExecutionHandler} which logs
20 * instead of throwing an exception (like e.g. {@link ThreadPoolExecutor}'s default
21 * {@link AbortPolicy} does) or just completely silently ignores the execute
22 * (like e.g. {@link DiscardPolicy} does).
24 * <p>This logs an ERROR level message (because typically that's a real problem),
25 * unless the {@link ExecutorService#isShutdown()} - then it logs only an INFO level message
26 * (because typically that's "just" a shutdown ordering issue, and not a real problem).
28 * @author Michael Vorburger.ch
30 public class LoggingRejectedExecutionHandler implements RejectedExecutionHandler {
32 // TODO This utility could eventually be moved to org.opendaylight.infrautils.utils.concurrent
34 private static final Logger LOG = LoggerFactory.getLogger(LoggingRejectedExecutionHandler.class);
37 public void rejectedExecution(Runnable runnable, ThreadPoolExecutor executor) {
38 if (executor.isShutdown() || executor.isTerminating() || executor.isTerminated()) {
39 LOG.info("rejectedExecution, but OK as ExecutorService is terminating or shutdown; "
40 + "executor: {}, runnable: {}", executor, runnable);
42 LOG.error("rejectedExecution (BUT ExecutorService is NOT terminating or shutdown, so that's a PROBLEM); "
43 + "executor: {}, runnable: {}", executor, runnable);