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