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