BUG-1075: ingress back pressure
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / ThreadPoolLoggingExecutor.java
1 /**
2  * Copyright (c) 2013 Cisco Systems, 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.openflowplugin.openflow.md.core;
9
10 import java.util.concurrent.BlockingQueue;
11 import java.util.concurrent.ThreadPoolExecutor;
12 import java.util.concurrent.TimeUnit;
13
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 import com.google.common.util.concurrent.ThreadFactoryBuilder;
18
19 /**
20  * threadPoolExecutor implementation logging exceptions thrown by threads
21  */
22 public class ThreadPoolLoggingExecutor extends ThreadPoolExecutor {
23     
24     private static Logger LOG = LoggerFactory.getLogger(ThreadPoolLoggingExecutor.class);
25
26     /**
27      * @param corePoolSize
28      * @param maximumPoolSize
29      * @param keepAliveTime
30      * @param unit
31      * @param workQueue
32      * @param poolName thread name prefix
33      */
34     public ThreadPoolLoggingExecutor(int corePoolSize, int maximumPoolSize,
35             long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, 
36             final String poolName) {
37         super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, 
38                 new ThreadFactoryBuilder().setNameFormat(poolName+"-%d").build());
39     }
40
41     @Override
42     protected void afterExecute(Runnable r, Throwable t) {
43         super.afterExecute(r, t);
44         // in case of executing pure Runnable
45         if (t != null) {
46             LOG.warn("thread in pool stopped with error", t);
47         }
48     }
49 }