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.RejectedExecutionException;
020import java.util.concurrent.RejectedExecutionHandler;
021import java.util.concurrent.ThreadPoolExecutor;
022
023/**
024 * Represent the kinds of options for rejection handlers for thread pools.
025 * <p/>
026 * These options are used for fine grained thread pool settings, where you
027 * want to control which handler to use when a thread pool cannot execute
028 * a new task.
029 * <p/>
030 * Camel will by default use <tt>CallerRuns</tt>.
031 */
032public enum ThreadPoolRejectedPolicy {
033
034    Abort, CallerRuns, DiscardOldest, Discard;
035
036    public RejectedExecutionHandler asRejectedExecutionHandler() {
037        if (this == Abort) {
038            return new RejectedExecutionHandler() {
039                @Override
040                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
041                    if (r instanceof Rejectable) {
042                        ((Rejectable)r).reject();
043                    } else {
044                        throw new RejectedExecutionException("Task " + r.toString() + " rejected from " + executor.toString());
045                    }
046                }
047
048                @Override
049                public String toString() {
050                    return "Abort";
051                }
052            };
053        } else if (this == CallerRuns) {
054            return new ThreadPoolExecutor.CallerRunsPolicy() {
055                @Override
056                public String toString() {
057                    return "CallerRuns";
058                }
059            };
060        } else if (this == DiscardOldest) {
061            return new RejectedExecutionHandler() {
062                @Override
063                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
064                    if (!executor.isShutdown()) {
065                        Runnable rejected = executor.getQueue().poll();
066                        if (rejected instanceof Rejectable) {
067                            ((Rejectable) rejected).reject();
068                        }
069                        executor.execute(r);
070                    }
071                }
072
073                @Override
074                public String toString() {
075                    return "DiscardOldest";
076                }
077            };
078        } else if (this == Discard) {
079            return new RejectedExecutionHandler() {
080                @Override
081                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
082                    if (r instanceof Rejectable) {
083                        ((Rejectable) r).reject();
084                    }
085                }
086
087                @Override
088                public String toString() {
089                    return "Discard";
090                }
091            };
092        }
093        throw new IllegalArgumentException("Unknown ThreadPoolRejectedPolicy: " + this);
094    }
095
096}