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    Block;
036
037    public RejectedExecutionHandler asRejectedExecutionHandler() {
038        if (this == Abort) {
039            return new RejectedExecutionHandler() {
040                @Override
041                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
042                    if (r instanceof Rejectable rejectable) {
043                        rejectable.reject();
044                    } else {
045                        throw new RejectedExecutionException("Task " + r.toString() + " rejected from " + executor.toString());
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 == Block) {
062            return new RejectedExecutionHandler() {
063                @Override
064                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
065                    if (!executor.isShutdown()) {
066                        try {
067                            executor.getQueue().put(r);
068                        } catch (InterruptedException e) {
069                            Thread.currentThread().interrupt();
070                            throw new RejectedExecutionException("Interrupted while waiting for queue space", e);
071                        }
072                    }
073                }
074
075                @Override
076                public String toString() {
077                    return "Block";
078                }
079            };
080        }
081        throw new IllegalArgumentException("Unknown ThreadPoolRejectedPolicy: " + this);
082    }
083
084}