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