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.model; 018 019import java.util.concurrent.ExecutorService; 020import java.util.concurrent.TimeUnit; 021 022import javax.xml.bind.annotation.XmlAccessType; 023import javax.xml.bind.annotation.XmlAccessorType; 024import javax.xml.bind.annotation.XmlAttribute; 025import javax.xml.bind.annotation.XmlRootElement; 026import javax.xml.bind.annotation.XmlTransient; 027 028import org.apache.camel.spi.Metadata; 029import org.apache.camel.util.concurrent.ThreadPoolRejectedPolicy; 030 031/** 032 * Specifies that all steps after this node are processed asynchronously 033 */ 034@Metadata(label = "eip,routing") 035@XmlRootElement(name = "threads") 036@XmlAccessorType(XmlAccessType.FIELD) 037public class ThreadsDefinition extends NoOutputDefinition<ThreadsDefinition> implements ExecutorServiceAwareDefinition<ThreadsDefinition> { 038 039 @XmlTransient 040 private ExecutorService executorService; 041 @XmlAttribute 042 private String executorServiceRef; 043 @XmlAttribute 044 @Metadata(javaType = "java.lang.Integer") 045 private String poolSize; 046 @XmlAttribute 047 @Metadata(javaType = "java.lang.Integer") 048 private String maxPoolSize; 049 @XmlAttribute 050 @Metadata(javaType = "java.lang.Long") 051 private String keepAliveTime; 052 @XmlAttribute 053 @Metadata(javaType = "java.util.concurrent.TimeUnit", 054 enums = "NANOSECONDS,MICROSECONDS,MILLISECONDS,SECONDS,MINUTES,HOURS,DAYS") 055 private String timeUnit; 056 @XmlAttribute 057 @Metadata(javaType = "java.lang.Integer") 058 private String maxQueueSize; 059 @XmlAttribute 060 @Metadata(javaType = "java.lang.Boolean") 061 private String allowCoreThreadTimeOut; 062 @XmlAttribute 063 @Metadata(defaultValue = "Threads") 064 private String threadName; 065 @XmlAttribute 066 @Metadata(javaType = "org.apache.camel.util.concurrent.ThreadPoolRejectedPolicy", enums = "Abort,CallerRuns,DiscardOldest,Discard") 067 private String rejectedPolicy; 068 @XmlAttribute 069 @Metadata(defaultValue = "true") 070 private String callerRunsWhenRejected; 071 072 public ThreadsDefinition() { 073 this.threadName = "Threads"; 074 } 075 076 @Override 077 public String getShortName() { 078 return "threads"; 079 } 080 081 @Override 082 public String getLabel() { 083 return "threads"; 084 } 085 086 @Override 087 public String toString() { 088 return "Threads[" + getOutputs() + "]"; 089 } 090 091 /** 092 * To use a custom thread pool 093 */ 094 @Override 095 public ThreadsDefinition executorService(ExecutorService executorService) { 096 setExecutorService(executorService); 097 return this; 098 } 099 100 /** 101 * To refer to a custom thread pool or use a thread pool profile (as 102 * overlay) 103 */ 104 @Override 105 public ThreadsDefinition executorServiceRef(String executorServiceRef) { 106 setExecutorServiceRef(executorServiceRef); 107 return this; 108 } 109 110 /** 111 * Sets the core pool size 112 * 113 * @param poolSize the core pool size to keep minimum in the pool 114 * @return the builder 115 */ 116 public ThreadsDefinition poolSize(int poolSize) { 117 return poolSize(Integer.toString(poolSize)); 118 } 119 120 /** 121 * Sets the core pool size 122 * 123 * @param poolSize the core pool size to keep minimum in the pool 124 * @return the builder 125 */ 126 public ThreadsDefinition poolSize(String poolSize) { 127 setPoolSize(poolSize); 128 return this; 129 } 130 131 /** 132 * Sets the maximum pool size 133 * 134 * @param maxPoolSize the maximum pool size 135 * @return the builder 136 */ 137 public ThreadsDefinition maxPoolSize(int maxPoolSize) { 138 return maxPoolSize(Integer.toString(maxPoolSize)); 139 } 140 141 /** 142 * Sets the maximum pool size 143 * 144 * @param maxPoolSize the maximum pool size 145 * @return the builder 146 */ 147 public ThreadsDefinition maxPoolSize(String maxPoolSize) { 148 setMaxPoolSize(maxPoolSize); 149 return this; 150 } 151 152 /** 153 * Sets the keep alive time for idle threads 154 * 155 * @param keepAliveTime keep alive time 156 * @return the builder 157 */ 158 public ThreadsDefinition keepAliveTime(long keepAliveTime) { 159 return keepAliveTime(Long.toString(keepAliveTime)); 160 } 161 162 /** 163 * Sets the keep alive time for idle threads 164 * 165 * @param keepAliveTime keep alive time 166 * @return the builder 167 */ 168 public ThreadsDefinition keepAliveTime(String keepAliveTime) { 169 setKeepAliveTime(keepAliveTime); 170 return this; 171 } 172 173 /** 174 * Sets the keep alive time unit. By default SECONDS is used. 175 * 176 * @param keepAliveTimeUnits time unit 177 * @return the builder 178 */ 179 public ThreadsDefinition timeUnit(TimeUnit keepAliveTimeUnits) { 180 return timeUnit(keepAliveTimeUnits.name()); 181 } 182 183 /** 184 * Sets the keep alive time unit. By default SECONDS is used. 185 * 186 * @param keepAliveTimeUnits time unit 187 * @return the builder 188 */ 189 public ThreadsDefinition timeUnit(String keepAliveTimeUnits) { 190 setTimeUnit(keepAliveTimeUnits); 191 return this; 192 } 193 194 /** 195 * Sets the maximum number of tasks in the work queue. 196 * <p/> 197 * Use <tt>-1</tt> or <tt>Integer.MAX_VALUE</tt> for an unbounded queue 198 * 199 * @param maxQueueSize the max queue size 200 * @return the builder 201 */ 202 public ThreadsDefinition maxQueueSize(int maxQueueSize) { 203 return maxQueueSize(Integer.toString(maxQueueSize)); 204 } 205 206 /** 207 * Sets the maximum number of tasks in the work queue. 208 * <p/> 209 * Use <tt>-1</tt> or <tt>Integer.MAX_VALUE</tt> for an unbounded queue 210 * 211 * @param maxQueueSize the max queue size 212 * @return the builder 213 */ 214 public ThreadsDefinition maxQueueSize(String maxQueueSize) { 215 setMaxQueueSize(maxQueueSize); 216 return this; 217 } 218 219 /** 220 * Sets the handler for tasks which cannot be executed by the thread pool. 221 * 222 * @param rejectedPolicy the policy for the handler 223 * @return the builder 224 */ 225 public ThreadsDefinition rejectedPolicy(ThreadPoolRejectedPolicy rejectedPolicy) { 226 return rejectedPolicy(rejectedPolicy.name()); 227 } 228 229 /** 230 * Sets the handler for tasks which cannot be executed by the thread pool. 231 * 232 * @param rejectedPolicy the policy for the handler 233 * @return the builder 234 */ 235 public ThreadsDefinition rejectedPolicy(String rejectedPolicy) { 236 setRejectedPolicy(rejectedPolicy); 237 return this; 238 } 239 240 /** 241 * Sets the thread name to use. 242 * 243 * @param threadName the thread name 244 * @return the builder 245 */ 246 public ThreadsDefinition threadName(String threadName) { 247 setThreadName(threadName); 248 return this; 249 } 250 251 /** 252 * Whether or not to use as caller runs as <b>fallback</b> when a task is 253 * rejected being added to the thread pool (when its full). This is only 254 * used as fallback if no rejectedPolicy has been configured, or the thread 255 * pool has no configured rejection handler. 256 * <p/> 257 * Is by default <tt>true</tt> 258 * 259 * @param callerRunsWhenRejected whether or not the caller should run 260 * @return the builder 261 */ 262 public ThreadsDefinition callerRunsWhenRejected(boolean callerRunsWhenRejected) { 263 return callerRunsWhenRejected(Boolean.toString(callerRunsWhenRejected)); 264 } 265 266 /** 267 * Whether or not to use as caller runs as <b>fallback</b> when a task is 268 * rejected being added to the thread pool (when its full). This is only 269 * used as fallback if no rejectedPolicy has been configured, or the thread 270 * pool has no configured rejection handler. 271 * <p/> 272 * Is by default <tt>true</tt> 273 * 274 * @param callerRunsWhenRejected whether or not the caller should run 275 * @return the builder 276 */ 277 public ThreadsDefinition callerRunsWhenRejected(String callerRunsWhenRejected) { 278 setCallerRunsWhenRejected(callerRunsWhenRejected); 279 return this; 280 } 281 282 /** 283 * Whether idle core threads is allowed to timeout and therefore can shrink 284 * the pool size below the core pool size 285 * <p/> 286 * Is by default <tt>false</tt> 287 * 288 * @param allowCoreThreadTimeOut <tt>true</tt> to allow timeout 289 * @return the builder 290 */ 291 public ThreadsDefinition allowCoreThreadTimeOut(boolean allowCoreThreadTimeOut) { 292 return allowCoreThreadTimeOut(Boolean.toString(allowCoreThreadTimeOut)); 293 } 294 295 /** 296 * Whether idle core threads is allowed to timeout and therefore can shrink 297 * the pool size below the core pool size 298 * <p/> 299 * Is by default <tt>false</tt> 300 * 301 * @param allowCoreThreadTimeOut <tt>true</tt> to allow timeout 302 * @return the builder 303 */ 304 public ThreadsDefinition allowCoreThreadTimeOut(String allowCoreThreadTimeOut) { 305 setAllowCoreThreadTimeOut(allowCoreThreadTimeOut); 306 return this; 307 } 308 309 @Override 310 public ExecutorService getExecutorService() { 311 return executorService; 312 } 313 314 @Override 315 public void setExecutorService(ExecutorService executorService) { 316 this.executorService = executorService; 317 } 318 319 @Override 320 public String getExecutorServiceRef() { 321 return executorServiceRef; 322 } 323 324 @Override 325 public void setExecutorServiceRef(String executorServiceRef) { 326 this.executorServiceRef = executorServiceRef; 327 } 328 329 public String getPoolSize() { 330 return poolSize; 331 } 332 333 public void setPoolSize(String poolSize) { 334 this.poolSize = poolSize; 335 } 336 337 public String getMaxPoolSize() { 338 return maxPoolSize; 339 } 340 341 public void setMaxPoolSize(String maxPoolSize) { 342 this.maxPoolSize = maxPoolSize; 343 } 344 345 public String getKeepAliveTime() { 346 return keepAliveTime; 347 } 348 349 public void setKeepAliveTime(String keepAliveTime) { 350 this.keepAliveTime = keepAliveTime; 351 } 352 353 public String getTimeUnit() { 354 return timeUnit; 355 } 356 357 public void setTimeUnit(String timeUnit) { 358 this.timeUnit = timeUnit; 359 } 360 361 public String getMaxQueueSize() { 362 return maxQueueSize; 363 } 364 365 public void setMaxQueueSize(String maxQueueSize) { 366 this.maxQueueSize = maxQueueSize; 367 } 368 369 public String getThreadName() { 370 return threadName; 371 } 372 373 public void setThreadName(String threadName) { 374 this.threadName = threadName; 375 } 376 377 public String getRejectedPolicy() { 378 return rejectedPolicy; 379 } 380 381 public void setRejectedPolicy(String rejectedPolicy) { 382 this.rejectedPolicy = rejectedPolicy; 383 } 384 385 public String getCallerRunsWhenRejected() { 386 return callerRunsWhenRejected; 387 } 388 389 public void setCallerRunsWhenRejected(String callerRunsWhenRejected) { 390 this.callerRunsWhenRejected = callerRunsWhenRejected; 391 } 392 393 public String getAllowCoreThreadTimeOut() { 394 return allowCoreThreadTimeOut; 395 } 396 397 public void setAllowCoreThreadTimeOut(String allowCoreThreadTimeOut) { 398 this.allowCoreThreadTimeOut = allowCoreThreadTimeOut; 399 } 400}