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.Callable; 020import java.util.concurrent.RejectedExecutionHandler; 021import java.util.concurrent.RunnableFuture; 022import java.util.concurrent.ScheduledThreadPoolExecutor; 023import java.util.concurrent.ThreadFactory; 024 025/** 026 * Scheduled thread pool executor that creates {@link RejectableFutureTask} instead of 027 * {@link java.util.concurrent.FutureTask} when registering new tasks for execution. 028 * <p/> 029 * Instances of {@link RejectableFutureTask} are required to handle {@link ThreadPoolRejectedPolicy#Discard} and 030 * {@link ThreadPoolRejectedPolicy#DiscardOldest} policies correctly, e.g. notify {@link Callable} and {@link Runnable} 031 * tasks when they are rejected. To be notified of rejection tasks have to implement {@link Rejectable} interface: <br/> 032 * <code><pre> 033 * public class RejectableTask implements Runnable, Rejectable { 034 * @Override 035 * public void run() { 036 * // execute task 037 * } 038 * @Override 039 * public void reject() { 040 * // do something useful on rejection 041 * } 042 * } 043 * </pre></code> 044 * <p/> 045 * If the task does not implement {@link Rejectable} interface the behavior is exactly the same as with ordinary 046 * {@link ScheduledThreadPoolExecutor}. 047 * 048 * @see RejectableFutureTask 049 * @see Rejectable 050 * @see RejectableThreadPoolExecutor 051 */ 052public class RejectableScheduledThreadPoolExecutor extends ScheduledThreadPoolExecutor { 053 054 public RejectableScheduledThreadPoolExecutor(int corePoolSize) { 055 super(corePoolSize); 056 } 057 058 public RejectableScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory) { 059 super(corePoolSize, threadFactory); 060 } 061 062 public RejectableScheduledThreadPoolExecutor(int corePoolSize, RejectedExecutionHandler handler) { 063 super(corePoolSize, handler); 064 } 065 066 public RejectableScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory, 067 RejectedExecutionHandler handler) { 068 super(corePoolSize, threadFactory, handler); 069 } 070 071 @Override 072 protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) { 073 if (runnable instanceof Rejectable) { 074 return new RejectableFutureTask<>(runnable, value); 075 } else { 076 return super.newTaskFor(runnable, value); 077 } 078 } 079 080 @Override 081 protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) { 082 if (callable instanceof Rejectable) { 083 return new RejectableFutureTask<>(callable); 084 } else { 085 return super.newTaskFor(callable); 086 } 087 } 088 089 @Override 090 public String toString() { 091 // the thread factory often have more precise details what the thread pool is used for 092 if (getThreadFactory() instanceof CamelThreadFactory) { 093 String name = ((CamelThreadFactory) getThreadFactory()).getName(); 094 return super.toString() + "[" + name + "]"; 095 } else { 096 return super.toString(); 097 } 098 } 099 100}