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.Callable;
020import java.util.concurrent.RejectedExecutionHandler;
021import java.util.concurrent.RunnableFuture;
022import java.util.concurrent.ScheduledThreadPoolExecutor;
023import java.util.concurrent.ThreadFactory;
024
025import org.apache.camel.Rejectable;
026
027/**
028 * Scheduled 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 ScheduledThreadPoolExecutor}.
050 *
051 * @see RejectableFutureTask
052 * @see org.apache.camel.Rejectable
053 * @see RejectableThreadPoolExecutor
054 */
055public class RejectableScheduledThreadPoolExecutor extends ScheduledThreadPoolExecutor {
056
057    public RejectableScheduledThreadPoolExecutor(int corePoolSize) {
058        super(corePoolSize);
059    }
060
061    public RejectableScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory) {
062        super(corePoolSize, threadFactory);
063    }
064
065    public RejectableScheduledThreadPoolExecutor(int corePoolSize, RejectedExecutionHandler handler) {
066        super(corePoolSize, handler);
067    }
068
069    public RejectableScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
070        super(corePoolSize, threadFactory, handler);
071    }
072
073    @Override
074    protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
075        if (runnable instanceof Rejectable) {
076            return new RejectableFutureTask<T>(runnable, value);
077        } else {
078            return super.newTaskFor(runnable, value);
079        }
080    }
081
082    @Override
083    protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
084        if (callable instanceof Rejectable) {
085            return new RejectableFutureTask<T>(callable);
086        } else {
087            return super.newTaskFor(callable);
088        }
089    }
090
091    @Override
092    public String toString() {
093        // the thread factory often have more precise details what the thread pool is used for
094        if (getThreadFactory() instanceof CamelThreadFactory) {
095            String name = ((CamelThreadFactory) getThreadFactory()).getName();
096            return super.toString() + "[" + name + "]";
097        } else {
098            return super.toString();
099        }
100    }
101
102}