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