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 {@link java.util.concurrent.FutureTask}
029 * when registering new tasks for execution.
030 * <p/>
031 * Instances of {@link RejectableFutureTask} are required to handle {@link ThreadPoolRejectedPolicy#Discard} and
032 * {@link ThreadPoolRejectedPolicy#DiscardOldest} policies correctly, e.g. notify {@link Callable} and {@link Runnable}
033 * tasks when they are rejected. To be notified of rejection tasks have to implement {@link Rejectable} interface: <br/>
034 * <code><pre>
035 * public class RejectableTask implements Runnable, Rejectable {
036 *     &#064;Override
037 *     public void run() {
038 *         // execute task
039 *     }
040 *     &#064;Override
041 *     public void reject() {
042 *         // do something useful on rejection
043 *     }
044 * }
045 * </pre></code>
046 * <p/>
047 * If the task does not implement {@link Rejectable} interface the behavior is exactly the same as with ordinary
048 * {@link ThreadPoolExecutor}.
049 *
050 * @see RejectableFutureTask
051 * @see Rejectable
052 * @see RejectableScheduledThreadPoolExecutor
053 */
054public class RejectableThreadPoolExecutor extends ThreadPoolExecutor {
055
056    public RejectableThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
057                                        BlockingQueue<Runnable> workQueue) {
058        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
059    }
060
061    public RejectableThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
062                                        BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
063        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
064    }
065
066    public RejectableThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
067                                        BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
068        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
069    }
070
071    public RejectableThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
072                                        BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory,
073                                        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        if (runnable instanceof Rejectable) {
080            return new RejectableFutureTask<>(runnable, value);
081        } else {
082            return super.newTaskFor(runnable, value);
083        }
084    }
085
086    @Override
087    protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
088        if (callable instanceof Rejectable) {
089            return new RejectableFutureTask<>(callable);
090        } else {
091            return super.newTaskFor(callable);
092        }
093    }
094
095    @Override
096    public String toString() {
097        // the thread factory often have more precise details what the thread pool is used for
098        if (getThreadFactory() instanceof CamelThreadFactory) {
099            String name = ((CamelThreadFactory) getThreadFactory()).getName();
100            return super.toString() + "[" + name + "]";
101        } else {
102            return super.toString();
103        }
104    }
105
106}