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 @Deprecated 036 DiscardOldest, 037 @Deprecated 038 Discard; 039 040 public RejectedExecutionHandler asRejectedExecutionHandler() { 041 if (this == Abort) { 042 return new RejectedExecutionHandler() { 043 @Override 044 public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { 045 if (r instanceof Rejectable) { 046 ((Rejectable) r).reject(); 047 } else { 048 throw new RejectedExecutionException("Task " + r.toString() + " rejected from " + executor.toString()); 049 } 050 } 051 052 @Override 053 public String toString() { 054 return "Abort"; 055 } 056 }; 057 } else if (this == CallerRuns) { 058 return new ThreadPoolExecutor.CallerRunsPolicy() { 059 @Override 060 public String toString() { 061 return "CallerRuns"; 062 } 063 }; 064 } else if (this == DiscardOldest) { 065 return new RejectedExecutionHandler() { 066 @Override 067 public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { 068 if (!executor.isShutdown()) { 069 Runnable rejected = executor.getQueue().poll(); 070 if (rejected instanceof Rejectable) { 071 ((Rejectable) rejected).reject(); 072 } 073 executor.execute(r); 074 } 075 } 076 077 @Override 078 public String toString() { 079 return "DiscardOldest"; 080 } 081 }; 082 } else if (this == Discard) { 083 return new RejectedExecutionHandler() { 084 @Override 085 public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { 086 if (r instanceof Rejectable) { 087 ((Rejectable) r).reject(); 088 } 089 } 090 091 @Override 092 public String toString() { 093 return "Discard"; 094 } 095 }; 096 } 097 throw new IllegalArgumentException("Unknown ThreadPoolRejectedPolicy: " + this); 098 } 099 100}