9589aced2a8798eb7a2971819c992fc123dd3b07
[genius.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / genius / infra / LoggingRejectedExecutionHandler.java
1 /*
2  * Copyright (c) 2017 Red Hat, Inc. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.genius.infra;
9
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;
17
18 /**
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).
23  *
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).
27  *
28  * @author Michael Vorburger.ch
29  */
30 public class LoggingRejectedExecutionHandler implements RejectedExecutionHandler {
31
32     // TODO This utility could eventually be moved to org.opendaylight.infrautils.utils.concurrent
33
34     private static final Logger LOG = LoggerFactory.getLogger(LoggingRejectedExecutionHandler.class);
35
36     @Override
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);
41         } else {
42             LOG.error("rejectedExecution (BUT ExecutorService is NOT terminating or shutdown, so that's a PROBLEM); "
43                     + "executor: {}, runnable: {}", executor, runnable);
44         }
45     }
46
47 }