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.spi; 018 019import java.io.Serializable; 020import java.util.concurrent.RejectedExecutionHandler; 021import java.util.concurrent.TimeUnit; 022 023import org.apache.camel.util.concurrent.ThreadPoolRejectedPolicy; 024 025/** 026 * A profile which defines thread pool settings. 027 * <p/> 028 * See more details at <a href="http://camel.apache.org/threading-model.html">threading model</a> 029 */ 030public class ThreadPoolProfile implements Serializable, Cloneable { 031 032 // TODO: Camel 3.0 consider moving to org.apache.camel 033 034 private static final long serialVersionUID = 1L; 035 036 private String id; 037 private Boolean defaultProfile; 038 private Integer poolSize; 039 private Integer maxPoolSize; 040 private Long keepAliveTime; 041 private TimeUnit timeUnit; 042 private Integer maxQueueSize; 043 private Boolean allowCoreThreadTimeOut; 044 private ThreadPoolRejectedPolicy rejectedPolicy; 045 046 /** 047 * Creates a new thread pool profile, with no id set. 048 */ 049 public ThreadPoolProfile() { 050 } 051 052 /** 053 * Creates a new thread pool profile 054 * 055 * @param id id of the profile 056 */ 057 public ThreadPoolProfile(String id) { 058 this.id = id; 059 } 060 061 /** 062 * Gets the id of this profile 063 * 064 * @return the id of this profile 065 */ 066 public String getId() { 067 return id; 068 } 069 070 /** 071 * Sets the id of this profile 072 * 073 * @param id profile id 074 */ 075 public void setId(String id) { 076 this.id = id; 077 } 078 079 /** 080 * Whether this profile is the default profile (there can only be one). 081 * 082 * @return <tt>true</tt> if its the default profile, <tt>false</tt> otherwise 083 */ 084 public Boolean isDefaultProfile() { 085 return defaultProfile != null && defaultProfile; 086 } 087 088 /** 089 * Sets whether this profile is the default profile (there can only be one). 090 * 091 * @param defaultProfile the option 092 */ 093 public void setDefaultProfile(Boolean defaultProfile) { 094 this.defaultProfile = defaultProfile; 095 } 096 097 /** 098 * Gets the core pool size (threads to keep minimum in pool) 099 * 100 * @return the pool size 101 */ 102 public Integer getPoolSize() { 103 return poolSize; 104 } 105 106 /** 107 * Sets the core pool size (threads to keep minimum in pool) 108 * 109 * @param poolSize the pool size 110 */ 111 public void setPoolSize(Integer poolSize) { 112 this.poolSize = poolSize; 113 } 114 115 /** 116 * Gets the maximum pool size 117 * 118 * @return the maximum pool size 119 */ 120 public Integer getMaxPoolSize() { 121 return maxPoolSize; 122 } 123 124 /** 125 * Sets the maximum pool size 126 * 127 * @param maxPoolSize the max pool size 128 */ 129 public void setMaxPoolSize(Integer maxPoolSize) { 130 this.maxPoolSize = maxPoolSize; 131 } 132 133 /** 134 * Gets the keep alive time for inactive threads 135 * 136 * @return the keep alive time 137 */ 138 public Long getKeepAliveTime() { 139 return keepAliveTime; 140 } 141 142 /** 143 * Sets the keep alive time for inactive threads 144 * 145 * @param keepAliveTime the keep alive time 146 */ 147 public void setKeepAliveTime(Long keepAliveTime) { 148 this.keepAliveTime = keepAliveTime; 149 } 150 151 /** 152 * Gets the time unit used for keep alive time 153 * 154 * @return the time unit 155 */ 156 public TimeUnit getTimeUnit() { 157 return timeUnit; 158 } 159 160 /** 161 * Sets the time unit used for keep alive time 162 * 163 * @param timeUnit the time unit 164 */ 165 public void setTimeUnit(TimeUnit timeUnit) { 166 this.timeUnit = timeUnit; 167 } 168 169 /** 170 * Gets the maximum number of tasks in the work queue. 171 * <p/> 172 * Use <tt>-1</tt> or <tt>Integer.MAX_VALUE</tt> for an unbounded queue 173 * 174 * @return the max queue size 175 */ 176 public Integer getMaxQueueSize() { 177 return maxQueueSize; 178 } 179 180 /** 181 * Sets the maximum number of tasks in the work queue. 182 * <p/> 183 * Use <tt>-1</tt> or <tt>Integer.MAX_VALUE</tt> for an unbounded queue 184 * 185 * @param maxQueueSize the max queue size 186 */ 187 public void setMaxQueueSize(Integer maxQueueSize) { 188 this.maxQueueSize = maxQueueSize; 189 } 190 191 /** 192 * Gets whether to allow core threads to timeout 193 * 194 * @return the allow core threads to timeout 195 */ 196 public Boolean getAllowCoreThreadTimeOut() { 197 return allowCoreThreadTimeOut; 198 } 199 200 /** 201 * Sets whether to allow core threads to timeout 202 * 203 * @param allowCoreThreadTimeOut <tt>true</tt> to allow timeout 204 */ 205 public void setAllowCoreThreadTimeOut(Boolean allowCoreThreadTimeOut) { 206 this.allowCoreThreadTimeOut = allowCoreThreadTimeOut; 207 } 208 209 /** 210 * Gets the policy for tasks which cannot be executed by the thread pool. 211 * 212 * @return the policy for the handler 213 */ 214 public ThreadPoolRejectedPolicy getRejectedPolicy() { 215 return rejectedPolicy; 216 } 217 218 /** 219 * Gets the handler for tasks which cannot be executed by the thread pool. 220 * 221 * @return the handler, or <tt>null</tt> if none defined 222 */ 223 public RejectedExecutionHandler getRejectedExecutionHandler() { 224 if (rejectedPolicy != null) { 225 return rejectedPolicy.asRejectedExecutionHandler(); 226 } 227 return null; 228 } 229 230 /** 231 * Sets the handler for tasks which cannot be executed by the thread pool. 232 * 233 * @param rejectedPolicy the policy for the handler 234 */ 235 public void setRejectedPolicy(ThreadPoolRejectedPolicy rejectedPolicy) { 236 this.rejectedPolicy = rejectedPolicy; 237 } 238 239 /** 240 * Overwrites each attribute that is null with the attribute from defaultProfile 241 * 242 * @param defaultProfile profile with default values 243 */ 244 public void addDefaults(ThreadPoolProfile defaultProfile) { 245 if (defaultProfile == null) { 246 return; 247 } 248 if (poolSize == null) { 249 poolSize = defaultProfile.getPoolSize(); 250 } 251 if (maxPoolSize == null) { 252 maxPoolSize = defaultProfile.getMaxPoolSize(); 253 } 254 if (keepAliveTime == null) { 255 keepAliveTime = defaultProfile.getKeepAliveTime(); 256 } 257 if (timeUnit == null) { 258 timeUnit = defaultProfile.getTimeUnit(); 259 } 260 if (maxQueueSize == null) { 261 maxQueueSize = defaultProfile.getMaxQueueSize(); 262 } 263 if (allowCoreThreadTimeOut == null) { 264 allowCoreThreadTimeOut = defaultProfile.getAllowCoreThreadTimeOut(); 265 } 266 if (rejectedPolicy == null) { 267 rejectedPolicy = defaultProfile.getRejectedPolicy(); 268 } 269 } 270 271 @Override 272 public ThreadPoolProfile clone() { 273 ThreadPoolProfile cloned = new ThreadPoolProfile(); 274 cloned.setDefaultProfile(defaultProfile); 275 cloned.setId(id); 276 cloned.setKeepAliveTime(keepAliveTime); 277 cloned.setMaxPoolSize(maxPoolSize); 278 cloned.setMaxQueueSize(maxQueueSize); 279 cloned.setPoolSize(maxPoolSize); 280 cloned.setAllowCoreThreadTimeOut(allowCoreThreadTimeOut); 281 cloned.setRejectedPolicy(rejectedPolicy); 282 cloned.setTimeUnit(timeUnit); 283 return cloned; 284 } 285 286 @Override 287 public String toString() { 288 return "ThreadPoolProfile[" + id + " (" + defaultProfile + ") size:" + poolSize + "-" + maxPoolSize 289 + ", keepAlive: " + keepAliveTime + " " + timeUnit + ", maxQueue: " + maxQueueSize 290 + ", allowCoreThreadTimeOut:" + allowCoreThreadTimeOut + ", rejectedPolicy:" + rejectedPolicy + "]"; 291 } 292 293}