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 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 } 061 throw new IllegalArgumentException("Unknown ThreadPoolRejectedPolicy: " + this); 062 } 063 064}