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