001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.util.concurrent;
018
019import java.util.concurrent.BlockingQueue;
020import java.util.concurrent.Callable;
021import java.util.concurrent.RejectedExecutionHandler;
022import java.util.concurrent.RunnableFuture;
023import java.util.concurrent.ThreadFactory;
024import java.util.concurrent.ThreadPoolExecutor;
025import java.util.concurrent.TimeUnit;
026
027/**
028 * Thread pool executor that creates {@link RejectableFutureTask} instead of
029 * {@link java.util.concurrent.FutureTask} when registering new tasks for execution.
030 * <p/>
031 * Instances of {@link RejectableFutureTask} are required to handle {@link org.apache.camel.ThreadPoolRejectedPolicy#Discard}
032 * and {@link org.apache.camel.ThreadPoolRejectedPolicy#DiscardOldest} policies correctly, e.g. notify
033 * {@link Callable} and {@link Runnable} tasks when they are rejected.
034 * To be notified of rejection tasks have to implement {@link org.apache.camel.Rejectable} interface: <br/>
035 * <code><pre>
036 * public class RejectableTask implements Runnable, Rejectable {
037 *     &#064;Override
038 *     public void run() {
039 *         // execute task
040 *     }
041 *     &#064;Override
042 *     public void reject() {
043 *         // do something useful on rejection
044 *     }
045 * }
046 * </pre></code>
047 * <p/>
048 * If the task does not implement {@link org.apache.camel.Rejectable} interface the behavior is exactly the same as with
049 * ordinary {@link ThreadPoolExecutor}.
050 *
051 * @see RejectableFutureTask
052 * @see org.apache.camel.Rejectable
053 * @see RejectableScheduledThreadPoolExecutor
054 */
055public class RejectableThreadPoolExecutor extends ThreadPoolExecutor {
056
057    public RejectableThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
058                                        BlockingQueue<Runnable> workQueue) {
059        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
060    }
061
062    public RejectableThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
063                                        BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
064        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
065    }
066
067    public RejectableThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
068                                        BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
069        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
070    }
071
072    public RejectableThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
073                                        BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
074        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
075    }
076
077    @Override
078    protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
079        return new RejectableFutureTask<T>(runnable, value);
080    }
081
082    @Override
083    protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
084        return new RejectableFutureTask<T>(callable);
085    }
086
087    @Override
088    public String toString() {
089        // the thread factory often have more precise details what the thread pool is used for
090        if (getThreadFactory() instanceof CamelThreadFactory) {
091            String name = ((CamelThreadFactory) getThreadFactory()).getName();
092            return super.toString() + "[" + name + "]";
093        } else {
094            return super.toString();
095        }
096    }
097
098}