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