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