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     */
017    package org.apache.camel;
018    
019    import java.util.concurrent.RejectedExecutionHandler;
020    import java.util.concurrent.ThreadPoolExecutor;
021    import javax.xml.bind.annotation.XmlEnum;
022    import 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     * @version 
034     */
035    @XmlType
036    @XmlEnum(String.class)
037    public enum ThreadPoolRejectedPolicy {
038    
039        Abort, CallerRuns, DiscardOldest, Discard;
040    
041        public RejectedExecutionHandler asRejectedExecutionHandler() {
042            if (this == Abort) {
043                return new ThreadPoolExecutor.AbortPolicy();
044            } else if (this == CallerRuns) {
045                return new ThreadPoolExecutor.CallerRunsPolicy();
046            } else if (this == DiscardOldest) {
047                return new ThreadPoolExecutor.DiscardOldestPolicy();
048            } else if (this == Discard) {
049                return new ThreadPoolExecutor.DiscardPolicy();
050            }
051            throw new IllegalArgumentException("Unknown ThreadPoolRejectedPolicy: " + this);
052        }
053    
054    }